Modelling the spread of disease (in MatLab, Runge-Kutta)

In summary, the conversation is about building a model using MATLAB to solve the basic dynamic equations in the SIR model in epidemiology. The model includes three populations: susceptible, infected, and recovered. The equations used are dS/dt = -beta * S*I/N, dI/dt = beta * S*I/N - alpha*I, and dR/dt = alpha*I, where alpha and beta are rates of recovery and infection. The conversation also discusses the introduction of vaccination and the use of the RK4 method. The function used in the model calculates the derivatives of the populations and the RK4 routine updates the populations in each time step. The conversation ends with the resolution of a mistake in the equations that was causing the constants
  • #1
Corrigan
1
0

Homework Statement



Hi!
I'm to build a model which solves the basic dynamic eqns in the so called SIR model in epidemology. I'm using MatLab and RK4.

There are tree populations in this model: Succeptible S, infected I and recovered R.

Homework Equations



The eqations are

dS/dt = -beta * S*I/N
dI/dt = beta * S*I/N - alpha*I
dR/dt = alpha*I

where alpha and beta are rates of recovery and infection (constants).

Vaccination will be introduced aswell, but I have set that to 0 for now.

The Attempt at a Solution



My base .m-file is this:

Code:
clear all
close all


%Step size and number of iterations are defined
dt=1;
N=1000;         %days

P = 1000000;

%The initial conditions are entered as the first row in the array z.

z(1)=P-1;             % = S
z(2)=1;               % = I
z(3)=1;               % = R
t(1)=0;

%RK4 on function

for n=1:(N)
 
    k1=P1f(t(n),z(n,:));
    k2=P1f(t(n)+dt/2,z(n,:)+1/2*k1*dt);
    k3=P1f(t(n)+dt/2,z(n,:)+1/2*k2*dt);
    k4=P1f(t(n)+dt,z(n,:)+k3*dt);
    z(n+1,:)=z(n,:)+1/6*(k1+2*k2+2*k3+k4)*dt;
    t(n+1)=t(n)+dt;
    
end

%The resluts are plotted

subplot(1,2,2)
plot(t,z(:,2),'r')
axis([0 1000 0 100000])
xlabel(''),ylabel('')
subplot(1,2,1)
plot(t,z(:,3))
axis([0 1000 0 100000])
xlabel(''),ylabel('')

with the function P1f.m:

Code:
function f=P1f(t,z)

%Def constants
alpha = 0.25; 
sigma = 50;
T = 0.1;
beta = sigma*T;
gamma= 0;


f(1) = -beta*z(1)*z(2)/z(3) - gamma*z(1);
f(2) = beta*z(1)*z(2)/z(3) - alpha*z(2);
f(3) = gamma*z(1) + alpha*z(2);
end

The constants k in the RK4 routine all become NaN, and I don't understand why. This happenes during the first iteration.
Most of my problem is that I don't fully understand how the 'function' definition in MATLAB works. As you can see, I only define the RHS of the original eqations for the first 3 elements in z. How does the function calculate the values past that first row?
I know the method is sound because I've used it before, last year, and I've forgotten how it works.

Very grateful for any help,
Eric



EDIT: Major mistake was dividing by R instead of N = R + I + S in the right hand sides. Seems to be working now, as far as I can see.
 
Last edited:
Physics news on Phys.org
  • #2




Hi Eric,

It seems like you have a good start on your model. To answer your question about how the function calculates the values past the first row, the function is essentially a set of instructions that tell MATLAB what to do with the input values. In this case, the input values are the time (t) and the array of populations (z). The function uses these values to calculate the derivatives of the populations (dS/dt, dI/dt, dR/dt) and returns them in the array f. The RK4 routine then uses these derivatives to update the populations in the next time step.

As for your issue with the constants becoming NaN, it seems like you have already found the mistake and corrected it. It's always a good idea to double check your equations and make sure all the variables are being used correctly. Good luck with your model! Let us know if you have any other questions.
 

Related to Modelling the spread of disease (in MatLab, Runge-Kutta)

1. How does the Runge-Kutta method work in modelling the spread of disease in MatLab?

The Runge-Kutta method is a numerical technique used to solve differential equations. In the context of modelling the spread of disease, it is used to solve a system of differential equations that represents the dynamics of the disease spread. This method works by breaking down the differential equations into smaller steps, calculating the rate of change at each step, and then using this information to approximate the overall change in the variables over a given time period.

2. What are the advantages of using MatLab for modelling the spread of disease?

MatLab is a powerful computational software that allows for the efficient implementation of complex mathematical models. It has built-in functions and algorithms specifically designed for solving differential equations, making it a suitable tool for modelling the spread of disease. Additionally, MatLab has a user-friendly interface and allows for easy visualization of the results, making it a popular choice for scientists and researchers.

3. How does the choice of parameters affect the accuracy of disease spread models in MatLab?

The choice of parameters in disease spread models can greatly impact the accuracy of the results. These parameters include the initial conditions, transmission rate, recovery rate, and population size. Making informed and realistic assumptions about these parameters is crucial in developing accurate models. Sensitivity analysis can also be performed to determine the impact of each parameter on the overall results.

4. Can MatLab be used to model the spread of different types of diseases?

Yes, MatLab can be used to model the spread of various types of diseases, including infectious diseases and non-communicable diseases. The key is to accurately represent the dynamics of the disease in the system of differential equations and choose appropriate parameters for the specific disease being modelled.

5. What are the limitations of using MatLab for modelling the spread of disease?

While MatLab is a powerful tool for modelling the spread of disease, it does have some limitations. One limitation is that it assumes the disease spread occurs in a well-mixed population, which may not be an accurate representation of real-world scenarios. Additionally, the accuracy of the models is highly dependent on the chosen parameters and assumptions made, which may not always reflect the true dynamics of disease spread.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
861
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
907
  • Engineering and Comp Sci Homework Help
Replies
1
Views
967
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
837
Back
Top