# Why does my SIR differential equation solver not work?

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?

• 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? – J. M. will be back soon Mar 14 at 0:44

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}]
`

• Thanks for the help! – DBless Jun 12 at 17:50