# 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 == 1500, i == 1, R == 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 == 1, i == .007, r == 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? Mar 14, 2019 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 == 1500, i == 1, R == 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! Jun 12, 2019 at 17:50