Message Boards Message Boards

0
|
16808 Views
|
7 Replies
|
5 Total Likes
View groups...
Share
Share this post:

FindRoot does not work

Posted 11 years ago
Dear:

In the example bellow the command FindRoot should result in {h->1.61589} but as you can see Mathematica could not solve the equation. Could you help me please?
In[302]:= x[y_] = Sqrt[16 - y^2];

A[h_] := 2 Integrate[x[y], {y, 0, h}];
In[316]:= FindRoot[A[h] == 4 \[Pi], {h, 1.5}]
During evaluation of In[316]:= FindRoot::nlnum: The function value {True} is not a list of numbers with dimensions {1} at {h} = {1.5}. >>
Out[316]= FindRoot[A[h] == 4 \[Pi], {h, 1.5}]
Thank s,
Ana
POSTED BY: Ana Squadri
7 Replies
Using SetDelayed is likely to cause an unnecessary slowdown. Adding assumptions for h such that it is a real number and so is x, allows one not to worry about Integrate returning ConditionalExpression or _?NumericQ.
In[1]:= x[y_] = Sqrt[16 - y^2];
A[h_] = 2 Integrate[x[y], {y, 0, h}, Assumptions -> 0 < h < 4];
AbsoluteTiming[FindRoot[A[h] == 4 Pi, {h, 1.5}]]

Out[3]= {0.003530, {h -> 1.61589}}
Incidentally, the ? in ?NumericQ parses to Information, what the tech support article really means is _?NumericQ or PatternTest[Blank[],NumericQ]
POSTED BY: Ilian Gachevski
Please see this article about why ?NumericQ is often used when working with numerical function in Mathematica:

http://support.wolfram.com/kb/3820
POSTED BY: Sean Clarke
Hello Ana,
In addition to the response to your previous post, many of the numerical functions like FindRoot need to have arguments that are numerical.

Here is an example:
Clear[x, y, A, h]
x[y_] := Sqrt[16 - y^2]
A[h_?NumericQ] := 2 Integrate[x[y], {y, 0, h}]
FindRoot[A[h], {h, 1.2}]

FindRoot[A[h] + 1 - h^2, {h, 1.2}]
Plot[A[h] + 1 - h^2, {h, -1, 1}]
POSTED BY: W. Craig Carter
Posted 11 years ago
 In[422]:= x[y_] = Sqrt[16 - y^2];
 
 A[h_] = 2 NIntegrate[x[y], {y, 0, h}];
 In[421]:= FindRoot[A[h], {h, 1.6}]
 Out[421]= {h -> 1.6}
 
 
 In[408]:= FindRoot[2 NIntegrate[Sqrt[16 - y^2], {y, 0, h}] == 4 \[Pi], {h, 1.6}]
 Out[408]= {h -> 1.61589}
Please one more doubt: why does the second solution above work and the first not? It seems for me that they are absolutely equivalents. Or not?
Thanks,
Ana
POSTED BY: Ana Squadri
Posted 11 years ago
Dear Paul T:

I thank you very much. It was a complete answer and help me a lot. Above I show the final result with Off.
In[403]:= Off[NIntegrate::nlim]

In[406]:= FindRoot[2 NIntegrate[Sqrt[16 - y^2], {y, 0, h}] == 4 \[Pi], {h, 1.6, 1.7}, Method -> "Brent"]
Out[406]= {h -> 1.61589}
POSTED BY: Ana Squadri
Posted 11 years ago
I tried moving away from something so purely symbolic as in the formulation of your problem. That is why you will see that I moved away from Integrate to NIntegrate when I reformulated your problem.

I also moved away from the default method which is Newton. You can read where they mention Newton, Secant, and Brent as methods in the FindRoot documents.

Sometimes it's better to bracket a root than try to hit it right off the bat, therefore I used a table to inspect the region. I then tried the two bracketing methods rather than using Newton. As you can see, Brent is a bit faster. The AbsoluteTiming result is system dependent though. Your computer will probably be faster than mine, which is several years old. I upgraded it, but it's still slow.

Finally, when I set about proving that the h value gave 4 ?, I didn't want it to end up with zero because sometimes that results in more distracting messages about the result of the integration being zero. Therefore I let it be a value called a and then I subracted 4 ? from a.
 In[9]:= Table[{h, 2 \!\(
 \*SubsuperscriptBox[\(\[Integral]\), \(0\), \(h\)]\(\((
 \*SqrtBox[\(16 -
 \*SuperscriptBox[\(y\), \(2\)]\)])\) \[DifferentialD]y\)\) -
    4 \[Pi]}, {h, 1, 2, .05}]
 
 Out[9]= {{1., -4.6505}, {1.05, -4.26386}, {1.1, -3.87858}, {1.15, \
 -3.49473}, {1.2, -3.11238}, {1.25, -2.7316}, {1.3, -2.35247}, {1.35, \
 -1.97506}, {1.4, -1.59943}, {1.45, -1.22568}, {1.5, -0.853873}, \
{1.55, -0.484088}, {1.6, -0.116404}, {1.65, 0.249097}, {1.7,
  0.612334}, {1.75, 0.973224}, {1.8, 1.33168}, {1.85, 1.68762}, {1.9,
  2.04095}, {1.95, 2.39158}, {2., 2.73941}}

In[18]:= AbsoluteTiming[
ruelSecant =
  FindRoot[2 NIntegrate[Sqrt[16 - y^2], {y, 0, h}] == 4 \[Pi], {h,
    1.6, 1.7}, Method -> "Secant"]]

During evaluation of In[18]:= NIntegrate::nlim: y = h is not a valid limit of integration. >>

Out[18]= {0.312500, {h -> 1.61589}}

In[19]:= AbsoluteTiming[
ruelBrent =
  FindRoot[2 NIntegrate[Sqrt[16 - y^2], {y, 0, h}] == 4 \[Pi], {h,
    1.6, 1.7}, Method -> "Brent"]]

During evaluation of In[19]:= NIntegrate::nlim: y = h is not a valid limit of integration. >>

Out[19]= {0.281250, {h -> 1.61589}}

a = (2 \!\(
\*SubsuperscriptBox[\(\[Integral]\), \(0\), \(h\)]\(\((
\*SqrtBox[\(16 -
\*SuperscriptBox[\(y\), \(2\)]\)])\) \[DifferentialD]y\)\)) /.
  ruelBrent

Out[14]= 12.5664

In[15]:= a - 4 \[Pi]

Out[15]= -1.42109*10^-14
If the function or equation you are attempting to find a root for is well behaved, then you can get away with poor starting values, single guesstimates, and methods like Newton, but otherwise, if your function or equation is not so well behaved, you may have to help Mathematica out a bit. Mathematica is very very powerful, but it's still up to you to use your mind. You can't expect Mathematica to do it all.

Incidently, even when I got it to settle down and just compute, it sitll gave me warning messages about not wanting to have h as a limit of integration. Those are the sort of messages that may be switched off by a function with the name of Off as in Off[ ].

For your research, look up FindRoot and read about methods under Option. There is another section towards the top of the FindRoot page called Details and Options; read that too.

Look up Integrate and NIntegrate and read about those.

Also, for your vocabulary, general information, and possible future use, look up the Off function and read about switching off annoying messages. I didn't use Off in my code, but if I was doing this for myself I would have.
POSTED BY: Paul T
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract