2
$\begingroup$

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! :)

$\endgroup$
  • $\begingroup$ Look at the error message again. The variables a and b do receive numerical values, but y1 and y2 do not... $\endgroup$ – sebhofer Jun 10 '16 at 11:53
  • $\begingroup$ @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) ? $\endgroup$ – xinxin guo Jun 10 '16 at 12:08
  • $\begingroup$ Replacing NIntegrate with Integrate fixed that specific problem. But then you use symbolic again. I don't think this can be done completely numerical. $\endgroup$ – Feyre Jun 10 '16 at 12:11
  • $\begingroup$ @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? $\endgroup$ – xinxin guo Jun 10 '16 at 12:22
  • $\begingroup$ make all dependencys explicit arguments. in this example the whole product y1 y2 can be passed as a single argument $\endgroup$ – george2079 Jun 10 '16 at 12:27
3
$\begingroup$
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}]

enter image description here

Edit

to answer your question in your comment:

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}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ no need for a delayed set ... $\endgroup$ – george2079 Jun 10 '16 at 12:20
  • $\begingroup$ @george2079 you are right, I edit it; thanks. $\endgroup$ – user36273 Jun 10 '16 at 12:22
  • $\begingroup$ @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. $\endgroup$ – xinxin guo Jun 10 '16 at 12:43
  • $\begingroup$ @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. $\endgroup$ – xinxin guo Jun 10 '16 at 12:56
  • $\begingroup$ @Guo Xinxin it's all right! $\endgroup$ – user36273 Jun 10 '16 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.