You are on page 1of 3

%Metodo de Bairstow

clear all; close all; clc;


grado=input('Ingrese el grado del polinmio: ');
coef=input('Ingrese los coeficientes del polinomio en orden ascendente []: ');
r=input('Ingrese el valor inicial de r: ');
s=input('Ingrese el valor inicial de s: ');
maxit=input('Ingrese el maximo numero de iteraciones : ');
errmax=input('Ingrese el maximo error aproximado permitido en(%) : ');

coefauxiliar=coef;
gradoauxiliar=grado;
arrc=[];
d=grado;
arrb=[];
flag=0;
count=0;

while(grado>0)

for j=1:maxit

d=grado;
arrb(d+1)=coef(d+1);
arrb(d)=coef(d)+r*arrb(d+1);
d=d-2;

while(d>=0)
arrb(d+1)=coef(d+1) + r*arrb(d+2) + s*arrb(d+3);
d=d-1;
end

d=grado;
arrc(d+1)=arrb(d+1);
arrc(d)=arrb(d)+ r*arrc(d+1);
d=d-2;

while(d>=0)
arrc(d+1)=arrb(d+1) + r*arrc(d+2) + s*arrc(d+3);
d=d-1;
end

ds = ( arrb(1)*arrc(3) - arrb(2)*arrc(2) )/( arrc(4)*arrc(2) - arrc(3)*arrc(3)


);
dr = ( arrb(1)*arrc(4) - arrb(2)*arrc(3) )/( arrc(3)*arrc(3) - arrc(2)*arrc(4)
);
r=r+dr;
s=s+ds;

err_r = abs(dr/r)*100 ;
err_s = abs(ds/s)*100 ;

if ( errmax > err_s || errmax > err_r )

root1 = ( r + abs(sqrt(r*r + 4*s)) )/2;


root2 = ( r - abs(sqrt(r*r + 4*s) ))/2;
fprintf(' ');

if(count==0)
fprintf(' %f %f ',root1,root2);
end
count=1;
flag=1;
break;
end

end
if j==maxit
fprintf('Se han realizado el m�ximo de iteraciones (%i) sin cumplir con
un error menor a %f porciento',maxit, errmax );
end
if flag==0
break;
end

for i=1:grado-1
coef(i)=arrb(i+2);
end

grado=grado-2;

if grado == 2
root1 = ( -(arrb(4)) + sqrt(arrb(4)*arrb(4) - 4*arrb(3)*arrb(5)) )/
(2*arrb(5));
root2 = ( -(arrb(4)) - sqrt( arrb(4)*arrb(4) - 4*arrb(3)*arrb(5) ))/
(2*arrb(5));
fprintf('%f%+fj %f%+fj\n ',root1,root2);
break;

elseif grado == 1
root = -arrb(3)/arrb(4);
fprintf('%f\n ',root);
break;

end

end

f=zeros(1,101);

for k=-50:50

for i=1:gradoauxiliar+1

f(k+51)=f(k+51)+coefauxiliar(i)*((k)^(i-1));

end

end

x=-50:50;
plot(x,f);
ax=gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
% M�todo de Muller
clear all; close all; clc;
grado= input('Ingrese el grado del polinomio: ');
fprintf('El polinomio de la forma (an*x^n + an-1*x^n-1 + ... + a1*x + a0)
debe ingresarse como vector (separado por espacios), \nSin olvidar los 0 como
coeficientes de los t�rminos inexistentes. \n');
coef= input('Ingrese los coeficientes del polinomio ENTRE CORCHETES de manera
ascendente [a0 a1 a2 ...] :'); %Los c�lculos se facilitan al tener los coef en un
vector
x0=input('Introduzca el valor de x0: ');
x1=input('Introduzca el valor de x1: ');
x2=input('Introduzca el valor de x2: ');
tol=input('Ingrese el error porcentual aproximado m�ximo: ');
Ea=100; %Error aproximado
i=0; %Contador de iteraciones
while abs(Ea) > tol
polivalor=polyval(coef,[x0 x1 x2]);

h0=x1-x0;
d0= (polivalor(2) - polivalor(1))/h0;
i=i+1;
h1=x2-x1;
d1= (polivalor(3) - polivalor(2))/h1;

a= (d1-d0)/ (h1+h0);
b= a*h1 + d1;
c= polivalor(3);

if b>0
x3= x2-2*c/(b + sqrt(b^2 -4*a*c));
else
x3= x2-2*c/(b - sqrt(b^2 -4*a*c));
end
Ea= abs((x3-x2)/x3)*100;
matrizdeiteraciones(i,1)=i; %Iteraci�n n�mero
matrizdeiteraciones(i,2)=x3; %Valor aproximado
matrizdeiteraciones(i,3)=Ea; %Error aproximado
x0=x1;
x1=x2;
x2=x3;

end
fprintf('La primera ra�z obtenida con el m�todo de Muller es: %f%+fi
\n',real(x3),imag(x3));

You might also like