Model simulation Code for SEIR
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
function seir_simulation()
% Parameters beta = 0.3; % Effective contact rate without control sigma = 1/5; % Rate of exposed individuals becoming infectious gamma = 1/10; % Rate of infectious individuals recovering N = 1000; % Total population E0 = 1; % Initial number of exposed individuals I0 = 1; % Initial number of infectious individuals R0 = 0; % Initial number of recovered individuals S0 = N - E0 - I0 - R0; % Initial number of susceptible individuals tspan = [0 200]; % Time span for simulation
% SEIR model without control function dydt = seir_model(t, y) S = y(1); E = y(2); I = y(3); R = y(4);
dS = -beta*S*I/N; dE = beta*S*I/N - sigma*E; dI = sigma*E - gamma*I; dR = gamma*I; dydt = [dS; dE; dI; dR]; end
% Solving SEIR model without control [t, y] = ode45(@seir_model, tspan, [S0 E0 I0 R0]);
% Plotting SEIR model without control figure; plot(t, y(:,1), 'b', t, y(:,2), 'r', t, y(:,3), 'g', t, y(:,4), 'k'); legend('Susceptible', 'Exposed', 'Infectious', 'Recovered'); xlabel('Time'); ylabel('Population'); title('SEIR Epidemiological Model without Control');
% SEIR model with control u_values = [0.1 0.2 0.3 0.4 0.5]; % Different values of control u(t)
for u = u_values % Apply control to beta beta_controlled = beta*u;
% SEIR model with control function dydt = seir_model_control(t, y) S = y(1); E = y(2); I = y(3); R = y(4);
dS = -beta_controlled*S*I/N; dE = beta_controlled*S*I/N - sigma*E; dI = sigma*E - gamma*I; dR = gamma*I; dydt = [dS; dE; dI; dR]; end
% Solving SEIR model with control [t_control, y_control] = ode45(@seir_model_control, tspan, [S0 E0 I0 R0]);
% Plotting SEIR model with control figure; plot(t_control, y_control(:,1), 'b', t_control, y_control(:,2), 'r', t_control, y_control(:,3), 'g', t_control, y_control(:,4), 'k'); legend('Susceptible', 'Exposed', 'Infectious', 'Recovered'); xlabel('Time'); ylabel('Population'); title(['SEIR Epidemiological Model with Control u(t) = ' num2str(u)]); end
end Error Misplaced function I am learning the above code but I don't know how to place the function properly.I need help.
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (2)
Shashi Kiran
am 20 Sep. 2024
I understand you are encountering errors related to incorrectly placed functions.
The error occurs because the functions need to follow the correct structure. To resolve this, place the main code at the top and move the function definitions to the end of the script.
Here is how you can organize the code:
function seir_simulation()
% Parameters
beta = 0.3; % Effective contact rate without control
sigma = 1/5; % Rate of exposed individuals becoming infectious
gamma = 1/10; % Rate of infectious individuals recovering
N = 1000; % Total population
E0 = 1; % Initial number of exposed individuals
I0 = 1; % Initial number of infectious individuals
R0 = 0; % Initial number of recovered individuals
S0 = N - E0 - I0 - R0; % Initial number of susceptible individuals
tspan = [0 200]; % Time span for simulation
% Solving SEIR model without control
[t, y] = ode45(@seir_model, tspan, [S0 E0 I0 R0]);
% Plotting SEIR model without control
figure;
plot(t, y(:,1), 'b', t, y(:,2), 'r', t, y(:,3), 'g', t, y(:,4), 'k');
legend('Susceptible', 'Exposed', 'Infectious', 'Recovered');
xlabel('Time');
ylabel('Population');
title('SEIR Epidemiological Model without Control');
% SEIR model with control for different u(t)
u_values = [0.1 0.2 0.3 0.4 0.5]; % Different values of control u(t)
for u = u_values
% Apply control to beta
beta_controlled = beta * u;
% Solving SEIR model with control
[t_control, y_control] = ode45(@(t,y) seir_model_control(t, y, beta_controlled, sigma, gamma, N), tspan, [S0 E0 I0 R0]);
% Plotting SEIR model with control
figure;
plot(t_control, y_control(:,1), 'b', t_control, y_control(:,2), 'r', t_control, y_control(:,3), 'g', t_control, y_control(:,4), 'k');
legend('Susceptible', 'Exposed', 'Infectious', 'Recovered');
xlabel('Time');
ylabel('Population');
title(['SEIR Epidemiological Model with Control u(t) = ' num2str(u)]);
end
% Nested function for SEIR model without control
function dydt = seir_model(t, y)
S = y(1);
E = y(2);
I = y(3);
R = y(4);
dS = -beta * S * I / N;
dE = beta * S * I / N - sigma * E;
dI = sigma * E - gamma * I;
dR = gamma * I;
dydt = [dS; dE; dI; dR];
end
% Nested function for SEIR model with control
function dydt = seir_model_control(t, y, beta_controlled, sigma, gamma, N)
S = y(1);
E = y(2);
I = y(3);
R = y(4);
dS = -beta_controlled * S * I / N;
dE = beta_controlled * S * I / N - sigma * E;
dI = sigma * E - gamma * I;
dR = gamma * I;
dydt = [dS; dE; dI; dR];
end
end
Refer to the following documentation for more details about the function:
Hope this solves your query.
4 Kommentare
Torsten
am 20 Sep. 2024
seir_simulation()
function seir_simulation()
% Parameters
beta = 0.3; % Effective contact rate without control
sigma = 1/5; % Rate of exposed individuals becoming infectious
gamma = 1/10; % Rate of infectious individuals recovering
N = 1000; % Total population
E0 = 1; % Initial number of exposed individuals
I0 = 1; % Initial number of infectious individuals
R0 = 0; % Initial number of recovered individuals
S0 = N - E0 - I0 - R0; % Initial number of susceptible individuals
tspan = [0 200]; % Time span for simulation
% Solving SEIR model without control
[t, y] = ode45(@seir_model, tspan, [S0 E0 I0 R0]);
% Plotting SEIR model without control
figure;
plot(t, y(:,1), 'b', t, y(:,2), 'r', t, y(:,3), 'g', t, y(:,4), 'k');
legend('Susceptible', 'Exposed', 'Infectious', 'Recovered');
xlabel('Time');
ylabel('Population');
title('SEIR Epidemiological Model without Control');
% SEIR model with control
u_values = [0.1 0.2 0.3 0.4 0.5]; % Different values of control u(t)
for u = u_values
% Apply control to beta
beta_controlled = beta*u;
% Solving SEIR model with control
[t_control, y_control] = ode45(@seir_model_control, tspan, [S0 E0 I0 R0]);
% Plotting SEIR model with control
figure;
plot(t_control, y_control(:,1), 'b', t_control, y_control(:,2), 'r', t_control, y_control(:,3), 'g', t_control, y_control(:,4), 'k');
legend('Susceptible', 'Exposed', 'Infectious', 'Recovered');
xlabel('Time');
ylabel('Population');
title(['SEIR Epidemiological Model with Control u(t) = ' num2str(u)]);
end
% SEIR model without control
function dydt = seir_model(t, y)
S = y(1);
E = y(2);
I = y(3);
R = y(4);
dS = -beta*S*I/N;
dE = beta*S*I/N - sigma*E;
dI = sigma*E - gamma*I;
dR = gamma*I;
dydt = [dS; dE; dI; dR];
end
% SEIR model with control
function dydt = seir_model_control(t, y)
S = y(1);
E = y(2);
I = y(3);
R = y(4);
dS = -beta_controlled*S*I/N;
dE = beta_controlled*S*I/N - sigma*E;
dI = sigma*E - gamma*I;
dR = gamma*I;
dydt = [dS; dE; dI; dR];
end
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Histograms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!