Why is my variable not updating in my SIR model?

  • MATLAB
  • Thread starter kthejohn
  • Start date
  • Tags
    Variable
In summary, the speaker has created an epidemics SIR model but is having trouble varying one of the parameters, v. They have provided the code they are using and have identified the issue as the parameter not updating. They also mention the importance of properly storing the data within the loop.
  • #1
kthejohn
4
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
  • #2
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.
 

Related to Why is my variable not updating in my SIR model?

1. Why is my variable not updating?

There could be several reasons why a variable is not updating. It could be due to a coding error, such as not properly assigning a new value to the variable. It could also be caused by the variable being declared as a constant, which cannot be changed. Another possibility is that the variable is not within the scope of the code where the update is attempted.

2. How do I fix a variable that is not updating?

To fix a variable that is not updating, you need to first identify the cause of the issue. Check your code for any errors or incorrect variable assignments. Make sure the variable is declared in the correct scope and is not a constant. If the variable is being updated in a function, make sure the function is being called correctly. If you are still unable to fix the issue, try debugging your code to identify the problem.

3. Can an immutable variable be updated?

No, an immutable variable cannot be updated. Immutable variables are constants, meaning their value cannot be changed once they are declared. If you need a variable to be updated, it should be declared as a regular variable.

4. Why is my variable still showing the old value after updating it?

This could be due to the variable being updated in one part of the code, but the old value is being displayed in another part. Make sure you are updating and displaying the variable in the same scope. Another possibility is that the update is not being properly executed, so check your code for any errors or logic issues.

5. How do I update a variable in a loop?

To update a variable in a loop, you need to make sure the variable is declared outside of the loop and then update it inside the loop. Otherwise, the variable will be re-initialized with each iteration of the loop, and the update will not be reflected. Also, make sure the loop is being executed as expected and the update is happening within the loop.

Similar threads

  • General Math
Replies
7
Views
3K
  • Calculus and Beyond Homework Help
Replies
10
Views
3K
  • Calculus and Beyond Homework Help
Replies
5
Views
2K
Replies
6
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • General Math
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Math Proof Training and Practice
6
Replies
175
Views
20K
Back
Top