user3575511
user3575511

Reputation: 322

Mathematica: FindFit for NIntegrate of ParametricNDSolve

I've seen several answers for quite similar topics with usage of ?NumericQ explained and still can not quite understand what is wrong with my implementation and could my example be evaluated at all the way I want it. I have solution of differential equation in form of ParametricNDSolve (I believe that exact form of equation is irrelevant):

sol = ParametricNDSolve[{n'[t] == g/(1/(y - f*y) + b*t + g*t)^2 - a*n[t] - c*n[t]^2, n[0] == y*f}, {n}, {t, 0, 10}, {a, b, c, g, f, y}]

After that I am trying to construct a function for FindFit or similar procedure, Nintegrating over function n[a,b,c,g,f,y,t] I have got above with some multiplier (I have chosen Log[z] as multiplier for simplicity)

Func[z_, a_, b_, c_, g_, f_] := 
 NIntegrate[
  Log[z]*(n[a, b, c, g, f, y][t] /. sol), {t, 0, 10}, {y, 0, Log[z]}]

So I have NIntegrate over my function n[params,t] derived from ParametricNDSolve with multiplier introducing new variable (z) which also present in the limits of integration (in the same form as in multiplier for simplicity of example)

I am able to evaluate the values of my function Func at any point (z) with given values of parameters (a,b,c,g,f): Func(0,1,2,3,4,5) could be evaluated. But for some reasons I cannot use FindFit like that:

FindFit[data, Func[z, a, b, c, g, f], {a, b, c, g, f}, z]

The error is:

NIntegrate::nlim: y = Log[z] is not a valid limit of integration.

I`ve tried a lot of different combinations of ?NumericQ usage and all seems to lead nowhere. Any help would be appreciated! Thanks in advance and sorry for pure English in the problem explanation.

Upvotes: 0

Views: 760

Answers (1)

agentp
agentp

Reputation: 6999

Here is a way to define your function:

sol = n /. 
  ParametricNDSolve[{n'[t] == 
     g/(1/(y - f*y) + b*t + g*t)^2 - a*n[t] - c*n[t]^2, 
    n[0] == y*f}, {n}, {t, 0, 10}, {a, b, c, g, f, y}]

Func[z_?NumericQ, a_?NumericQ, b_?NumericQ, c_?NumericQ, g_?NumericQ, 
  f_?NumericQ] := 
    NIntegrate[Log[z]*sol[a, b, c, g, f, y][t],
      {t, 0, 10}, {y, 0, Log[z]}]

test: Func[2, .45, .5, .13, .12, .2] -> 0.106107

I'm not optimistic you will get good results from FindFit with a function with so many parameters and which is so computationally expensive.

Upvotes: 1

Related Questions