Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive function within an NDSolve is not updating
    primarykey
    data
    text
    <p>The code below models a simple SIR model (used in disease control) in Mathematica. (I copied it directly from my notebook).</p> <p>The equations can be solved using <code>NDSolve</code> and the solutions are inserted into three different functions for further use.</p> <p>As can be seen the Beta term on the first line varies depending on the value of Inf[t], which is one of the three solutions of the <code>NDSolve</code> function.</p> <p>This code works fine and I have included this in order to better explain my quesion below.</p> <pre><code>Beta = Piecewise[{{0.01, Inf[t] &gt; 20}, {.06, Inf[t] &lt;= 20}}]; Mu = 0.1; Pop = 100; ans = NDSolve[{S'[t] == -Beta S[t] Inf[t], Inf'[t] == Beta S[t] Inf[t] - Mu Inf[t], R'[t] == Mu Inf[t], S[0] == Pop - 1, Inf[0] == 1, R[0] == 0}, {S[t], Inf[t], R[t]}, {t, 0, 10}]; Sus[t_] = S[t] /. ans[[1, 1]]; Infected[t_] = Inf[t] /. ans[[1, 2]]; Rec[t_] = R[t] /. ans[[1, 3]]; </code></pre> <p>I now wanted to update the code so that instead of having an either/or value for the <code>Beta</code> parameter based on the <code>Inf[t]</code> value, I would have the Beta value being equal to the output of a function where <code>Inf[t]</code> is the input. This can be seen below where <code>UpdateTransmission[]</code> is the function. </p> <p>When I try and run the code below though the <code>Beta</code> value remains at 0 and does not increase. The problem is not with the <code>UpdateTransmission</code> function as I have tested this independently.</p> <pre><code>Beta = UpdateTransmission[SpinMatrix, ThresholdMatrix, Inf[t]]; Mu = 0.1; Pop = 100; ans = NDSolve[{S'[t] == -Beta S[t] Inf[t], Inf'[t] == Beta S[t] Inf[t] - Mu Inf[t], R'[t] == Mu Inf[t], S[0] == Pop - 1, Inf[0] == 1, R[0] == 0}, {S[t], Inf[t], R[t]}, {t, 0, 10}]; Sus[t_] = S[t] /. ans[[1, 1]]; Infected[t_] = Inf[t] /. ans[[1, 2]]; Rec[t_] = R[t] /. ans[[1, 3]]; Plot[{Sus[t], Infected[t], Rec[t]}, {t, 0, 5}] </code></pre> <p>Can anyone shed some light on why this may not be running correctly?</p> <p>Edit: here is the updated function</p> <pre><code>UpdateTransmission[S_, Th_, Infect_] := Module[{BetaOverall}, P = S; For[i = 1, i &lt;= Pop, i++, P[[i]] = Sign[Infect - Th[[i]]];]; BetaOverall = ((Count[P, 1]*.02) + (Count[P, -1]*.5))/Pop ] </code></pre> <p>Here are the two lists that are referred to in the code above:</p> <pre><code>SpinMatrix = Table[-1, {Pop}] val := RandomReal[NormalDistribution[.5, .1]] ThresholdMatrix = Table[Pop*val, {Pop}] </code></pre> <p><strong>Edit Edit</strong></p> <p>Ok I've put everything together and tried to plot my three curves. Now as can be seen here they are all flat-lining. The Sus[t] line is staying at 100 whilst the other two seem to be staying below 1. What should be happening here is that the Sus[t] line should drop considerably and the other two lines should ramp up.</p> <p>(I tried to insert and image but I can't as I don't have the reputation points required so I'll just past in the code and you can see the plot yourself on your own machine)</p> <pre><code> Pop = 100; SpinMatrix = Table[-1, {Pop}]; val := RandomReal[NormalDistribution[.5, .1]]; ThresholdMatrix = Table[Pop*val, {Pop}]; updateTransmission[S_, Th_, Infect_] := Module[{}, P = S; For[i = 1, i &lt;= Pop, i++, P[[i]] = Sign[Infect - Th[[i]]];]; Return[((Count[P, 1]*.02) + (Count[P, -1]*.5))/Pop]]; beta[t_] := updateTransmission[SpinMatrix, ThresholdMatrix, Inf[t]]; mu = 0.1; ans = NDSolve[{S'[t] == -beta[t] S[t] Inf[t], Inf'[t] == beta[t] S[t] Inf[t] - mu Inf[t], R'[t] == mu Inf[t], S[0] == Pop - 1, Inf[0] == 1, R[0] == 0}, {S[t], Inf[t], R[t]}, {t, 0, 10}]; Sus[t_] = S[t] /. First@ans; Infected[t_] = Inf[t] /. First@ans; Rec[t_] = R[t] /. First@ans; Plot[{Sus[t], Infected[t], Rec[t]}, {t, 0, 10}] </code></pre> <p>The output that I am expecting should look similar to that of the code given below:</p> <pre><code>Beta = Piecewise[{{0.5, Inf[t] &gt; 20}, {.02, Inf[t] &lt;= 20}}]; Mu = 0.1; Pop = 100; ans = NDSolve[{S'[t] == -Beta S[t] Inf[t], Inf'[t] == Beta S[t] Inf[t] - Mu Inf[t], R'[t] == Mu Inf[t], S[0] == Pop - 1, Inf[0] == 1, R[0] == 0}, {S[t], Inf[t], R[t]}, {t, 0, 10}]; Sus[t_] = S[t] /. ans[[1, 1]]; Infected[t_] = Inf[t] /. ans[[1, 2]]; Rec[t_] = R[t] /. ans[[1, 3]]; Plot[{Sus[t], Infected[t], Rec[t]}, {t, 0, 10}] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload