Re: Problem with Mathematica driving me nuts
- To: mathgroup at smc.vnet.net
- Subject: [mg46820] Re: Problem with Mathematica driving me nuts
- From: Jon Harrop <jdh30 at cam.ac.uk>
- Date: Tue, 9 Mar 2004 04:31:09 -0500 (EST)
- Organization: University of Cambridge
- References: <c2he1m$ahd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
benwoodward.com wrote: > FindRoot[x^2 == 4x - 4, {x, 1}] > > Out[4]= > {x -> 1.99902} > ... > When the root is clearly two. > ... Yes. By using FindRoot you are asking Mathematica to find the root numerically, rather than symbolically. In this case (it's an easy solution) you probably want to do it symbolically using: In[1] = Solve[x^2 == 4x - 4, x] Out[1] = {{x -> 2},{x -> 2}} which tells you that there is a repeated root at x=2. The format looks strange because Mathematica is giving you substitution rules. These are often useful if you want to do other, automated things with your solutions. In this case, for example, we can check the answer by applying the rules to substitute "2" and "2" back in for "x" in the equation, and we find: In[2] = x^2 == 4x - 4 /. % Out[2] = {True, True} as one would hope, given that these are the solutions of the equation. :) If you really want to do it numerically then you need to tell the FindRoot function to use higher precision itself, rather than using N[...] to change the precision of the answer. You can do this by changing the default working precision. Changing this alone gives: In[3] = FindRoot[x^2 == 4x - 4, {x, 1}, WorkingPrecision->100] FindRoot::cvnwt : Newton's method failed to converge to the prescribed accuracy after 15 iterations. Out[3] = {x -> 1.99998} which is better, but still no sweet potato. You obviously want to increase the maximum number of iterations: In[4] = FindRoot[x^2 == 4x - 4, {x, 1}, MaxIterations->200, WorkingPrecision->100] Out[4] = {x -> 1.99999999999999999999999999999999999999999999929935076783759 1464538135208355041934359869029061742114} There, that's better. A quick reference for the options accepted by most functions is: In[5] = Options[FindRoot] Out[5] = {AccuracyGoal -> Automatic, Compiled -> True, DampingFactor -> 1, Jacobian -> Automatic, MaxIterations -> 15, WorkingPrecision -> 16} If you're going to be making lots of calls to FindRoot then you can set the options globally using SetOptions[]. Cheers, Jon.