Newton's Method Help

5 views (last 30 days)
Nicholas Fuller
Nicholas Fuller on 15 Sep 2020
Commented: David Hill on 15 Sep 2020
For my Numerical Analysis class we are using Newton's Method to find the roots of a given function. The function given was "x = 2*sin(x)", and the answer we were given was "1.8954942670340", but my code returns -1.4014 after 7 iterations in the loop. For the variable "functn" I subtracted x in the orignal equation to get "2*sin(x) - x = 0". The tolerance we needed for the roots was 1e^-6. There is certainly an issue in the code, I just can't seem to find it. Thanks for reading, and looking this over.
clear all
close all
clc
functn = @(x) 2*sin(x) - x
interval = 0.001; %small x-step used to approximate the function derivative
x = 0.01:0.01:1;
dydx = (functn(x+interval) - functn(x))/interval;
Root_value = -0.57; % Function value
ytolerance = 1e-6; % The convergence tolerance
initial_guess = 0.6; % The initial guess for the location of the root x0
% set the starting function value to the initial guess, and compute the initial error
x=initial_guess;
yerror = Root_value-functn(x);
check=1;
while abs(yerror)>ytolerance
dydx = (functn(x+interval)-functn(x))/interval;
dx = yerror/dydx;
x=x+dx;
disp("x"+check+" = "+x);
disp(" ");
yerror = Root_value - functn(x);
check=check+1;
end

Accepted Answer

David Hill
David Hill on 15 Sep 2020
f = @(x) 2*sin(x) - x;
fp= @(x) 2*cos(x) - 1;
tol=1e-10;
yerror=1;
g=1.2;%guess needs to be close enough to the root you are seeking
while abs(yerror)>tol
yerror=f(g)/fp(g);
g=g-yerror;
end
  1 Comment
David Hill
David Hill on 15 Sep 2020
If you estimate fp:
f = @(x) 2*sin(x) - x;
tol=1e-10;
yerror=1;
g=1.2;
while abs(yerror)>tol
yerror=f(g)/((f(g+.001)-f(g))/.001);
g=g-yerror;
end

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!