I am trying to to fit the data set to an SEIR model, however, I have encountered several problems while doing so and can not make it function..

Here is the most basic version of the code as well as the most basic set of data to test it with. Could you please have a look and help me solve this issue? I have been struggling several long days to solve it, but I am unable do it.

The goal is to fit the data set (which represents the "Infected population" (II in the code) into the model, so that the fit function produces appropriate parameters k1,k2,k3,k4 for the best fit.

data set:

flulist = {{0, 0.001}, {1, 0.003}, {2, 0.007}, {3, 0.025}, {4, 
0.072}, {5, 0.222}, {6, 0.282}, {7, 0.256}, {8, 0.233}, {9, 
0.189}, {10, 0.123}, {11, 0.07}, {12, 0.025}, {13, 0.011}, {14, 


model1 = ParametricNDSolveValue[{
SS'[t] == -k1*SS[t]*II[t] + k2*RR[t],
EE'[t] == k1*SS[t]*II[t] - k3*EE[t],
II'[t] == k3*EE[t] - k4*II[t],
RR'[t] == -k2*RR[t] + k4*II[t],
SS[0] == 0.79, EE[0] == 0, II[0] == 0.31, RR[0] == 0},
II, {t, 0, 300}, {k1, k2, k3, k4}];

fit = FindFit[flulist, model1[k1, k2, k3, k4][t], {k1, k2, k3, k4}, t]

It takes a long time to calculate, and in the end it still gives error messages instead of any results. From working examples online, this process should take a second (not 10 minutes), so something is obviously not right with the fitting process. I would not mind taking it 10 minutes, but it does not yield results.

I have tried different integration Methods, Accuracy/PrecisionGoal, Non/LinearModelFit/FindFit/Predict.. literally everything I found on the internet, however, I can not get the fitting process to work for this system of equations.

The possible reasons that might be causing this error could be:

  1. Fit function might be confusing which equation out of the system of equations it is supposed to match with the data (e.g. it is supposed to find best parameters to match infected, II equation, but does it actually do it?)
  2. integration method problem
  3. Initial parameters SS[0] == 0.79, EE[0] == 0, II[0] == 0.31, RR[0] == 0,, are firmly set. Even though it tries to match the data with different initial value eg. first data point has II[0]=0.001, not II[0] == 0.31.

I have tried approaching all the above problems but.. I would not be posting here if I hadn't tried everything.

If you can fix this, or create an entirely new/different approach to find the best fit, I would be grateful. Thank you very much for helping.

  • $\begingroup$ It looks like one of your data points is entered incorrectly: {6, 282}. $\endgroup$
    – JimB
    Mar 22, 2018 at 23:34
  • $\begingroup$ oh yes, sorry. I wrongly copied it. When I put the code into browser box it split into two different columns and when I tried to put it back to one I accidentally deleted 0. fixed it in the text now $\endgroup$
    – Peter
    Mar 22, 2018 at 23:39
  • $\begingroup$ On my MMA 11.2 Linux, I get a result (and errors!) after a few second, but it matches very poorly. This the "fit": {k1 -> 5.7926, k2 -> 33.3961, k3 -> 0.620497, k4 -> 2.71203}. $\endgroup$
    – anderstood
    Mar 23, 2018 at 1:15
  • $\begingroup$ Can you double check your ODE system if there is any typo? $\endgroup$ Mar 23, 2018 at 4:18

2 Answers 2


It is not (yet?) an actual answer, but too long for a comment. The idea is to solve the equations one by one, so that in the end there is only one "dirty" ODE that might be solved numerically (I hope).

eqs = {SS'[t] == -k1*SS[t]*II[t] + k2*RR[t], 
E'[t] == k1*SS[t]*II[t] - k3*EE[t], II'[t] == k3*EE[t] - k4*II[t], 
RR'[t] == -k2*RR[t] + k4*II[t]}
SSsol[t_] = SS[t] /. DSolve[eqs[[1]], SS[t], t] // First;
eqs2 = eqs /. SS[t] -> SSsol[t] /. SS'[t] -> SSsol'[t] // Simplify;
EEsol[t_] = EE[t] /. DSolve[eqs2[[2]], EE[t], t] // First;
eqs3 = eqs2 /. EE[t] -> EEsol[t] /. EE'[t] -> EEsol'[t] // Simplify;
RRsol[t_] = RR[t] /. DSolve[eqs3[[4]], RR[t], t] // First;
eqs4 = eqs3 /. RR[t] -> RRsol[t] /. RR'[t] -> RRsol'[t] // Simplify;

So now eqs4[[3]] yields an ODE with 4 constants, corresponding to the number of constraints. I have to stop for now and I'm not sure if it is a good trail but I'd think it simplifies the problem.


This is not an answer, but some hints.

Play around with the parameters.

   Plot[Evaluate[{SS[t], II[t], RR[t], EE[t]} /. 
     First@NDSolve[{SS'[t] == -k1*SS[t]*II[t] + k2*RR[t], 
       EE'[t] == k1*SS[t]*II[t] - k3*EE[t], 
       II'[t] == k3*EE[t] - k4*II[t], RR'[t] == -k2*RR[t] + k4*II[t],
       SS[0] == 0.79, EE[0] == 0, II[0] == 0.31, 
       RR[0] == 0} /. {k1 -> 1, k2 -> 1, k3 -> 1, k4 -> 1}, {SS, II, 
   RR, EE}, {t, 0, 15}]], {t, 0, 15}, PlotRange -> {0, 1}, 
   PlotStyle -> {Blue, Red, Green, Magenta}, 
   Epilog -> Point[flulist]], {{k1, 1}, -5, 10}, {{k2, 1}, -5, 
   10}, {{k3, 1}, -5, 5}, {{k4, 1}, -5, 10}]

enter image description here

You will see, it is difficult to find appropriate ones.

Are you sure, that all equations are right? Wikipedia (https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#The_SEIR_model) gives a slightly different SEIR-model than yours.

enter image description here

Does this have any relevance?


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.