ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 08 Oct 2012 11:57:09 +0200Problem with find_roothttps://ask.sagemath.org/question/9399/problem-with-find_root/Hello,
I'm running into a problem with "find_root." To give an example, when I use the following code, "find_root" returns a value that is not a root:
-----------------
sage: var('q')
sage: h = ((q+.2*(40 - q))^-0.5)/((40-q+.2*(q))^-0.5) == ((q+.2*(40 - q))^.5-(50)^.5+(50-8)^.5)/((40-q+.2*(q))^0.5-(50)^.5+(50-32)^.5)
sage: h_a = find_root(h, 0, 40)
sage: print h_a
sage: #h_a = 11.76222222321
sage: t = ((h_a+.2*(40 - h_a))^-0.5)/((40-h_a+.2*(h_a))^-0.5)
sage: print t
sage: z = ((h_a+.2*(40 - h_a))^.5-(50)^.5+(50-8)^.5)/((40-h_a+.2*(h_a))^0.5-(50)^.5+(50-32)^.5)
sage: print z
40.0
0.447213595499989
3.56694309609917e13
-----------------
As you can see, sage returns a root of 40, which is not a root (to check this, note that t does not equal z). My question is why find_root is doing this. There are several ways in which I could fix the problem "locally;" e.g. setting the bounds between 0 and 30 returns the correct root of 11.76, but I need to eliminate the error to apply my code to a more general problem. Please let me know if I am doing something wrong here!
Thanks!Mon, 08 Oct 2012 08:28:08 +0200https://ask.sagemath.org/question/9399/problem-with-find_root/Answer by achrzesz for <p>Hello,
I'm running into a problem with "find_root." To give an example, when I use the following code, "find_root" returns a value that is not a root:</p>
<hr/>
<p>sage: var('q')</p>
<p>sage: h = ((q+.2<em>(40 - q))^-0.5)/((40-q+.2</em>(q))^-0.5) == ((q+.2<em>(40 - q))^.5-(50)^.5+(50-8)^.5)/((40-q+.2</em>(q))^0.5-(50)^.5+(50-32)^.5)</p>
<p>sage: h_a = find_root(h, 0, 40)</p>
<p>sage: print h_a</p>
<p>sage: #h_a = 11.76222222321</p>
<p>sage: t = ((h_a+.2<em>(40 - h_a))^-0.5)/((40-h_a+.2</em>(h_a))^-0.5)</p>
<p>sage: print t</p>
<p>sage: z = ((h_a+.2<em>(40 - h_a))^.5-(50)^.5+(50-8)^.5)/((40-h_a+.2</em>(h_a))^0.5-(50)^.5+(50-32)^.5)</p>
<p>sage: print z</p>
<p>40.0</p>
<p>0.447213595499989</p>
<h2>3.56694309609917e13</h2>
<p>As you can see, sage returns a root of 40, which is not a root (to check this, note that t does not equal z). My question is why find_root is doing this. There are several ways in which I could fix the problem "locally;" e.g. setting the bounds between 0 and 30 returns the correct root of 11.76, but I need to eliminate the error to apply my code to a more general problem. Please let me know if I am doing something wrong here!</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/9399/problem-with-find_root/?answer=14115#post-id-14115You can't include 40 into interval in this case
plot(h,(q,0,45),ymin=-50,ymax=50)Mon, 08 Oct 2012 09:16:46 +0200https://ask.sagemath.org/question/9399/problem-with-find_root/?answer=14115#post-id-14115Comment by mattias for <p>You can't include 40 into interval in this case</p>
<pre><code>plot(h,(q,0,45),ymin=-50,ymax=50)
</code></pre>
https://ask.sagemath.org/question/9399/problem-with-find_root/?comment=18916#post-id-18916Thank you achrzesz. right, is there any way to get find_root to return the root over the interval (0,40) instead of returning the `discontinuity'? To clarify, the problem is that it does return a value, but not the correct one (or an error message).Mon, 08 Oct 2012 10:00:21 +0200https://ask.sagemath.org/question/9399/problem-with-find_root/?comment=18916#post-id-18916Answer by achrzesz for <p>Hello,
I'm running into a problem with "find_root." To give an example, when I use the following code, "find_root" returns a value that is not a root:</p>
<hr/>
<p>sage: var('q')</p>
<p>sage: h = ((q+.2<em>(40 - q))^-0.5)/((40-q+.2</em>(q))^-0.5) == ((q+.2<em>(40 - q))^.5-(50)^.5+(50-8)^.5)/((40-q+.2</em>(q))^0.5-(50)^.5+(50-32)^.5)</p>
<p>sage: h_a = find_root(h, 0, 40)</p>
<p>sage: print h_a</p>
<p>sage: #h_a = 11.76222222321</p>
<p>sage: t = ((h_a+.2<em>(40 - h_a))^-0.5)/((40-h_a+.2</em>(h_a))^-0.5)</p>
<p>sage: print t</p>
<p>sage: z = ((h_a+.2<em>(40 - h_a))^.5-(50)^.5+(50-8)^.5)/((40-h_a+.2</em>(h_a))^0.5-(50)^.5+(50-32)^.5)</p>
<p>sage: print z</p>
<p>40.0</p>
<p>0.447213595499989</p>
<h2>3.56694309609917e13</h2>
<p>As you can see, sage returns a root of 40, which is not a root (to check this, note that t does not equal z). My question is why find_root is doing this. There are several ways in which I could fix the problem "locally;" e.g. setting the bounds between 0 and 30 returns the correct root of 11.76, but I need to eliminate the error to apply my code to a more general problem. Please let me know if I am doing something wrong here!</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/9399/problem-with-find_root/?answer=14005#post-id-14005Mpmath can give you the error message
f=lambda q:((q+.2*(40. - q))^-0.5)/((40.-q+.2*(q))^-0.5) -( ((q+.2*(40. - q))^.5-(50.)^.5+(50.-8.)^.5)/((40.-q+.2*(q))^0.5-(50.)^.5+(50.-32.)^.5))
from mpmath import *
mp.dps = 15; mp.pretty = True
s1=findroot(f,11)
print s1 # no error message
s1=findroot(f,40) # error messageMon, 08 Oct 2012 11:57:09 +0200https://ask.sagemath.org/question/9399/problem-with-find_root/?answer=14005#post-id-14005