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

 採用された回答

David Hill
David Hill 2020 年 9 月 15 日

0 投票

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 件のコメント

David Hill
David Hill 2020 年 9 月 15 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics and Optimization についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by