I am using Mathematica's FindFit function to fit a parametric solution to my data and I am getting the error
FindFit::nrnum: The function value [complicated expression] is not a real number at {ka,kd,beta} = {0.7,0.1,0.1}
he initial guesses of the parameters {ka=0.7,kd=0.1,beta=0.1} are very close to the values I used to mock up the data, and I can't figure out where I am going wrong. I posted a similar version of this question on stack exchange but didn't get any response.
First I use the ParametricNDSolveValue function to parametrically solve two coupled first order differential equations for the functions 'a[t]' and 'b[t]' with parameters ka, kd, and beta:
sol = ParametricNDSolveValue[{D[a[t], t] == -kd*a[t] + ka*(1 - a[t] - b[t]), a[0] == ka/(ka + kd),
D[b[t], t] == -kd*b[t] + ka*(1 - Exp[-beta*t]) (1 - a[t] - b[t]), b[0] == 0 }, {a, b},
{t, 0, 20}, {ka, kd, beta}]
I can extract the solution for a[t]
for ka=0.7, kd=0.1, beta=0.1
by using:
y1 = sol[0.7, 0.1, 0.1][[1]]
Plotting y1
as a function of t
gives the expected curve:
Plot[y1[t], {t, 0, 20}, PlotRange -> All]
Then I create a data table eyeballing the values from the above plot:
dataA = {{0, 0.87}, {5, 0.84}, {10, 0.75}, {15, 0.68}, {20, 0.62}}
Finally to test FindFit
, I use FindFit
with sol[ka, kd, beta][[1]][t]
to fit the data giving the same initial values for ka
, kd
, and beta
that I used to generate the above data:
fit = FindFit[dataA, sol[ka, kd, beta][[1]][t], {{ka, 0.7}, {kd, 0.1}, {beta, 0.1}}, t]
At which point I get the error:
FindFit::nrnum: "The function value 1/2\ ((-0.87+Abs[0.7[0.]])^2+(-0.84+Abs[0.7[5.]])^2+(-0.75+Abs[0.7[10.]])^2+(-0.68+Abs[0.7[15.]])^2+(-0.62+Abs[0.7[20.]])^2) is not a real number at {ka,kd,beta} = {0.7,0.1,0.1}."
I tried using Abs[sol[ka, kd, beta][[1]][t]]
in FindFit
, that didn't work. Then I tried adding constraints:
fit = FindFit[dataA, {Abs[sol[ka, kd, beta][[1]][t]], {ka > 0, kd > 0,
beta > 0}}, {{ka, 0.7}, {kd, 0.1}, {beta, 0.1}}, t]
And that failed too.
Any clues what is wrong with my code? Thanks a lot!