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.

조회 수: 1 (최근 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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by