       Re: Integrate query

• To: mathgroup at smc.vnet.net
• Subject: [mg23388] Re: [mg23361] Integrate query
• From: Carl Woll <carlw at u.washington.edu>
• Date: Fri, 5 May 2000 02:07:24 -0400 (EDT)
• References: <200005040659.CAA17509@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi,

One idea is to just use FindRoot. However,  I prefer to use a different
technique. Start from your equation (where I replace f by b in the rhs)

Integrate[f[x],{x,0,a}]==b

Since a depends on b, make this dependence explicit by writing a[b], and
then differentiate the equation to obtain

f[a[b]]a'[b]==1

which is a differential equation, with the obvious initial condition
a==0. So, use NDSolve to get the solution, and evaluate it at the desired
point b. A function which will do this for a function f is given below.

a[x_, f_] := NDSolve[{z'[b] - 1/f[z[b]] == 0, z == 0}, z, {b, 0,
x}][[1,1,2]][x]

The [[1,1,2]] picks out the interpolating function we want, which we then
evaluate at the point x. Now, for a simple test. Let

f[x_] := E^x

a[1, f] // Timing

{0.016 Second, 0.693147}

Integrate[f[x], {x, 0, a[1, f]}]

0.999999

If you want to find a for various values of b with the same function f, then
you shouldn't use the above function, since it will solve the ODE too many
times. Instead, let max be the maximum value of b for which you want to find
a. Then define a function g as follow:

g[f_] :=g[f]= NDSolve[{z'[b] - 1/f[z[b]] == 0, z == 0}, z,
{b,0,max}][[1,1,2]]

Then, g[f][b] will give you the value of a that you want. In this situation,
the NDSolve approach is much better than the FindRoot approach, since the
NDSolve will be evaluated once, but FindRoot will need to be evaluated for
each value of b that you are interested in.

You can improve the accuracy of your result by including an improved
PrecisionGoal and AccuracyGoal in the NDSolve function.

Carl Woll

"A. E. Siegman" wrote:

> I have a non-negative analytic function f[x] whose area (integral of
> f[x] from 0 to Infinity) is unity.
>
> I want to find the upper limit such that the integral up to that limit
> will contain a fixed fraction of the total area, i.e. find  a  such that
>
>    Integrate[f[x], {x,0,a}] == f         (f<=1)
>
> The question is, what's the most efficient way to program this, if I
> want to find  a  with fair accuracy, and with a variety of different
> functions f[x]?
>
> Thanks . . .

```

• Prev by Date: ReExitDialog in ButtonfFunction
• Next by Date: Re: avoid spurious vertical segment in step function plot
• Previous by thread: Re: Integrate query
• Next by thread: Re: Integrate query