Setting a variable equal to the output of FindRoot



I define a function $f(x)$ as

f[x_] := x*Exp[-x] - 0.16064;

Then I set a variable actualroot to the function FindRoot, starting at $x=3$

actualroot = FindRoot[f[x], {x, 3}]

and get the output

{x -> 2.88976}

Later, I want to compare this output with a different estimate ($x\approx -2.88673$) of the root, and calculate error, so I have

Abs[(actualroot - estimateroot)/actualroot]

and I get this output:

Abs[(-2.88673 + (x -> 2.88976))/(x -> 2.88976)]

How do I get Mathematica to evaluate this expression? I also tried using N[] function to give me a decimal evaluation, but it didn't work.


Posted 2012-07-13T20:05:52.203

Reputation: 103

Question was closed 2016-08-18T13:37:02.083

2You can use actualroot = FindRoot[f[x], {x, 3}][[1, 2]] – – 2012-07-13T20:27:39.393



The usual way to get the values of the results of FindRoot, Solve, etc., which are lists of Rule is the following:

f[x_] := x E^(-x) - 0.16064
actualroot = x /. FindRoot[f[x], {x, 3}]
estimateroot = -2.88673;
Abs[(actualroot - estimateroot)/actualroot]



Peter Breitfeld

Posted 2012-07-13T20:05:52.203

Reputation: 4 892

Thanks, new to mathematica, just diving in – DWC – 2012-07-13T21:05:57.700


@DWC well, then it's probably good to know that /. is shorthand for ReplaceAll. Apart from its doc page reading this tutorial on transformation rules will prove fruitful.

– Sjoerd C. de Vries – 2012-07-13T21:51:01.380