Skip to main content

differential equations - How do I fit NDSolve result to experimental data?



I have two coupled ordinary differential equations which I solve numerically in Mathematica, but now I want to fit the solution with experimental data.


I do following, (the experimental data corresponds to z[t] in my notation)


ss = 
ParametricNDSolve[{
z'[t] == 4*(1 - X^-1)*z[t] + 4*z[t]*y[t]/X - 8*(z[t])^2 &&
y'[t] == γ*z[t] - γ*y[t]/X && z[0] == z0 &&
y[0] == y0} /. {X -> 20},
{z, y}, {t, 0, 5000}, {γ, z0, y0}]


I know the value of X = 20. The rest has to be determined by fitting. Also time has to be rescaled to fit with experimental data. hence in total of 4 parameters.


γ and y0 does not have any constraint apart from being positive. but x0 has to be very small (close to zero).


data = {{4.01, 0.0338}, {6.02, 0.0719}, {7.99, 0.14}, {9.99, 0.216}, {9.98, 
0.25}, {12., 0.307}, {12., 0.35}, {14., 0.459}, {16., 0.558}, {16.,
0.605}, {18., 0.688}, {20., 0.767}, {22., 0.848}, {24.,
0.898}, {26., 0.932}, {28., 0.965}, {30., 0.98}, {32., 0.987}, {34.,
0.99}, {36., 0.994}, {38., 0.983}, {40., 0.973}, {42.,
0.968}, {44., 0.95}, {46., 0.941}, {48., 0.933}, {50., 0.918}, {52.,
0.911}, {54., 0.901}, {56., 0.89}, {58., 0.881}, {60.,

0.871}, {62., 0.864}, {64., 0.861}, {66., 0.854}, {68.,
0.846}, {70., 0.845}, {72., 0.837}, {74., 0.835}, {76.,
0.825}, {78., 0.825}, {80., 0.822}, {82., 0.817}, {84.,
0.813}, {84., 0.82}, {86., 0.812}, {88., 0.816}, {90., 0.808}, {92.,
0.805}, {94., 0.807}, {96., 0.802}, {98., 0.798}, {100.,
0.799}, {102., 0.797}, {104., 0.79}, {106., 0.797}, {108.,
0.792}, {110., 0.791}, {112., 0.788}, {114., 0.792}, {116.,
0.786}, {118., 0.787}, {120., 0.785}, {122., 0.783}, {124.,
0.788}, {126., 0.783}, {128., 0.781}, {130., 0.78}, {132.,
0.78}, {134., 0.775}, {136., 0.774}, {138., 0.777}, {140.,

0.775}, {142., 0.774}, {144., 0.77}, {146., 0.773}, {148.,
0.772}, {150., 0.768}, {152., 0.772}, {154., 0.764}, {156.,
0.77}, {158., 0.769}, {160., 0.771}, {162., 0.769}, {164.,
0.769}, {166., 0.77}, {168., 0.764}, {170., 0.765}, {172.,
0.762}, {174., 0.761}, {176., 0.762}, {178., 0.765}, {180.,
0.764}, {182., 0.763}, {184., 0.764}, {186., 0.761}, {188.,
0.762}, {190., 0.765}, {192., 0.76}, {194., 0.756}, {196.,
0.764}, {198., 0.757}, {200., 0.762}, {202., 0.758}, {204.,
0.758}, {206., 0.757}, {208., 0.753}, {210., 0.759}, {212.,
0.757}, {214., 0.758}, {216., 0.76}, {218., 0.755}, {220.,

0.758}, {222., 0.754}, {224., 0.758}, {226., 0.754}, {228.,
0.755}, {230., 0.753}, {232., 0.753}}

and then I use NonlinearModelFit](http://reference.wolfram.com/mathematica/ref/NonlinearModelFit.html) to obtain a fit, but it's really bad:


fit1 = 
NonlinearModelFit[
datak,
{z[γ, z0, y0][α*t] /. ss, γ > 0, 0 < z0 < 0.05, y0 > 0, 0 < α < 0.5},
{γ, z0, y0, α}, t]


Comments

Popular posts from this blog

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...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...