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 CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by