I am new in MATLAB and trying to write this code on NEWTON-RAPHSON. Please help me to rectify the error in this code. I have shared the program with the error signal below. Its urgent. Any help will be highly appreciated.

2 ビュー (過去 30 日間)
syms x
f= cos(3*x) - x
x(1)=input('Enter your guess:');
tol=input('Enter the tolerance:');
fdash=diff((f));
for i=1:100
sym x(i+1)=x(i)-(f(x(i))/fdash(x(i)));
if abs((x(i+1)-x(i)))<tol;
break
end
end
root=x(i).
The error I get is:
??? Error using ==> mupadmex Error in MuPAD command: Index exceeds matrix dimensions.
Error in ==> sym.sym>sym.subsref at 1366 B = mupadmex('mllib::subsref',A.s,inds{:});
Error in ==> newtonraphson2 at 8 if abs((x(i+1)-x(i)))<tol;_ * * *

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 7 月 25 日
編集済み: Geoff Hayes 2014 年 7 月 25 日
Why do you have a sym at the line
sym x(i+1)=x(i)-(f(x(i))/fdash(x(i)));
Shouldn't this just be an evaluation of the statement
x(i+1)=x(i)-(f(x(i))/fdash(x(i)));
instead?
The error message is telling you that there is a problem at the line
abs((x(i+1)-x(i)))<tol
As i, on the first iteration, is 1, then x(i) should be valid. Since the error message is index exceeds matrix dimension then that suggests that x(2) or x(i+1) is the problem.
EDIT
I don't have the Symbolic Toolbox so re-wrote the code as
f = @(x)cos(3*x) - x;
x(1) = input('Enter your guess:')
tol = input('Enter the tolerance:');
fdash=@(x)3*(-sin(3*x)) - 1;
for k=1:100
x(k+1)=x(k)-(f(x(k))/fdash(x(k)));
if abs((x(k+1)-x(k)))<tol;
break
end
end
  2 件のコメント
Tuhin
Tuhin 2014 年 7 月 25 日
Thank you Geoff for replying. Now it is working. It was a great help for me.. Thanks again.
Geoff Hayes
Geoff Hayes 2014 年 7 月 25 日
Glad to have been able to help, Tuhin!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by