1
$\begingroup$

I want to NIntegrate my NDSolve result. I don't have a exact function form because it is coming from the NDSolve result. It looks like Integrate only permit numerical forms.

The function y1[t] could be solved through several differential equations:

S1 = NDSolve[{y1'[t] == -0.9*y1[t] + SuperPlus[z1][t] + 
     SuperMinus[z2][t] + 0.005*SuperPlus[z1][t]*y2[t] + 
     0.008*SuperMinus[z2][t]*y2[t] + 0.005*x[t]*SuperPlus[z1][t] + 
     0.008*x[t]*SuperMinus[z2][t] - 0.008*y1[t]*SuperMinus[z1][t] - 
     0.005*y1[t]*SuperPlus[z2][t], 
   y2'[t] == -0.8*y2[t] + SuperPlus[z2][t] + SuperMinus[z1][t] + 
     0.008*y1[t]*SuperMinus[z1][t] + 0.005*y1[t]*SuperPlus[z2][t] + 
     0.005*x[t]*SuperPlus[z2][t] + 0.008*x[t]*SuperMinus[z1][t] - 
     0.005*SuperPlus[z1][t]*y2[t] - 0.008*SuperMinus[z2][t]*y2[t],
   SuperPlus[z1]'[t] == 0.63*y1[t] - SuperPlus[z1][t],
   SuperPlus[z2]'[t] == 0.64*y2[t] - SuperPlus[z2][t],
   SuperMinus[z1]'[t] == 0.27*y1[t] - SuperMinus[z1][t],
   SuperMinus[z2]'[t] == 0.16*y2[t] - SuperMinus[z2][t],
   x[t] + y1[t] + y2[t] + SuperPlus[z1][t] + SuperPlus[z2][t] + 
     SuperMinus[z1][t] + SuperMinus[z2][t] == 50000000,
   SuperPlus[z1][0] == SuperPlus[z2][0] == SuperMinus[z1][0] == 
    SuperMinus[z2][0] == 0, y1[0] == y2[0] == 10000000, 
   x[0] == 30000000}, {x, y1, y2, SuperPlus[z1], SuperMinus[z1], 
   SuperPlus[z2], SuperMinus[z2]}, {t, 0, 100}]

The result is quite clear in image:

Plot[Evaluate[y1[t] /. S1], {t, 0, 100}, PlotStyle -> Blue, 
 AxesOrigin -> {0, 0}, PlotRange -> All]

The image is here: y1[t]

While, I can't use NIntegrate or Integrate to this function y1[t]. It doesn't give me result.

NIntegrate[Boole[y1[t]], {t, 0, 100}] 

NIntegrate::inumr: The integrand Boole[y1[t]] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,100}}. >>

What should I do? Shall I use Monte Carlo method??

$\endgroup$
  • $\begingroup$ Please clarify your question. What, precisely, do you wish to do but are unable? $\endgroup$ – bbgodfrey Oct 5 '15 at 0:02
  • $\begingroup$ problem is updated and clarified. $\endgroup$ – Zhaohui Li Oct 5 '15 at 0:15
  • 1
    $\begingroup$ Hint: If Plot[Evaluate[y1[t] /. S1], {t, 0, 100}] works, then NIntegrate[Evaluate[y1[t] /. S1], {t, 0, 100}] probably will. The similarity in the syntax of related functions is by design. $\endgroup$ – Michael E2 Oct 5 '15 at 1:08
  • $\begingroup$ Why did you not include the error message? Is it not a hint to what is wrong? $\endgroup$ – Michael E2 Nov 4 '15 at 11:00
2
$\begingroup$
NIntegrate[y1[t] /. S1, {t, 0, 100}]

yields

(* {1.17532*10^9} *}

which seems reasonable. Using Boole was the problem.

$\endgroup$
  • $\begingroup$ I can't believe it is so simple ~!!!! Thanks you very much. You really save my day! $\endgroup$ – Zhaohui Li Oct 5 '15 at 0:22
2
$\begingroup$

As shown by bbgodfrey , NIntegrate can solve your problem, but I just want to add, Integrate can actually handle InterpolatingFunction:

int[t_] = Integrate[y1[t] /. S1, t]

The output is an InterpolatingFunction representing the definite integral of $y_1(t)$ over $[0,t]$. The advantage of this approach is, you don't need to integrate again and again if you want to know the integral at other points. Just compare the following:

Table[int@t, {t, 0, 100, 1/10}]; // AbsoluteTiming
(* {0.015558, Null} *)
int2[tt_] := NIntegrate[y1[t] /. S1, {t, 0, tt}]
Table[int2@t, {t, 0, 100, 1/10}]; // AbsoluteTiming
(* {2.340323, Null} *)

Then

同学,如果你现在所获得的两个答案中的某一个真的帮到了你的话,你完全可以点一下你所满意的答案旁边的小勾(也就是上面那条评论里所说的Click the checkmark sign)来把它采纳(accept)了的。

$\endgroup$
  • $\begingroup$ something went wong... $\endgroup$ – chris Nov 4 '15 at 12:18
  • $\begingroup$ @chris Yeah, after trying for times I noticed the original timing is inaccurate. Modified, thx for pointing out. $\endgroup$ – xzczd Nov 4 '15 at 12:54

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.