You are on page 1of 2

Puntofijo

% Mtodos acotados: Mtodo del punto fijo.


clear, clc
cf = input('Ingrese funcin f: ');
cg = input('Ingrese funcin g: ');
f = inline(cf);
g = inline(cg);
syms x
dg = diff(cg, x);
x = input('Ingrese primer valor: ');

if( abs(eval(dg))<1 )
tol = input('Ingrese tolerancia: ');
disp(' n x0 error')
fprintf(' \0.0000 \%8.4f ----\n',x)
n = 0; error = 100;
while( error > tol )
n = n+1;
anterior = x;
x = g(x);
error = abs(((x-anterior)/x)*100);
disp([n,x,error])
end
fplot(cf,[x-5,x+5],'r'),grid,
else
disp('Ingrese otra funcin g(x), pues con la actual, el mtodo
diverge.')
end

secante
fx=input('ingrese la funcion f(x)=','s');
syms x %define como una variable
f=inline(fx); %crea una funcion a partir de los caracteres de fx
x0=input('ingrese el valor inicial inferior: ');
x1=input('ingrese el valor inicial superior: ');
max1=input('ingrese iteraciones: ')
fplot(fx,[x0,x1],'r'),grid,
fx0=f(x0);
fx1=f(x1);
if fx1*fx0==0
if fx0==0
fprintf('la raiz de la funcion es %f\n',x0)
else
fprintf('la raiz de la funcion es %f\n',x1)
end
elseif fx1~=0 && fx1~=0
disp('k X0 Xz X1 f(X)')
for z=1:max1
xz=x1-(f(x1)*(x0-x1))/(f(x0)-f(x1));
fxz=f(xz);
fprintf('%d\t%8.6f\t%8.6f\t%8.6f\t%8.6f\n',z,x0,xz,x1,fxz)
if fxz==0
fprintf('la raiz de la funcion es %f\n',xz)
break
else
x1=xz;
fx1=fxz;
end
end
hold on, plot(xz, fxz,'ob','MarkerSize',10,'MarkerFaceColor','k'),
hold off
else
disp('no hay raiz')
end
clear

You might also like