Você está na página 1de 5

ALGORITMO PARA EL MÉTODO DE BISECCIÓN

function raiz=bisec(xizq,xder,ff)
%METODO DE BISECCION
Nmax=50; %numero maximo de interaciones
epsi=1e-5; %tolerancia de convergencia
yizq=feval(ff, xizq); yder=feval(ff, xder);
if yizq*yder<0
iter=1;
convergio=false;
while (iter<=Nmax && ~convergio)
xmed=(xizq+xder)/2; ymed=feval(ff,xmed);
convergio=abs(ymed)<=epsi | abs((xder-xizq)/2)<=epsi;
if ~convergio
if ymed*yizq>0
xizq=xmed; yizq=ymed;
else
xder=xmed; yder=ymed;
end
iter=iter+1;
end
end
if convergio
raiz=xmed;
else
raiz=NaN;
end
else
raiz=NaN;
end

>> f=inline('-x.^3/2400+x.^2/20+7*x/6+340')
f = Inline function:
f(x) = -x.^3/2400+x.^2/20+7*x/6+340
>> f=@(x)-x.^3/2400+x.^2/20+7*x/6+340
f = function_handle with value:
@(x)-x.^3/2400+x.^2/20+7*x/6+340
>> raiz=bisec(100,200,f)
raiz =
166.3293
>> f=inline('21*x.^3/5000000-127*x.^2/1000000+1293*x/50000')
f =Inline function:
f(x) = 21*x.^3/5000000-127*x.^2/1000000+1293*x/50000
>> f=@(x)21*x.^3/5000000-127*x.^2/1000000+1293*x/50000
f = function_handle with value:
@(x)21*x.^3/5000000-127*x.^2/1000000+1293*x/50000
>> raiz=bisec(-5,3,f)
raiz =
0
ALGORITMO PARA EL METODO DE LA SECANTE
function raiz=secan(x1,x2,ff)
%METODO DE LA SECANTE
Nmax=50; % Numero maximo de interaciones
epsi=1e-5; %Tolerancia de convergencia
y1=feval(ff,x1); y2=feval(ff,x2);
iter=1;
convergio=false;
while (iter<= Nmax & ~convergio)
x3=x1-y1*(x2-x1)/(y2-y1); y3=feval(ff,x3);
convergio=abs((x2-x3)/x2)<=epsi;
if ~convergio
x1=x2;
y1=y2;
x2=x3;
y2=y3;
iter=iter+1;
end
end
if convergio
raiz=x3;
else
raiz=NaN;
end
>> f=@(x)-x.^3/2400+x^2/20+7*x/6+340

f =function_handle with value:

@(x)-x.^3/2400+x^2/20+7*x/6+340

>> raiz=secan(1,2,f)

raiz =

166.3293
ALGORITMO PARA MÉTODO DE NEWTON RAPHSON
function raiz=raphson(xsup,ff)
%METODO DE NEWTON RAPHSON
Nmax=50; % Numero maximo de interaciones
epsi=1e-5; %Tolerancia de convergencia
delta=1e-6; %delta x
iter=1;
convergio=false;
while (iter<= Nmax && ~convergio)
deriv=(feval(ff,xsup+delta)-feval(ff,xsup))/delta;
xcalc=xsup-feval(ff,xsup)/deriv;
convergio=abs((xsup-xcalc)/xsup)<=epsi;
if ~convergio
xsup=xcalc;
iter=iter+1;
end
end
if convergio
raiz=xcalc;
else
raiz=NaN;
end

f = function_handle with value:

@(x)-x.^3/2400+x^2/20+7*x/6+340

>> raiz=raphson(2,f)

raiz =

166.3293
ALGORITMO PARA EL MÉTODO DE FALSA POSICIÓN
function raiz=falsa(x1,x2,ff)
%METODO DE LA FALSA POSICION
Nmax=50; % Numero maximo de interaciones
epsi=1e-5; %Tolerancia de convergencia
y1=feval(ff,x1); y2=feval(ff,x2);
if y1*y2<0
iter=1;
convergio=false;
while (iter<= Nmax && ~convergio)
x3=x1-y1*(x2-x1)/(y2-y1); y3=feval(ff,x3);
convergio=abs(y3)<=epsi;
if ~convergio
if y3*y1>0
x1=x3; y1=y3;
else
x2=x2; y2=y3;
end
iter=iter+1;
end
end
if convergio
raiz=x3;
else
raiz=NaN;
end
else
raiz=NaN;
end
f=

function_handle with value:

@(x)-x.^3/2400+x^2/20+7*x/6+340

>> raiz=falsa(100,200,f)

raiz =

166.3293
%METODO DE NEWTON RAPHSON
cf=input('ingrese ua funcion a evaluar:');
syms x
f=inline(cf);
derivada=diff(cf, x);
df=inline(derivada);
tol=input('ingrese tolerancia:');
error=50;
x=input('ingrese el valor inicial:');
n=0;
disp(' n xi error')
while(error>tol)
fprintf('\t%i\t%3.5f\t%f\n' ,n,x, error);
n=n+1;
x=x-f(x)/df(x);
error=abs(f(x));
end

>> newton

ingrese ua funcion a evaluar:'x^3+2*x^2+20*x-20'

ingrese tolerancia:0.00001

ingrese el valor inicial:1

n xi error

0 1.00000 50.000000

1 0.88889 0.060357

2 0.88656 0.000025

Você também pode gostar