Finding roots using Newton raphson method

58 ビュー (過去 30 日間)
R Abhinandan
R Abhinandan 2020 年 10 月 27 日
コメント済み: R Abhinandan 2020 年 10 月 27 日
Here is my code and my output is a function and not a numerical value as I expected. Can anyone debug this code?
syms f y x df
f=@(y) exp(y)-(sin(pi*y/3));
df=@(y) exp(y)-((pi*cos(pi*y/3))/3);
x(1)=-3.0;
error=0.00001;
for i=1:10
x(i+1)=x(i)-((f(x(i)))/df(x(i)));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i);
output:
- (sin((pi*(exp(-3)/(pi/3 + exp(-3)) + 3))/3) + exp(- exp(-3)/(pi/3 + exp(-3)) - 3))/(exp(- exp(-3)/(pi/3 + exp(-3)) - 3) - (pi*cos((pi*(exp(-3)/(pi/3 + exp(-3)) + 3))/3))/3) - exp(-3)/(pi/3 + exp(-3)) - 3

採用された回答

Alan Stevens
Alan Stevens 2020 年 10 月 27 日
Just get rid of the first line, you don't need it
f=@(y) exp(y)-(sin(pi*y/3));
df=@(y) exp(y)-((pi*cos(pi*y/3))/3);
x(1)=-3.0;
error=0.00001;
for i=1:10
x(i+1)=x(i)-((f(x(i)))/df(x(i)));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i);
disp(root)
  3 件のコメント
Alan Stevens
Alan Stevens 2020 年 10 月 27 日
This is what I get:
>> f=@(y) exp(y)-(sin(pi*y/3));
df=@(y) exp(y)-((pi*cos(pi*y/3))/3);
x(1)=-3.0;
error=0.00001;
for i=1:10
x(i+1)=x(i)-((f(x(i)))/df(x(i)));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i);
disp(root)
-3.0454
R Abhinandan
R Abhinandan 2020 年 10 月 27 日
Thank you, im sorry i didnt know that we have to save before running the code.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNewton-Raphson Method についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by