# Using ouput of ParametericNDSolve as argument in NMinimize: NMinimize::nnum: function value not a number

I am trying to fit some experimental data and I am trying to use NMinimize function to calculate the parameters that fit the experimental dataset. I am also using ParametricNDSolve and I can't make it to work together.

This is a toy example:

sol0 = ParametricNDSolve[{x'[t] == k*x[t], x[0] == x0}, {x[t]}, {t, 0, 100}, {k, x0}]

So I have to find k and x0 that best fit the exp data

The data is:

data = RandomReal[{0.95 #,  1.05 #}] & /@ ((x[t][1, 10] /. sol0 /. t -> #) & /@ Range[0, 10])
{9.59679, 27.9723, 73.9852, 190.905, 534.717, 1421.69, 4178.41, 11296.8, 30648.2, 79970.6, 229304.}

I create an objective function:

objfn[k_, x0_] :=
Norm[((  x[t][k, x0] /. sol0 /. t -> #  ) & /@
Table[i, {i, 0, 10}]) - data]

Now I try to find the paramters:

NMinimize[{objfn[k, x0], k > 0, x0 > 0}, {k, x0}]

I've got a relative long error message that starts with

NMinimize::nnum.

Unfortunately, I can't figure out the changes that I have to introduce to make this work. I would really appreciate if any could help me.

Thank you very much in advance Fernando

• Greetings! Make the most of Mma.SE and take the tour now. Help us to help you, write an excellent question. Edit if improvable, show due diligence, give brief context, include minimum working examples of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. Commented Sep 3, 2015 at 11:10
• There are things to do after your question has been answered, but don't rush, you may want stay vigilant some time after you get the first answer as its likely that the best approaches may come later improving over a previous reply. Therefore, its a good idea to wait at least 24 hours before voting the deserving answers and accepting the best one for you. (Links contain useful information) Commented Sep 3, 2015 at 12:39
• @rhermans I noticed that you have started using the welcome comment above, and I agree with your recommendations overall, especially with the one regarding waiting a while to accept an answer. I wonder, though: should one really wait as long as 24 hours before voting up deserving answers? Commented Sep 3, 2015 at 16:16
• @MarcoB votes get locked after some time, and some issues with the answer may become evident only after discussion. Because of the time zone differences the comments that may reveal those issues may not come within a few hours. But I concede that this "restriction" is open for discussion and I do not have the final word. Commented Sep 3, 2015 at 16:23

Notice the details of the error:

NMinimize::nnum: The function value [...] is not a number at {k,x0} = {1.91862,1.66351}. >>

You need to be sure that objfn doesn't evaluate until is given a Real value as argument. For that, change objfn[k_, x0_] to objfn[k_Real, x0_Real].

You can also avoid the substitution Rule and evaluate the functions directly.

objfn[k_Real, x0_Real] :=
Norm[Table[sol0[[1, 2]][k, x0], {t, 0, 10}] - data]

Now this definition of objfn can be used with NMinimize

NMinimize[{objfn[k, x0], k > 0, x0 > 0}, {k, x0}]
{527.089, {k -> 0.990979, x0 -> 10.6917}}
• Hi @rhermans, Many thanks for the prompt answer. Commented Sep 3, 2015 at 12:48
• My pleasure! You can return the favor, As you receive help you can give back. Stay in the community, earn reputation and badges, learn and share what you have learned. Commented Sep 3, 2015 at 12:54