Newton's Method

We wrote the program newton.c to compute the root of the function

f(x)=xex − 1/5

using Newton's method with starting guess x0=2.

The derivative of f(x) was calculated using the Reduce Computer Algebra System, which we ran by typing some commands into a terminal window as follows:

$ /nfs/home/ejolson/opt/bin/reduce
Reduce (Free PSL version), 19-Sep-2014 ...

1: load_package gentran;

2: gentranlang!* := 'c;

gentranlang* := c

3: f:=x*exp(-x)-1/5;

          x
       - e  + 5*x
f := -------------
            x
         5*e

4: gentran r:=eval(f);
r=(-exp((double)(x))+5.0*x)/(5.0*exp((double)(x)));

t

5: gentran r:=eval(df(f,x));
r=(-x+1.0)/exp((double)(x));

t

6: quit;

Quitting
Note that the output for f(x) and its derivative f '(x) was copied into our C program using the mouse. The derivative is easy to calculate by hand; however, using a computer algebra system to automate the generation of the necessary C code helps eliminate typographic errors that could lead to wrong answers. Further automation is also possible.

The resulting C program produces the output

x=   2.522188780213870e+00
x=   2.542569166682001e+00
x=   2.542641356856976e+00
x=   2.542641357773527e+00
x=   2.542641357773526e+00
x=   2.542641357773527e+00
x=   2.542641357773526e+00
x=   2.542641357773527e+00
x=   2.542641357773526e+00
x=   2.542641357773527e+00
Note that the approximation has converged after 4 itterations of Newton's method.

We can verify our answer by plugging the approximation back into f(x). Here is how to check the approximation by again using reduce.

$ /nfs/home/ejolson/opt/bin/reduce
Reduce (Free PSL version), 19-Sep-2014 ...

1: x:=2.542641357773527;

      2542641357773527
x := ------------------
      1000000000000000

2: evalf(x*exp(-x));

0.2

3: quit;

Quitting

Last Updated: Fri Sep 19 15:08:33 PDT 2014