0
$\begingroup$

I'm trying to solve a system of SIR differential equations with Mathematica.

My code is:

DSolve[
  {S'[t] == -0.0005 S[t] i[t], 
   i'[t] == 0.0005*S[t] i[t] - 0.1 i[t], 
   R'[t] == 0.1 i[t], 
   S[0] == 1500, i[0] == 1, R[0] == 0}, 
  {S[t], i[t], R[t]}, t]

However, when I evaluate the above, the solver goes on and on forever. Investigating I found this code on another question on Math Stack exchange:

b = 0.18; k = 0.14;
system = 
  {s'[t] == -b s[t] i[t], 
   i'[t] == b s[t] i[t] - k i[t], 
   r'[t] == k i[t], 
   s[0] == 1, i[0] == .007, r[0] == 0};

sol = NDSolve[system, {s, i, r}, {t, 0, 100}]
Plot[Evaluate[{s[t], i[t]} /. sol], {t, 0, 100}]
`

Can somebody explain why this code runs and my code takes forever?

$\endgroup$
1
  • 5
    $\begingroup$ DSolve[] tries to look for a symbolic solution, which takes more effort that a numerical integration like what is done by NDSolve[]. Do you really need a symbolic solution? $\endgroup$ Mar 14, 2019 at 0:44

1 Answer 1

8
$\begingroup$

There is likely no analytic solution. I recommend using NDSolveValue. It will provide interpolation functions that are good approximations almost instantly. You would code it like this:

{sF, iF, rF} =
  NDSolveValue[
    {S'[t] == -0.0005 S[t] i[t], i'[t] == 0.0005*S[t] i[t] - 0.1 i[t], R'[t] == 0.1 i[t], 
     S[0] == 1500, i[0] == 1, R[0] == 0},
    {S, i, R}, {t, 0, 100}]

And you plot the results like this:

Plot[{sF[t], iF[t], rF[t]}, {t, 0, 100}, PlotLegends -> {S, "I", R}]

plot

$\endgroup$
1
  • $\begingroup$ Thanks for the help! $\endgroup$
    – Danny B
    Jun 12, 2019 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.