Model simulation Code for SEIR

13 ビュー (過去 30 日間)
Sunday
Sunday 2024 年 9 月 20 日
編集済み: Torsten 2024 年 9 月 20 日

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.

採用された回答

Sunday
Sunday 2024 年 9 月 20 日
Still encountering same error t,y]=ode45(@seir_model, tspan,[S0,E0,I0,R0]);
  1 件のコメント
Torsten
Torsten 2024 年 9 月 20 日
編集済み: Torsten 2024 年 9 月 20 日
Copy @Shashi Kiran 's or my code from above, and you will see that both work.
If you really use
t,y]=ode45(@seir_model, tspan,[S0,E0,I0,R0]);
instead of
[t,y]=ode45(@seir_model, tspan,[S0,E0,I0,R0]);
I know what the error is -:)

サインインしてコメントする。

その他の回答 (2 件)

Shashi Kiran
Shashi Kiran 2024 年 9 月 20 日
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:
  1. function: https://www.mathworks.com/help/matlab/ref/function.html?s_tid=doc_ta#description
Hope this solves your query.
  4 件のコメント
Sunday
Sunday 2024 年 9 月 20 日
[t,y]=ode45(@seir_model, tspan,[S0,E0,I0,R0];
Sunday
Sunday 2024 年 9 月 20 日
Below is the error t,y]=ode45(@seir_model, tspan,[S0,E0,I0,R0];

サインインしてコメントする。


Torsten
Torsten 2024 年 9 月 20 日
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

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by