# In NDSolve, how to involve in the odes a integration function that depends on ode functions and can't be analytically integrated?

I think it would be better to use this simple code to explain my questions:

Clear["Global*"]

int[a_?NumericQ, b_?NumericQ] :=
NIntegrate[Sin[y1[t] y2[t] r]/r, {r, a, b}];

(* int[a_, b_] := NIntegrate[Sin[y1[t] y2[t] r]/r, {r, a, b}];*)
(* This works, but gives some warnings. *)

odes = {y1'[t] == y2[t] y3[t],
y2'[t] == -y1[t] y3[t] + int[y2[t], y3[t]],
y3'[t] == -0.51 y1[t] y2[t] t, y1[0] == 0, y2[0] == 1, y3[0] == 2};

sol = NDSolve[odes, {y1, y2, y3}, {t, 0, 12}];
Plot[{y1[t], y2[t], y3[t]} /. sol // Evaluate, {t, 0, 12}]


The above code doesn't work, and gives the "NIntegrate::inumr: errors: "The integrand Sin[r\y1[t]\y2[t]]/r has evaluated to non-numerical values for all sampling points in the region with boundaries {{1.,2.}}."

Two question:

(1) I want all the functions in my odes are purely numerically defined (i.e., use _?Numeric@), why mathematica doesn't allow this. mathematica seems treat all ode functions (y1,y2,y3 in my code) analytically, so is it possible to use a numerically defined function (int in my code) that depend on ode functions in NDSolve?

(2) If I delete _?Numeric@ (which is not my favored solution), the code posted works, but gives warnings, how can I avoid these warnings.

Thanks! :)

• Look at the error message again. The variables a and b do receive numerical values, but y1 and y2 do not... Jun 10 '16 at 11:53
• @sebhofer Thanks for the quick response. Then how can I defined a tntegration function that depends on ode functions (i.e., how to define int in my above code) ? Jun 10 '16 at 12:08
• Replacing NIntegrate with Integrate fixed that specific problem. But then you use symbolic again. I don't think this can be done completely numerical. Jun 10 '16 at 12:11
• @Feyre, thanks. Actually, this problem originated from a mathematica versions of a Matlab code in my research. The odes in my research is much more complicated than the code posted here, and it involves a integration term that (a) can't be analytically integrated and (b) depends on ode functions. So you means such odes can't be handled by NDSolve? Jun 10 '16 at 12:22
• make all dependencys explicit arguments. in this example the whole product y1 y2 can be passed as a single argument Jun 10 '16 at 12:27

int[a_, b_] = Integrate[Sin[y1[t] y2[t] r]/r, {r, a, b}, Assumptions -> a < b]
odes = {y1'[t] == y2[t] y3[t],
y2'[t] == -y1[t] y3[t] + int[y2[t], y3[t]],
y3'[t] == -0.51 y1[t] y2[t] t, y1[0] == 0, y2[0] == 1, y3[0] == 2}

sol = NDSolve[odes, {y1, y2, y3}, {t, 0, 12}]


Edit

int[a_?NumericQ, b_?NumericQ, y1_?NumericQ, y2_?NumericQ] :=
NIntegrate[Sin[y1 y2 + Cos[r]]/(Cos[r] + 2), {r, a, b}]
odes := {
y1'[t] == y2[t] y3[t],
y2'[t] == -y1[t] y3[t] + int[y2[t], y3[t], y1[t], y2[t]],
y3'[t] == -0.51 y1[t] y2[t] t,
y1[0] == 0, y2[0] == 1, y3[0] == 2}

sol = NDSolve[odes, {y1, y2, y3}, {t, 0, 12}]


• no need for a delayed set ... Jun 10 '16 at 12:20
• @george2079 you are right, I edit it; thanks.
– user36273
Jun 10 '16 at 12:22
• @rewi thanks, but this not what I want, because if you meet a more complicated integrand, Integrate would run "forever", so your code would fail (I have tested this by changing the integrand). NIntegrate is a must for me. Jun 10 '16 at 12:43
• @rewi, for example, if the integrand in my code is Sin[y1[t] y2[t] + Cos[r]]/(Cos[r] + 2)`, then your code would fail. Jun 10 '16 at 12:56
• @Guo Xinxin it's all right!
– user36273
Jun 10 '16 at 13:28