Find the Root of the Equations Newton Method

3 views (last 30 days)
% Find the roots of the equation
%Use initial guesses of x=1. and x=-1. What happens if you give an initial guess of x=0? Explain.
% Find the root of the equation
%starting from the initial condition x=0.
%How many iterations are required to obtain accuracy of 6 decimal places?
% Below is what I have so far
x = -2;
Tol = 0.0000001;
count = 0;
dx=1; %this is a fake value so that the while loop will execute
f=-13; % 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 + 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 = x^3 + 3*x + 1; % compute the new value of f(x)
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
end
% This produces the following output:
step x dx f(x)
---- ----------- --------- ----------
0 -2.00000000 1.00000000 -13.00000000
1 -1.13333333 0.86666667 -3.85570370
2 -0.57073065 0.56260268 -0.89809804
3 -0.34491904 0.22581161 -0.07579185
4 -0.32234116 0.02257788 -0.00051597
5 -0.32218536 0.00015580 -0.00000002
6 -0.32218535 0.00000001 0.00000000
  3 Comments
SAPAN NAYAK
SAPAN NAYAK on 4 Apr 2021
Hello Sir, My question is how to get rhe complex roots of this equation like =4.1185e-07+i4.0939e-06
Walter Roberson
Walter Roberson on 4 Apr 2021
Edited: Walter Roberson on 4 Apr 2021
I have marked the only real difference. The other changes do not affect the computation, and were only made because fprintf() normally ignores the imaginary portion of values.
% Find the roots of the equation
%Use initial guesses of x=1. and x=-1. What happens if you give an initial guess of x=0? Explain.
% Find the root of the equation
%starting from the initial condition x=0.
%How many iterations are required to obtain accuracy of 6 decimal places?
% Below is what I have so far
x = -2 + pi*1j; %<---- THIS IS THE ONLY REAL DIFFERENCE
Tol = 0.0000001;
count = 0;
dx=1; %this is a fake value so that the while loop will execute
f=-13; % because f(-2)=-13
fprintf('step x dx f(x)\n')
step x dx f(x)
fprintf('---- ----------- --------- ----------\n')
---- ----------- --------- ----------
fprintf('%3i %45s %12.8f %45s\n', count, mat2str(x), dx, mat2str(f))
0 -2+3.14159265358979i 1.00000000 -13
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 + 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 = x^3 + 3*x + 1; % compute the new value of f(x)
fprintf('%3i %45s %12.8f %45s\n', count, mat2str(x), dx, mat2str(f))
end
1 -2.11618125003165+3.44140687381899i 0.32153794 60.362313822926+15.8008341471295i 2 -1.30567192182889+2.41418111503511i 1.30847932 17.686529205077+5.51897989055528i 3 -0.699361801093534+1.77941870614676i 0.87780139 5.20308460398037+2.31500568396276i 4 -0.1632106840138+1.44388453515715i 0.63248810 1.5268065373346+1.43682485125081i 5 0.406353487669999+1.6428115912685i 0.60330367 -1.00387802350351+1.30856439976018i 6 0.141387836351897+1.69655460025i 0.27036107 0.206121152824967+0.308219943874544i 7 0.162669423670935+1.75733557103833i 0.06439901 -0.0147682355158745-0.0155423767087743i 8 0.161099322766261+1.75438738854152i 0.00334021 -5.18091918002561e-05-2.83047357569899e-05i 9 0.161092677383075+1.75438095979388i 0.00000925 -4.48330705893341e-10+5.62003776849451e-11i 10 0.161092677313043+1.75438095978372i 0.00000000 -2.22044604925031e-16

Sign in to comment.

Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!