1
$\begingroup$

I used the following code.

NV[x1_, x2_] := NIntegrate[3 x^2, {x, x1, x2}]

FindRoot[NV[0, t] == 3, {t, .001}, PrecisionGoal -> 20]

Output:

NIntegrate::nlim: x = t is not a valid limit of integration. >> {t -> 1.44225}

The output is correct.

However, I want to remove the comment: "x = t is not a valid limit of integration. >>" from the output.

$\endgroup$
  • 2
    $\begingroup$ Use NV[x1_?NumericQ, x2_?NumericQ] := ... instead of NV[x1_,x2_]. $\endgroup$ – kglr Feb 24 '15 at 11:35
  • $\begingroup$ You can take a look at Quiet if you have faith in the solution. $\endgroup$ – Yves Klett Feb 24 '15 at 11:38
3
$\begingroup$

Nasser gave a fine and simple enough approach. I will propose a different, not that it is better, but just to give another view.

So, if in general you have an integral y[t]==Integrate[f[x],{x,0,t}], it is equivalent to the differential equation: y'[t]=f[t] with the initial condition y[0]==0. You might solve it numerically and then find the solution of the equation y[t]==J, where Jis the integral value you are assumed to know. Its solution yields the upper integral limit being looked for.

Let f[x]=x^2. Then the solution of the differential equation is

nds = NDSolve[{y'[t] == t^2, y[0] == 0}, y, {t, 0, 3}][[1, 1]]; 

and the upper integral limit is given by the following:

    FindRoot[(y[t] /. nds) == 1, {t, 0.01}] // Quiet

(*  {t -> 1.44225}   *)

I do not see any advantage with respect to the approach of Nasser, but it was interesting to me that one may operate also this way.

Have fun!

$\endgroup$
3
$\begingroup$

If a highly accurate solution is desired, I would follow kguler's comment; otherwise, here is a variation on Alexei Boulbitch's approach:

sol = NDSolveValue[{y'[x] == 3 x^2, y[0] == 0, WhenEvent[y[x] == 3, "StopIntegration"]},
   y, {x, 0, 3}];
sol["Domain"][[1, -1]]
(*  1.44225  *)

Or somewhat more directly:

Catch @ NDSolve[{y'[x] == 3 x^2, y[0] == 0, WhenEvent[y[x] == 3, Throw[x]]}, {}, {x, 0, 3}]
(*  1.44225  *)
$\endgroup$
  • $\begingroup$ Please comment what is "Domain". I like your approach very much. $\endgroup$ – Alexei Boulbitch Feb 25 '15 at 9:34
  • $\begingroup$ @AlexeiBoulbitch Please see this answer for information about "Domain" and other properties of InterpolatingFunction. Briefly, "Domain" returns a list of intervals, one for each input to the I-Fn specifying its domain. $\endgroup$ – Michael E2 Feb 25 '15 at 11:18
1
$\begingroup$

You can't do numerical integration with upper limit being symbol. Just use Integrate instead

ClearAll[t, x, x1, x2, nv]
nv[x1_, x2_] := Integrate[3 x^2, {x, x1, x2}]
FindRoot[nv[0, t] == 3, {t, .001}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ But i have some integration where I must have to use NIntegrate. Otherwise it cant be solved $\endgroup$ – Abhijit Saha Feb 24 '15 at 12:57

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.