Stuck at coding newton-Rapson method

3 ビュー (過去 30 日間)
seoung gi Sin
seoung gi Sin 2022 年 4 月 10 日
編集済み: Stephen23 2022 年 4 月 10 日
I inputed the number exp(-x) into an input and 3 into x0
but the Matlab says that Index exceeds the number of array elements, index must not exceed 1
Could you solve this problem for me, Thanks in advance.
clear all
clc
syms x;
fun=input('Enter the function as a variable of x');
f=inline(fun);
f2=diff(f(x));
x0=input('Enter the initial value of interval');
z=[];
error=10^-8;
tol=inf;
i=0;
z(1)=x0;
while tol>error
i=i+1;
z(i+1)=z(i)-(f(z(i))/f2(z(i)));
error=(z(i+1)-z(i))/z(i+1);
end
  1 件のコメント
Stephen23
Stephen23 2022 年 4 月 10 日
編集済み: Stephen23 2022 年 4 月 10 日
Why learn how to code using deprecated functions? https://www.mathworks.com/help/matlab/ref/inline.html

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

回答 (1 件)

KSSV
KSSV 2022 年 4 月 10 日
Replace this line:
f2=diff(f(x));
with
f2=inline(diff(f(x)));
  1 件のコメント
seoung gi Sin
seoung gi Sin 2022 年 4 月 10 日
Thank you. It worked

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

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by