Skip to main content

numerics - How to fit 3 data sets to a model of 4 differential equations?


I'm a biologist and a newbie in Mathematica. I want to fit three data sets to a model consisting of four differential equations and 10 parameters. I want to find the parameters best fitting to my model. I have searched the forum and found several related examples. However, I could not find anything that matched my question.


Here are the details:


I have three time-series datasets: (xdata, ydata, zdata)


time = Quantity[{0, 3, 7, 11, 18, 25, 38, 59}, "seconds"];
tend = QuantityMagnitude[Last[time]];

xdata:


xdata = Quantity[{0, 0.223522, 0.0393934, 0.200991, 0.786874, 1, 
0.265464, 0.106174}, "milligram"];

xfitdata = QuantityMagnitude[Transpose[{time, xdata}]];

ydata:


ydata = Quantity[{0, 0.143397, 0.615163, 0.628621, 0.53515, 0.519805, 
0.757092, 1}, "milligram"];
yfitdata = QuantityMagnitude[Transpose[{time, ydata}]];

wdata:


wdata = Quantity[{0.0064948, 0.221541, 1, 0.434413, 0.732392, 
0.458638, 0.1484432, 0.0294298}, "milligram"];

wfitdata = QuantityMagnitude[Transpose[{time, wdata}]];

I used ParametricNDSolve to solve the 4-DE model:


pfun = {x, y, z, w} /.  
ParametricNDSolve[{x'[t] ==
k1 - k10 x[t] w[t - 25] - k2 x[t] - k3 w[t] w[t],
y'[t] == -k8 y[t] + k10 x[t] w[t - 25] + k3 w[t] x[t],
z'[t] == k4 y[t] - k5 z[t],
w'[t] == (k6 x[t])/(y[t]^n + 1) - k7 w[t], x[t /; t <= 0] == 0.01,
y[t /; t <= 0] == 0.01, z[t /; t <= 0] == 0.01,

w[t /; t <= 0] == 0.01}, {x, y, z, w}, {t, 0, tend}, {k1, k2, k3,
k4, k5, k6, k7, k8, n, k10}]

Then I used FindFit. But I don't know how to specify that xdata is supposed to be fitted to x[t], zdata to z[t] and wdata to w[t] via least-squares fit. For y[t], there are no time-series data, but the parameter (k8) for y[t] is supposed to be determined as well.


I have tried the following, which is apparently wrong:


fit = FindFit[xfitdata, 
pfun[{k1, k2, k3, k4, k5, k6, k7, k8, n, k10}][
t], {{k1, 0.0859}, {k2, 0.0125}, {k3, 0.8541}, {k4, 0.0185}, {k5,
0.1004}, {k6, 0.5002}, {k7, 0.0511}, {k8, 0.0334}, {n, 9}, {k10,
0.8017}}, t]


This is the error message:


FindFit::nrlnum: The function value {0. +<<1>>[0.],-0.223522+<<1>>,-0.0393934+<<1>>,-0.200991+<<1>>,-0.786874+<<1>>[{0.0859,0.0125,0.8541,0.0185,0.1004,0.5002,0.0511,0.0334,9.,0.8017}][18.],-1.+<<1>>[25.],-0.265464+<<1>>,-0.106174+<<1>>[59.]} is not a list of real numbers with dimensions {8} at {k1,k2,k3,k4,k5,k6,k7,k8,n,k10} = {0.0859,0.0125,0.8541,0.0185,0.1004,0.5002,0.0511,0.0334,9.,0.8017}. >>

I'm lost and I would really appreciate your help!



Answer



Since the question isn't clear about which datasets are which and arguably has too many parameters, I'll use the example from here instead:


A+Bk1⇋k2XX+Bk3⟶products}⟹A+2B⟶products


We solve the system and generate some fake data:


sol = ParametricNDSolveValue[{

a'[t] == -k1 a[t] b[t] + k2 x[t], a[0] == 1,
b'[t] == -k1 a[t] b[t] + k2 x[t] - k3 b[t] x[t], b[0] == 1,
x'[t] == k1 a[t] b[t] - k2 x[t] - k3 b[t] x[t], x[0] == 0
}, {a, b, x}, {t, 0, 10}, {k1, k2, k3}
];

abscissae = Range[0., 10., 0.1];
ordinates = With[{k1 = 0.85, k2 = 0.15, k3 = 0.50},
Through[sol[k1, k2, k3][abscissae], List]
];


data = ordinates + RandomVariate[NormalDistribution[0, 0.1^2], Dimensions[ordinates]];
ListLinePlot[data, DataRange -> {0, 10}, PlotRange -> All, AxesOrigin -> {0, 0}]

The data look like this, where blue is A, purple is B, and gold is X:


Plot of kinetic traces


The key to the exercise, of course, is the simultaneous fitting of all three datasets in order for the rate constants to be determined self-consistently. To achieve this we have to prepend to each point a number, i, that labels the dataset:


transformedData = {
ConstantArray[Range@Length[ordinates], Length[abscissae]] // Transpose,
ConstantArray[abscissae, Length[ordinates]],

data
} ~Flatten~ {{2, 3}, {1}};

We also need a model that returns the values for either A, B, or X depending on the value of i:


model[k1_, k2_, k3_][i_, t_] := 
Through[sol[k1, k2, k3][t], List][[i]] /;
And @@ NumericQ /@ {k1, k2, k3, i, t};

The fitting is now straightforward. Although it will help if reasonable initial values are given, this is not strictly necessary here:


fit = NonlinearModelFit[

transformedData,
model[k1, k2, k3][i, t],
{k1, k2, k3}, {i, t}
];

Result of fitting


The result is correct. Worth noting, however, is that the off-diagonal elements of the correlation matrix are quite large:


fit["CorrelationMatrix"]
(* -> {{ 1., 0.764364, -0.101037},
{ 0.764364, 1., -0.376295},

{-0.101037, -0.376295, 1. }} *)

Just to be sure of having directly addressed the question, I will note that the process does not change if we have less than the complete dataset available (although the parameters might be determined with reduced accuracy in this case). Typically it will be most difficult experimentally to measure the intermediate, so let's get rid of the dataset for X (i == 3) and try again:


reducedData = DeleteCases[transformedData, {3, __}];
fit2 = NonlinearModelFit[
reducedData,
model[k1, k2, k3][i, t],
{k1, k2, k3}, {i, t}
];


The main consequence is that the error on k3 is significantly larger:


Result of fitting without concentration data for X


This can be seen to be the result of greater correlation between k1 and k3 when fewer data are available for fitting:


fit2["CorrelationMatrix"]
(* -> {{ 1., 0.7390200, -0.1949590},
{ 0.7390200, 1., 0.0435416},
{-0.1949590, 0.0435416, 1. }} *)

On the other hand, the correlation between k2 and k3 is greatly reduced, so that all of the rate constants are still sufficiently well determined and the overall result does not change substantially.


Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]