Detecting an error in Muller method.

24 visualizaciones (últimos 30 días)
llucia
llucia el 7 de Abr. de 2023
Comentada: Torsten el 7 de Abr. de 2023
Hi! I am programming Muller method as I wan to find the complex roots of an ecuation.
However, it does not work properly and I cannot detect why.
If someone could help me, I would really appreciate it.
Thanks in advance.
Here is the code:
%Muller, mejora del método de la secante, nos permite sacar tanto reales
%como complejas.
f = @(x) x.^3 + 2*x.^2 + 10*x -20;
x0 = -1;
x1 = 0;
x2 = 1;
eps_x = 10e-10;
eps_f = 10e-10;
maxits = 100;
[x0,x1,x2,n,error] = muller(f,x0,x1,x2,eps_x, eps_f,maxits)
function [x0,x1,x2, n, error] = muller(f, x0, x1, x2, eps_x, eps_f, maxits)
x0 = -1;
x1 = 0;
x2 = 1;
n = 0;
error(1) = eps_x + 1;
while n < maxits && (error(n+1) > eps_x || abs(f(x0)) > eps_f)
c = f(x2);
b = ((x0 - x2)^2*(f(x1)-f(x2))-(x1-x2)^2*(f(x0)-f(x2)))/((x0-x2)*(x1-x2)*(x0-x1));
a = ((x1-x2)*(f(x0)-f(x2))-(x0-x2)*(f(x1)-f(x2)))/((x0-x2)*(x1-x2)*(x0-x1));
x3 = x2 - (2*c)/(b+sign(b)*sqrt(b*b-4*a*c));
x0 = x1;
x1 = x2;
x2 = x3;
n = n+1;
error(n+1) = abs(x2-x1);
end
fprintf ('Las raíces son %.6f %.6f %.6f', x0, x1,x2)
end
  4 comentarios
llucia
llucia el 7 de Abr. de 2023
I have written an fprintf and I just get the real root but not the complex ones.
VBBV
VBBV el 7 de Abr. de 2023
That is due to usage of abs in this line
error(n+1) = abs(x2-x1);
%abs

Iniciar sesión para comentar.

Respuesta aceptada

Torsten
Torsten el 7 de Abr. de 2023
The argument of the root in the calculation of x2 must become negative.
Or simply start with complex values for x1,x2 and/or x3, e.g.
x0 = -1;
x1 = 0;
x2 = 3*1i;
  4 comentarios
llucia
llucia el 7 de Abr. de 2023
perfect. Thanks a lot. It works perfectly now.
Torsten
Torsten el 7 de Abr. de 2023
In numerical computations, you will never automatically get complex numbers if you don't start with complex numbers and if there are no expressions that can generate complex numbers (like the sqrt here).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Splines en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by