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 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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;_ * * *
0 commentaires
Réponse acceptée
Geoff Hayes
le 25 Juil 2014
Modifié(e) : Geoff Hayes
le 25 Juil 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
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!