Math 111: MATLAB Assignment 2

Newton's Method

Due Date: November 30, 2007

The sample program below illustrates how Newton's Method is used to find the root of an equation. Consult the MATLAB TA's if you have any questions.

% Lines beginning with a percent sign (%) are comments

% Find the one real root of x^3 + 3*x + 1 = 0 using Newton's Method

x = 0;

N = 10000; Tol = 0.01;

count = 1;

while count <= N

f = x^3 + 3*x + 1;

Df = 3*x^2 + 3;

xnew = x - (f/Df);

if (abs(xnew - x)) < Tol

break

end

x = xnew;

count = count + 1;

end

 

% Below is a list and description of parameters used in this program

% x We start with an initial guess x = 0

% N is the Maximum number of Iterations you will allow the program to do

% Tol is the Tolerance, the max difference permitted between exact and approximate root

% count is the number of iterations performed

Once you have saved this program, for example as newton.m, typing the filename, newton, at

the prompt in the Command window will run the program. Type xnew and count to print the root and the number of iterations, respectively.

Modify this code appropriately to do the following to hand in:

1.      Find the roots of the equation x^3-2*x = 0. Use initial guesses of x=1. and x=-1. What happens if you give an initial guess of x=0? Explain.

2.      Find the root of the equation cos(x)-3*x = 0. How many iterations are required to obtain accuracy of 6 decimal places?