5
$\begingroup$

I am trying to solve for a variable which appears in the limits of an integral, rather like in the following;

Clear[f, a, x]
f[a_] := Integrate[x^2 + a x, {x, 0, a }]
f[Power[6, (3)^-1]]
NSolve[5 == f[a], a, Reals]

This works absolutely fine. But the integral I really care about is a mess, and so has, of course, no nice closed form. So I try the following;

Clear[f, a, x]
f[a_] := NIntegrate[x^2 + a x, {x, 0, a }]
f[Power[6, (3)^-1]]
NSolve[5 == f[a], a, Reals]

and this does not work. Instead I get NIntegrate::nlim: x = a is not a valid limit of integration. In both of these examples, the third line evaluates the function f[a] fine. I also tried

f[a_?NumericQ] := NIntegrate[x^2 + a x, {x, 0, a }]

which fails with NSolve::nsmet: This system cannot be solved with the methods available to NSolve.

I rather hoped the link below would answer my problem, but of course it refers to Integrate, not NIntegrate.

https://math.stackexchange.com/questions/38012/compute-unknown-limit-from-known-integral-in-mathematica

I'd be very grateful for any suggestions.

$\endgroup$
6
$\begingroup$
  1. When a is a symbol in f[a_] := NIntegrate[x^2 + a x, {x, 0, a }] you won't get the result.
  2. Defining f[a_?NumericQ] :=NIntegrate[...] it can't work in NSolve[5 == f[a], a, Reals] since a is assumed to be a Symbol therein.

You can use

FindRoot[f[a] - 5, {a, 1}]
{{a -> 1.81712}} 

or you should define f this way:

f[a_] := Integrate[x^2 + a x, {x, 0, a}]
| improve this answer | |
$\endgroup$
3
$\begingroup$

Use FindRoot[]

Clear[f, a, x]
x0 = Power[6, (3)^-1];
f[a_?NumericQ] := NIntegrate[x^2 + a x, {x, 0, a}]

(a /. FindRoot[f[a] == 5, {a, 1}]) == N@x0

(* True *)
| improve this answer | |
$\endgroup$
1
$\begingroup$

What I would suggest is to do a scan of f[a] over a range of a's that you're interested in. You define an interpolation function to smooth out these values, then either FindRoot or NDSolve f[x] == 5.

| improve this answer | |
$\endgroup$

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.