Está en la página 1de 4

function raiz=biseccion(a,b)

%Datos

%f = es la funcion

%a,b son los valores extremos del intervalo

%Resultados= Raiz de la solucion

%raiz=valor de la solucion

error =0.0005;

k=0;

fxa=f(a);

fxb=f(b);

if fxa*fxb<=0

while abs(b-a)/2>error

m=(a+b)/2;

fprintf('%5d%10.5f%10.5f%10.5f\n',k,a,b,m);

k=k+1;

fxm=f(m);

if fxa*fxm<=0

b=m;

fxa=fxm

else

a=m;

fxa=fxm;

end

end

raiz=m;

else

fprintf('Cambiar de Limites');

end

///////////////////////////////
function x=biseccion(f,a,b,errormax)

if (feval(f,a)*feval(f,b))>0

disp('En este tramo el método no asegura nada');

x=0;

else

er=inf;

while (er>=errormax)

c=(a+b)/2;

if (feval(f,a)*feval(f,c))<=0

b=c;

else

a=c;

end

er=abs((b-a)/a);

end

x=c;

end

/////////////////////////////////////////////

function raiz=Regulafalsi(a,b)

%Datos

%f = es la funcion

%a,b son los valores extremos del intervalo

%Resultados= Raiz de la solucion

%raiz=valor de la solucion

error =0.0005;

k=0;

fxa=f(a);

fxb=f(b);

if fxa*fxb<=0

while 1
w=(fxb*a-fxa*b)/(fxb-fxa);

fprintf('%5d%10.5f%10.5f%10.5f\n',k,a,b,w);

k=k+1;

fxw=f(w);

if fxa*fxw<=0

b=w;

fxb=fxw;

else

a=w;

fxa=fxw;

end

if abs(fxw)<=error

break;

end

end

raiz=w;

else

fprintf('Cambiar de Limites');

end

///////////////////////////////////////////////////////

function raiz=Secante(x1,x2)

%Datos

%f = es la funcion

%a,b son los valores extremos del intervalo

%Resultados= Raiz de la solucion

%raiz=valor de la solucion

error =0.0005;

NTOL=50;

k=0;

while 1
x=x2-f1(x2)/((f1(x2)-f1(x1))/(x2-x1));

fprintf('%5d%10.5f%10.5f%10.5f\n',k,x1,x2,x);

k=k+1;

x1=x2;

x2=x;

if (abs(x1-x2)<=error)|(k==NTOL)

break;

end

end

if k<NTOL

raiz=x;

end

/////////////////////////////////////////////////////////////////

function y=f(x)

y=x.^2-2*x-2;

%plot(x,y);

%grid on

//////////////////////////////////////////////////////////

function y=f1(x)

y=exp(-x)-x;

////////////////////////////////////////////////

%Graficando

%x=linspace(-5,5,200);

x=-5:0.01;200;

y=x.^3-7*x+2;

plot(x,y);

////////////////////////////////////////////////

También podría gustarte