Message Boards Message Boards

0
|
25676 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Fitting system of Differential equations to a dataset

Posted 11 years ago
Hello everyone! I am trying to model the kinetics of an experimental system. I am trying to find the values of 3 variables in a system of differential equations by fitting them to an experimental data set. I have values for "g" as a function of time and I would like to find the values of "k1", "k2", and "k3" that provide the best fit to my data with minimun and maximum value constraints. The values of the k's in my example below are the correct values, as I have set this up to learn how so I can apply it to a more complex system. I've tried to learn through the online resources of Mathematica but I am having trouble getting it to work for me. So in short, I would like to find the values of k1, k2, and k3 that solve my set of equations to best fit my experimental values of g.

Notebook below: 
k1 = 20;
k2 = 10;
k3 = 2;
totaltime = 3;
dataset1 = {{0, 0}, {0.15, 0.271161}, {0.3, 0.525314}, {0.45,
    0.68284}, {0.6, 0.782049}, {0.75, 0.846992}, {0.9,
    0.890934}, {1.05, 0.921414}, {1.5, 0.969472}};
 solution = NDSolve[
    {a'[t] == -k1*a[t]*b[t],
     b'[t] == -k1*a[t]*b[t],
     c'[t] == k1*a[t]*b[t] - k3*c[t]*f[t],
     d'[t] == -k2*d[t]*e[t],
     e'[t] == -k2*d[t]*e[t],
     f'[t] == k2*d[t]*e[t] - k3*c[t]*f[t],
     g'[t] == k3*c[t]*f[t],
     a[0] == 5,
    b[0] == 2,
    c[0] == 0,
    d[0] == 2,
    e[0] == 1,
    f[0] == 0,
    g[0] == 0},
   {a, b, c, d, e, f, g},
   {t, 0, totaltime},
   MaxSteps -> Infinity];
Show[{ListPlot[dataset1, PlotRange -> {{0, 1.6}, {0, 1}},
   PlotRangeClipping -> True]},
Plot[Evaluate[{f[t], g[t]} /. solution],
  {t, 0, totaltime}, PlotLegends -> {"f", "g"}]]
POSTED BY: A Magyar
5 Replies
The major key is creating a function using ?NumericQ
Not really necessary to use _?NumericQ, at least in version 9. Could try something like
 Clear[k1, k2, k3];
 totaltime = 3;
 dataset1 = {{0, 0}, {0.15, 0.271161}, {0.3, 0.525314}, {0.45,
     0.68284}, {0.6, 0.782049}, {0.75, 0.846992}, {0.9,
     0.890934}, {1.05, 0.921414}, {1.5, 0.969472}};
 
 model = ParametricNDSolveValue[{a'[t] == -k1*a[t]*b[t],
     b'[t] == -k1*a[t]*b[t], c'[t] == k1*a[t]*b[t] - k3*c[t]*f[t],
     d'[t] == -k2*d[t]*e[t], e'[t] == -k2*d[t]*e[t],
    f'[t] == k2*d[t]*e[t] - k3*c[t]*f[t], g'[t] == k3*c[t]*f[t],
    a[0] == 5, b[0] == 2, c[0] == 0, d[0] == 2, e[0] == 1, f[0] == 0,
    g[0] == 0}, g, {t, 0, totaltime}, {k1, k2, k3}];

fit = FindFit[dataset1, model[k1, k2, k3][t], {k1, k2, {k3, 0}}, t]

(* Out[4]= {k1 -> 20.0007, k2 -> 9.99989, k3 -> 2.} *)
Check the fit
Plot[model[k1, k2, k3][t] /. fit, {t, 0, 1.5}, Epilog -> {Red, Point[dataset1]}]
POSTED BY: Ilian Gachevski
The solution posted by Ilian was exactly what I needed. It seems like using ParametricNDSolveValue was the way to go. I really appreciate the help!
POSTED BY: A Magyar
Posted 10 years ago

Hello llian,

I have similar issue modeling reaction kinetic with six rate constants and 8 equations. I wish to fit the model result to my experimental data and generate the fitted rate constants to my experimental data. Here is the post:

http://community.wolfram.com/groups/-/m/t/399734

Thank you for your anticipated help.

Peter

POSTED BY: Peter Adewale
Posted 8 years ago

Hi Ilian,

I am wondering if you could help me with an example that is slightly modified from this one. I won't create an original post because the question builds on the code that you have already posted herein.

Question: How would the code be modified if three data sets existed for "g," "a," and "b" as functions of time, and one wanted to fit the data simultaneously?

Thank you in advance if you have time to reply!

Kirt Robinson

POSTED BY: kirt robinson
For an example of how to do this, please see the documentation for NonlinearModelFit.

The first example under "Generalizations and Extnesions" shows to how find the best fit parameters for a model defined using NDSolve.
 The major key is creating a function using ?NumericQ, in this case called model, which accepts values of the parameters and returns an Interpolation.
POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract