I need to change this code into one that can solve complex roots
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Elena Cecilia Valero Garza
le 12 Sep 2022
Commenté : Torsten
le 13 Sep 2022
I have this Newton-Raphson method code, but I need to change it into a code that can actually solve polynomials and give as an answer complex roots.
Code:
function [tabla, raiz]=newtonraphsonMN(f,xa,errorD,imax)
tabla=[];
error=Inf;
i=0;
xr=NaN;
df=diff(f);
while error>errorD && i<=imax
i=i+1;
fxa=double(subs(f,xa));
fpxa=double(subs(df,xa));
xr=xa-fxa/fpxa;
error=100*abs((xr-xa)/xr);
tabla=[tabla; [xa fxa fpxa xr error]];
xa=xr;
end
%raiz
raiz=xr;
0 commentaires
Réponse acceptée
John D'Errico
le 12 Sep 2022
Modifié(e) : John D'Errico
le 13 Sep 2022
Interesting. It does not work? :) Gosh, You could have fooled me. I'll try an example. (I've attached the code you gave, so it will be used.)
syms X
f = X^2 + X + 1;
Does f has complex roots?
solve(f)
vpa(ans)
Of course. I'd not have used an example that lacks complex roots, since that is your question.
[tabla, raiz]=newtonraphsonMN(f,1 + i,1e-12,100);
Strange.
tabla
format long
raiz
To me, it seems to have worked. But then, what do I know? :)
(Hint: Do you see what I did different?)
2 commentaires
Torsten
le 13 Sep 2022
To solve for polynomial roots, use MATLAB's "roots" command.
Newton's method will only give you one solution per call. Which one you get depends on the initial guess.
Example:
To determine all roots of p(x) = x^2+x+1:
p = [1 1 1];
sol = roots(p)
polyval(p,sol)
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!
