MATLAB Why is my variable not updating in my SIR model?

  • Thread starter Thread starter kthejohn
  • Start date Start date
  • Tags Tags
    Variable
AI Thread Summary
The issue with the SIR model not updating the variable 'v' stems from the scope of the variable within the loop. The variable 'V' must be declared as global before the loop to ensure it can be accessed outside of it. Additionally, the way data is stored during the iterations is crucial for tracking changes. Properly managing variable scope and data storage will resolve the problem of the parameter not updating. Ensuring these adjustments are made will allow for successful implementation of the desired parameter variations in the model.
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

Back
Top