Find the Root of the Equations Newton Method
3 views (last 30 days)
Show older comments
% 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
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
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')
fprintf('---- ----------- --------- ----------\n')
fprintf('%3i %45s %12.8f %45s\n', count, mat2str(x), dx, mat2str(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 %45s %12.8f %45s\n', count, mat2str(x), dx, mat2str(f))
end
Answers (0)
See Also
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!