Re: Problem using Solve or Nsolve

• To: mathgroup at smc.vnet.net
• Subject: [mg124763] Re: Problem using Solve or Nsolve
• From: Bill Rowe <readnews at sbcglobal.net>
• Date: Sat, 4 Feb 2012 06:32:40 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com

```On 2/3/12 at 2:10 AM, tototo at yopmail.com (droopy) wrote:

>I am a new user of mathematica and i am trying to find the upper
>limit of an integrate such as it is equal to a certain value.

>Therefore, for the moment i did : f = (4^x)*E^-4/Gamma[x + 1];
>Reduce[{Integrate[f, {x, 0, A}] == 0.5}, {A}]

>But it doesn't work, I did the same thing with Solve :
>Solve[Integrate[f, {x, 0, A}] == 0.5, A] but it doesn't work.

>The only way that i found to obtain a solution was with
>FindRoot[Integrate[f, {x, 0, A}] == 0.5, {A, 2}]

Using FindRoot is appropriate. NSolve, Solve and Reduce are not
intended to handle this type of problem But there are a few
things you should change.

Don't use single upper case letters as variables. This ensures
you will not have conflicts with built-in symbols. In this
particular case, you didn't encounter a problem since there
(currently) is no built-in symbol A. But if you get in the habit
of using single upper case letters as variables, you will
encounter problems eventually. Best to not do this.

Using Integrate here is inefficient. This will cause Mathematica
to try and find a symbolic solution to the integral for every
time FindRoot supplies a new guess for the upper limit. That is
you have Mathematica repeatedly trying to do something it cannot do.

The way to set this up is to define a helper function:

g[y_?NumericQ] := NIntegrate[f, {x, 0, y}]

The test ?NumericQ and usage of SetDelayed (:=) ensures
Mathematica will not attempt to evaluate g until a numeric value
is supplied to g.

Compare:

In[10]:= Timing[FindRoot[g[x] - .5, {x, 2}]]

Out[10]= {0.069981,{x->3.86298}}

In[11]:= ClearSystemCache[];
Timing[FindRoot[Integrate[f, {x, 0, a}] - .5, {a, 2}]]

Out[11]= {16.6628,{a->3.86298}}

Both answers are the same but using Mathematica takes more than
200 times as long to get the answer when using Integrate instead
of NIntegrate.

```

• Prev by Date: Cautionary tale : Remember to close StringToStream...
• Next by Date: Re: Funny Behavior of Module
• Previous by thread: Re: Problem using Solve or Nsolve
• Next by thread: Funny Behavior of Module