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

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

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}]