Model simulation Code for SEIR

13 views (last 30 days)
Sunday
Sunday on 20 Sep 2024
Edited: Torsten on 20 Sep 2024

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.

Accepted Answer

Sunday
Sunday on 20 Sep 2024
Still encountering same error t,y]=ode45(@seir_model, tspan,[S0,E0,I0,R0]);
  1 Comment
Torsten
Torsten on 20 Sep 2024
Edited: Torsten on 20 Sep 2024
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 -:)

Sign in to comment.

More Answers (2)

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

Sign in to comment.


Torsten
Torsten on 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

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!