I am fitting data using a system of coupled differential equations and have defined a model function like so:
Clear[a, b, c, d, e, f, fit];
fit[a_?NumberQ,b_?NumberQ,c_?NumberQ,d_?NumberQ,e_?NumberQ,f_?NumberQ] :=
(fit[a, b, c, d, e, f] = NDSolve[{
x'[t] == a - b*x[t] + c*(y[t] - x[t]),
y'[t] == d - e*y[t] + f*(x[t] - y[t]),
x[0] == 0,y[0] == 0},{x, y},{t, 0, 100}] // First // Values)
It works just fine when I plug in parameter values just to check out some plots:
Plot[{fit[1, 0.5, 0.5, 0.8, 0.5, 0.5][[1]][t],fit[1, 0.5, 0.5, 0.8, 0.5, 0.5][[2]][t]},
{t, 0, 15}, PlotStyle -> {{Dashed, Black}, Red}]
However, I want to reference the x and y fits separately, without specifying parameter values, like so:
fit[a,b,c,d,e,f][[1]][t]
(* Interpolating Function[...][t] *)
instead what that gets me is:
fit[a,b,c,d,e,f][[1]][t]
(* a[t] *)
I want to do this because I want to use a KroneckerDelta to make a combined fit that I can pass into NonlinearModel fit like in this solution: https://mathematica.stackexchange.com/a/15913/52355 because as is my work around is I have two functions:
fitX[a_?NumberQ,b_?NumberQ,c_?NumberQ,d_?NumberQ,e_?NumberQ,f_?NumberQ] :=
(fitX[a, b, c, d, e, f] = {x}/.NDSolve[{
x'[t] == a - b*x[t] + c*(y[t] - x[t]),
y'[t] == d - e*y[t] + f*(x[t] - y[t]),
x[0] == 0,y[0] == 0},{x, y},{t, 0, 100}] // First)
fitY[a_?NumberQ,b_?NumberQ,c_?NumberQ,d_?NumberQ,e_?NumberQ,f_?NumberQ] :=
(fitY[a, b, c, d, e, f] = {y}/.NDSolve[{
x'[t] == a - b*x[t] + c*(y[t] - x[t]),
y'[t] == d - e*y[t] + f*(x[t] - y[t]),
x[0] == 0,y[0] == 0},{x, y},{t, 0, 100}] // First)
that I pass in separately and essentially fit the curves to the data one at a time, but I would prefer to fit the both curves to the data at once. Is there a way that I could rewrite the original fit function so that I can recover the interpolating function without first passing in parameter values, or is that just not possible?
When I tried searching up this problem, I found this post: ParametricNDSolve not returning an interpolating function after specifying parameter vlaue which doesn't seem to be quite the problem I am having.
fit[a,b,c,d,e,f][[1]][t]
, WRI introducedIndexed[fit[a,b,c,d,e,f],1][t]
, which delays takingPart
1 untilfit[..]
evaluates to aList
. Does that help? -- You might be better off usingParametricNDSolve[]
though. $\endgroup$