0
$\begingroup$

I am writing a procedure that simulates the findRoot built-in function using the same code that appears at chapter 6 of $\textit{Programming in mathematica}$ by Paul Wellin. The code is the following.

findRoot[expr_, {var_, init_}, \[Epsilon]_] :=
 Module[{xi = init, fun = Function[fvar, expr]},
  While[Abs[fun[xi]] > \[Epsilon],
   xi = N[xi - fun[xi]/fun'[xi]]];
  {var -> xi}]

In the text, the code is tested with success, but when I test it it doesn't works. For example,

In[5]:= findRoot[x^2 - 2, {x, 2.0}, 0.0001]

Out[5]= {x -> 2.}

I tried to change the variable fvar by var, and the program works, but this is a reuse of names that makes mathematica color the variable var in red.

findRoot[expr_ == val_, {var_, init_}, \[Epsilon]_] :=
 Module[{xi = init, fun = Function[var, expr - val]},
  While[Abs[fun[xi]] > \[Epsilon],
   xi = N[xi - fun[xi]/fun'[xi]]];
  {var -> xi}]

I need some suggestion to clear this issue.

$\endgroup$
  • $\begingroup$ Ignore the red coloring of var. It is a warning not an error message. $\endgroup$ – Bob Hanlon Apr 3 at 20:21
  • $\begingroup$ @Bob Hanlon That is one possibility, but not yet is clear to me why the code works for the author of the text. $\endgroup$ – DIEGO R. Apr 3 at 20:24
  • 1
    $\begingroup$ I suspect that it is a typo that was not caught in the proofing. $\endgroup$ – Bob Hanlon Apr 3 at 20:26
  • $\begingroup$ @BobHanlon Thanks. $\endgroup$ – DIEGO R. Apr 3 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.