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 Ansicht (letzte 30 Tage)
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;_ * * *

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 25 Jul. 2014
Bearbeitet: Geoff Hayes am 25 Jul. 2014
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

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by