0% found this document useful (0 votes)
58 views3 pages

Deepak Sela 4/16/2015 Ratnaswamy 1) : While

The document describes an implementation of Newton's method to find the root of a function. It provides code to iteratively calculate the next estimate of the root (x) of a function (f) starting from an initial guess. The code prints the step number, current x value, change in x from previous step (dx), and function value (f) at each iteration. It runs the algorithm twice, first with an initial guess of x=-1 to find the root of f(x)=x^3-2x, and then with an initial guess of x=1. It converges to the correct root in both cases within 7 iterations. The document also discusses testing the algorithm with an initial guess of x=0 and

Uploaded by

Deepak Sela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views3 pages

Deepak Sela 4/16/2015 Ratnaswamy 1) : While

The document describes an implementation of Newton's method to find the root of a function. It provides code to iteratively calculate the next estimate of the root (x) of a function (f) starting from an initial guess. The code prints the step number, current x value, change in x from previous step (dx), and function value (f) at each iteration. It runs the algorithm twice, first with an initial guess of x=-1 to find the root of f(x)=x^3-2x, and then with an initial guess of x=1. It converges to the correct root in both cases within 7 iterations. The document also discusses testing the algorithm with an initial guess of x=0 and

Uploaded by

Deepak Sela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Deepak Sela

4/16/2015
Ratnaswamy
1)
x = 1;
Tol = 0.0000001;
count = 0;
dx=1;
%this is a fake value so that the while loop will execute
f=-1;
% because f(-2)=-13
fprintf('step
x
dx
f(x)\n')
fprintf('---- ----------- ------------------\n')
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
xVec=x;fVec=f;
while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for
this statement to proceed
count = count + 1;
fprime = 3*x^2 - 2;
xnew = x - (f/fprime);
% compute the new value of x
dx=abs(x-xnew);
% compute how much x has changed since last step
x = xnew;
f = x^3 - 2*x;
% compute the new value of f(x)
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
end
x = -1;
Tol = 0.0000001;
count = 0;
dx=1;
%this is a fake value so that the while loop will execute
f=-3;
% because f(-2)=-13
fprintf('step
x
dx
f(x)\n')
fprintf('---- ----------- ------------------\n')
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
xVec=x;fVec=f;
while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for
this statement to proceed
count = count + 1;
fprime = 3*x^2 - 2;
xnew = x - (f/fprime);
% compute the new value of x
dx=abs(x-xnew);
% compute how much x has changed since last step
x = xnew;
f = x^3 - 2*x;
% compute the new value of f(x)
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
end

X = -1
>> newton2
step

dx

---- ----------- ---------

f(x)
----------

0 -1.00000000 1.00000000 -3.00000000


1 2.00000000 3.00000000 4.00000000
2 1.60000000 0.40000000 0.89600000
3 1.44225352 0.15774648 0.11551761
4 1.41501064 0.02724288 0.00319099
5 1.41421424 0.00079640 0.00000269
6 1.41421356 0.00000067 0.00000000
7 1.41421356 0.00000000 0.00000000

X=1
>> newton2
step

dx

---- ----------- ---------

f(x)
----------

0 1.00000000 1.00000000 -1.00000000


1 2.00000000 1.00000000 4.00000000
2 1.60000000 0.40000000 0.89600000
3 1.44225352 0.15774648 0.11551761
4 1.41501064 0.02724288 0.00319099
5 1.41421424 0.00079640 0.00000269
6 1.41421356 0.00000067 0.00000000
7 1.41421356 0.00000000 0.00000000

An initial guess of 0 will invalidate the function, which has no constant, and
that will lead to few iterations. The program only recognizes the numbers that
are inputted for the x and f value; if zero, the program will only return outputs
(as seen below).
X=0
>> newton2
step
x
dx
f(x)
---- ----------- --------- ---------0 0.00000000 1.00000000 0.00000000
1 0.00000000 0.00000000 0.00000000

2)

x = 0;
Tol = 0.0000001;
count = 0;
dx=1;
%this is a fake value so that the while loop will execute
f=1;
% because f(-2)=-13
fprintf('step
x
dx
f(x)\n')
fprintf('---- ----------- ------------------\n')
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
xVec=x;fVec=f;
while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for
this statement to proceed
count = count + 1;
fprime = sin(x) - 3;
xnew = x - (f/fprime);
% compute the new value of x
dx=abs(x-xnew);
% compute how much x has changed since last step
x = xnew;
f = cos(x)-3*x;
% compute the new value of f(x)
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
end

>> newton2
step

dx

---- ----------- ---------

f(x)
----------

0 0.00000000 1.00000000 1.00000000


1 0.33333333 0.33333333 -0.05504305
2 0.31273959 0.02059374 0.01327548
3 0.31767044 0.00493084 -0.00304567
4 0.31653723 0.00113321 0.00070732
5 0.31680029 0.00026307 -0.00016381
6 0.31673937 0.00006093 0.00003796
7 0.31675348 0.00001412 -0.00000880
8 0.31675021 0.00000327 0.00000204
9 0.31675097 0.00000076 -0.00000047
10 0.31675080 0.00000018 0.00000011
11 0.31675084 0.00000004 -0.00000003

8 iterations are enough. Observation of the 9 th iteration shows that x became


a greater value and every value past that is also greater than 8.

You might also like