Why is my variable not updating in my SIR model?

  • Context: MATLAB 
  • Thread starter Thread starter kthejohn
  • Start date Start date
  • Tags Tags
    Variable
Click For Summary
SUMMARY

The forum discussion centers on a user encountering issues with updating the variable 'v' in their SIR model simulation. The user is utilizing MATLAB for their implementation, specifically employing a loop structure to iterate through a range of values for 'v'. A key takeaway is the importance of variable scope; the variable 'V' must be declared as global before the loop to ensure it is accessible outside of its local scope. This adjustment allows for proper data storage and retrieval after the loop execution.

PREREQUISITES
  • Understanding of SIR model dynamics in epidemiology
  • Familiarity with MATLAB programming and syntax
  • Knowledge of variable scope and global variables in programming
  • Basic concepts of numerical simulations and iterative algorithms
NEXT STEPS
  • Learn about MATLAB global variables and their usage
  • Explore advanced MATLAB data storage techniques for simulation results
  • Study the impact of parameter variations in SIR models
  • Investigate optimization techniques for improving simulation performance in MATLAB
USEFUL FOR

Researchers in epidemiology, MATLAB programmers, and anyone interested in enhancing their understanding of SIR models and simulation data management.

kthejohn
Messages
4
Reaction score
0
I made an epidemics SIR model but want to vary one of the parameters but it's not working can someone help I pasted the code below I believe the parameter I want to change, v, is not updating:

Matlab:
v_range=linspace(0,.1,1000);
numinfected=0*v_range;

for vi=1:1000;
    
    for klok=1:100000;
    t=klok*dt;
    S_to_I=dt*S*I*a;
    I_to_R=dt*b*I;
    nu=v_range(vi);
    S_to_V=dt*nu*S;
    I_to_Q=dt*I*q;
    Q_to_R=dt*Q*r;

    S=S-S_to_I-S_to_V;
    I=I+S_to_I-I_to_R-I_to_Q;
    R=R+I_to_R+Q_to_R;
    V=V+S_to_V;
    
    %store data...
 
Last edited by a moderator:
Physics news on Phys.org
What matters is how you store the data. Remember, ##V## is within the loop and not accessible outside it. If you want to access it outside the loop, make it global: global V. Put this statement before the loop.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 45 ·
2
Replies
45
Views
7K
Replies
4
Views
4K
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K