29 views (last 30 days)
Show older comments
Abdullahi on 10 Aug 2024 at 8:41
Answered: Torsten on 10 Aug 2024 at 18:17
Open in MATLAB Online
% Define the SITR model as a system of ODEs
function dydt = SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N)
S = y(1); % Susceptible
I = y(2); % Infectious
Q = y(3); % Isolated
T = y(4); % Treated
R = y(5); % Recovered
dSdt = -beta * S * I / N;
dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;
dQdt = delta * I - lambda * Q;
dTdt = alpha * I - mu * T;
dRdt = gamma * I + lambda * Q + mu * T;
dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];
end
% Observed data (replace with actual data)
% Format: [time, infected, isolated, treated, recovered]
observed_data = [
0, 1, 0, 0, 0;
10, 50, 10, 5, 15;
20, 100, 25, 15, 50;
30, 150, 35, 30, 100;
40, 200, 50, 50, 200
];
% Initial conditions
N = 1000000; % Total population
S0 = N - 1;
I0 = 1;
Q0 = 0;
T0 = 0;
R0 = 0;
y0 = [S0, I0, Q0, T0, R0];
% Time points for the solution (based on observed data)
tspan = observed_data(:, 1);
% Define the objective function for optimization
function error = objectiveFunction(params)
beta = params(1);
gamma = params(2);
delta = params(3);
alpha = params(4);
lambda = params(5);
mu = params(6);
% Solve the ODE with the current parameters
[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);
% Interpolate the model's output to match the time points of observed data
model_values = interp1(t, y(:, 2:5), tspan);
% Calculate the sum of squared errors
error = sum((model_values - observed_data(:, 2:5)).^2, 'all');
end
% Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]
initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];
% Perform optimization using fminsearch
options = optimset('MaxFunEvals', 1000, 'MaxIter', 1000);
optimized_params = fminsearch(@objectiveFunction, initial_params, options);
Unrecognized function or variable 'tspan'.
Error in solution>objectiveFunction (line 50)
[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);
Error in fminsearch (line 209)
fv(:,1) = funfcn(x,varargin{:});
% Display optimized parameters
disp('Optimized parameters:');
disp(optimized_params);
% Solve the system with optimized parameters
[t, y] = ode45(@(t, y) SITRModel(t, y, optimized_params(1), optimized_params(2), optimized_params(3), optimized_params(4), optimized_params(5), optimized_params(6), N), linspace(0, 160, 100), y0);
% Plot the solution with optimized parameters
figure;
plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');
legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');
title('Fitted SITR Model for COVID-19');
xlabel('Days');
ylabel('Population');
grid on;
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (2)
Star Strider on 10 Aug 2024 at 13:08
Open in MATLAB Online
It took a few minutes to determine what the problems are with this (there were several).
I got it to work. Most of the problems were with respect to passing all the necessary arguments to the various funcitons. I will let you explore the changes I made, probably the most important of which was using ‘@SITRModel’ to pass the function as an argument.
Try this —
% Define the SITR model as a system of ODEs
function dydt = SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N)
S = y(1); % Susceptible
I = y(2); % Infectious
Q = y(3); % Isolated
T = y(4); % Treated
R = y(5); % Recovered
dSdt = -beta * S * I / N;
dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;
dQdt = delta * I - lambda * Q;
dTdt = alpha * I - mu * T;
dRdt = gamma * I + lambda * Q + mu * T;
dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];
end
% Observed data (replace with actual data)
% Format: [time, infected, isolated, treated, recovered]
observed_data = [0, 1, 0, 0, 0;
10, 50, 10, 5, 15;
20, 100, 25, 15, 50;
30, 150, 35, 30, 100;
40, 200, 50, 50, 200];
% Initial conditions
N = 1000000; % Total population
S0 = N - 1;
I0 = 1;
Q0 = 0;
T0 = 0;
R0 = 0;
y0 = [S0, I0, Q0, T0, R0];
% Time points for the solution (based on observed data)
tspan = observed_data(:, 1);
% Define the objective function for optimization
function error = objectiveFunction(params,tspan,SITRModel,y0,N,observed_data)
beta = params(1);
gamma = params(2);
delta = params(3);
alpha = params(4);
lambda = params(5);
mu = params(6);
% Solve the ODE with the current parameters
[t, y] = ode45(@(t, y) SITRModel(t, y, beta, gamma, delta, alpha, lambda, mu, N), tspan, y0);
% Interpolate the model's output to match the time points of observed data
model_values = interp1(t, y(:, 2:5), tspan);
% Calculate the sum of squared errors
error = sum((model_values - observed_data(:, 2:5)).^2, 'all');
end
% % Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]
initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];
initial_params = initial_params + rand(1,6)/100
initial_params = 1x6
0.3046 0.1054 0.0536 0.0203 0.1001 0.1038
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% % Perform optimization using fminsearch
options = optimset('MaxFunEvals', 1000, 'MaxIter', 1000);
optimized_params = fminsearch(@(params)objectiveFunction(params,tspan,@SITRModel,y0,N,observed_data), initial_params, options);
% Display optimized parameters
disp('Optimized parameters:');
Optimized parameters:
disp(optimized_params);
0.3375 0.1071 0.0483 0.0476 0.0501 0.0536
% optimized_params = initial_params;
% Solve the system with optimized parameters
[t, y] = ode45(@(t, y) SITRModel(t, y, optimized_params(1), optimized_params(2), optimized_params(3), optimized_params(4), optimized_params(5), optimized_params(6), N), linspace(0, 160, 100), y0);
% Plot the solution with optimized parameters
figure;
plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');
legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');
title('Fitted SITR Model for COVID-19');
xlabel('Days');
ylabel('Population');
grid on;
.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Torsten on 10 Aug 2024 at 18:17
Open in MATLAB Online
% Observed data (replace with actual data)
% Format: [time, infected, isolated, treated, recovered]
observed_data = [
0, 1, 0, 0, 0;
10, 50, 10, 5, 15;
20, 100, 25, 15, 50;
30, 150, 35, 30, 100;
40, 200, 50, 50, 200
];
% Initial conditions
N = 1000000; % Total population
S0 = N - 1;
I0 = 1;
Q0 = 0;
T0 = 0;
R0 = 0;
y0 = [S0, I0, Q0, T0, R0];
% Time points for the solution (based on observed data)
tspan = observed_data(:, 1);
% Initial guess for parameters [beta, gamma, delta, alpha, lambda, mu]
initial_params = [0.3, 0.1, 0.05, 0.02, 0.1, 0.1];
objectiveFunction(initial_params,y0,N,observed_data)
ans = 2.8078e+04
% Perform optimization using fminsearch
options = optimset('MaxFunEvals', 10000, 'MaxIter', 10000);
%optimized_params = fminsearch(@(params)objectiveFunction(params,y0,N,observed_data), initial_params, options);
optimized_params = fmincon(@(params)objectiveFunction(params,y0,N,observed_data), initial_params, [],[],[],[],zeros(6,1),[],[],options);
Local minimum possible. Constraints satisfied.fmincon stopped because the size of the current step is less thanthe value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
% Display optimized parameters
disp('Optimized parameters:');
Optimized parameters:
disp(optimized_params);
0.3375 0.1334 0.0354 0.0342 0.0000 0.0000
objectiveFunction(optimized_params,y0,N,observed_data)
ans = 2.3540e+04
% Solve the system with optimized parameters
[t, y] = ode15s(@(t, y) SITRModel(t, y, optimized_params,N), linspace(0,160,100), y0,odeset('RelTol',1e-9,'AbsTol',1e-9));
% Plot the solution with optimized parameters
figure;
plot(t, y(:, 1), 'b-', t, y(:, 2), 'r-', t, y(:, 3), 'g-', t, y(:, 4), 'm-', t, y(:, 5), 'k-');
legend('Susceptible', 'Infectious', 'Isolated', 'Treated', 'Recovered');
title('Fitted SITR Model for COVID-19');
xlabel('Days');
ylabel('Population');
grid on;
% Define the objective function for optimization
function error = objectiveFunction(params,y0,N,observed_data)
% Solve the ODE with the current parameters
[t, y] = ode15s(@(t, y) SITRModel(t, y, params,N), observed_data(:,1), y0,odeset('RelTol',1e-9,'AbsTol',1e-9));
% Calculate the sum of squared errors
error = sum((y(:,2:5) - observed_data(:,2:5)).^2, 'all');
end
% Define the SITR model as a system of ODEs
function dydt = SITRModel(t, y, params,N)
beta = params(1);
gamma = params(2);
delta = params(3);
alpha = params(4);
lambda = params(5);
mu = params(6);
S = y(1); % Susceptible
I = y(2); % Infectious
Q = y(3); % Isolated
T = y(4); % Treated
R = y(5); % Recovered
dSdt = -beta * S * I / N;
dIdt = beta * S * I / N - gamma * I - delta * I - alpha * I;
dQdt = delta * I - lambda * Q;
dTdt = alpha * I - mu * T;
dRdt = gamma * I + lambda * Q + mu * T;
dydt = [dSdt; dIdt; dQdt; dTdt; dRdt];
end
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
RF and Mixed SignalAntenna ToolboxAntenna CatalogMonopole Antennas
Find more on Monopole Antennas in Help Center and File Exchange
Tags
- parse error
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office