Newton's Method Matlab

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Newton’s Method

Tutorial 5

Use MATLAB and Newton’s Method to find the root to at least five decimal places of accuracy. You can
write your own program or search code online and modify it.
4 2
x −3 x +1=0 , x 0 =−1

Solution
We start our computational process by choosing an initial approximation. Let our first guess be
x 0=−1

f ( x0 ) 1 1
x 1=x 0 − =−1− =−
f '( x 0 ) 2 2

f (x )=x 4 −3 x 2 +1
f (x )=(−1) 4 −3(−1)2 +1=−1

f ' (x )=4 x3−6 x


f ' (x )=4(−1)3 −6(−1)=2
f ( x1 ) 1 1
x 2=x 1 − =−1− =−
f ' ( x1 ) 2 2

f (x )=x 4 −3 x 2 +1
1 4 1 2
( ) ( )
f (x )= − −3 − +1=0 . 3125
2 2
3
f ' (x )=4 x −6 x
3
1 1
2 ( ) ( )
f ' (x )=4 − −6 − =2 .5
2
f ( x2 ) 1 0 . 3125 1
x 3=x 2 − ( )
f '( x 2 )
=− −
2 2.5
=− =−0 . 625
2
f (x )=x 4 −3 x 2 +1
1 4 1 2
( ) ( )
f (x )= − −3 − +1=0 . 3125
2 2
3
f ' (x )=4 x −6 x
3
1 1
( ) ( )
f ' (x )=4 − −6 − =2 .5
2 2
f ( x3)
x 4 =x 3 −
f ' ( x 3) ( 58 )−−0.2.019287109
=−
7734375
5
=− +(0 . 006954225)=−0 .618045775
8

f (x )=x 4 −3 x 2 +1
f (x )=(−0 . 625 )4−3 (−0 . 625 )2 +1=−0 . 019287109
f ' (x )=4 x3 −6 x
3
f ' (x )=4 (−0 .625 ) −6 (−0. 625 ) =2. 7734375

f ( x4 ) −0 . 000032576
x 5=x 4 − =(−0 . 618045775 )− =(−0 .618045775 )−(−0 . 000011786 )=−0. 618033989
f ' ( x4 ) 2 .763948716
f (x )=x 4 −3 x 2 +1
f (x )=(−0 . 618045775 )4 −3 (−0 . 618045775 )2 +1=−0. 000032576
f ' (x )=4 x3 −6 x
3
f ' (x )=4 (−0 .618045775 ) −6 (−0. 618045775 )=2 . 763948716
%Program approximates (finds aroot) of polynomial function
%with the given accuracy
x = -1; %choose initial approximation
Tol = 0.00001; %choose the accuracy (here it is five decimal places)
count = -1; %set the intial count
dx=1; %this is a fake value so that the while loop will execute
f=-.5; %set the initial function value f(-1)=-.5
fprintf('step x dx f(x)\n')
fprintf('---- ---------- --------- ----------\n')
fprintf('%31 %12.8f %12.8f %12.8\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 = 4*x^3-6*x;
xnew = x - (f/fprime); % compute the new value of x
dx=abs(x-xnew);
x = xnew;
f = x^4 -3*x^2 +1; % compute the new value of f(x)
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
end

This is a Classroom License for instructional use only.

Research and commercial use is prohibited.

step x dx f(x)

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

0 2.36000000 0.64000000 3.42425600

1 2.12719678 0.23280322 0.37109985

2 2.09513604 0.03206074 0.00652663

3 2.09455167 0.00058436 0.00000215

4 2.09455148 0.00000019 0.00000000

step x dx f(x)

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


0 -0.75000000 0.25000000 -0.37109375

1 -0.61805556 0.13194444 -0.00005961

2 -0.61803399 0.00002157 -0.00000000

3 -0.61803399 0.00000000 0.00000000

You might also like