8/11/2019 programas de metodos numericos.docx
1/9
functiony=f(x)y=x+lnx;
Mtodo de la biseccionfunction[c,k]=biseccion(a,b,t)k=0;c=(a+b)/2;whileb-a>=t & f(c)~=0
iff(a)*f(c)0
disp('elija otros puntos')return
endfprintf('\n k | a b c error \n')fori=1:ceil(n)+1
c=(a+b)/2;e=abs(b-a);fprintf('%3d | %5.6f |%5.6f |%5.6f |%5.6f |\n',i-1,a,b,c,e)iff(a)*f(c)=e && abs(f(c))>=e
iff(c)==0break;
endiff(a)*f(c)
8/11/2019 programas de metodos numericos.docx
2/9
elsea=c;
endk=k+1;c=[(f(a)*b)-(f(b)*a)]/(f(a)-f(b));
endend
functiony=f(x)y=x+lnx;
Mtodo de newtonfunction[x,iter]=newton2(x0,f,df)% Algoritmo Newton-RaphsonN = 100; eps = 1.e-5; %Defino max. Numero. iteraciones y errormaxval = 10000.0; % Defino valor para divergenciax=x0;while(N>0)
xn = x-f(x)/df(x);%Funcin de Newtonifabs(f(xn))maxval
disp(['# de Iteraciones = ',num2str(iter)]);error('La Solucion Diverge');break;
end;N = N - 1;x = xn;
end;error('No Converge');return;% Fin de la Funcion
8/11/2019 programas de metodos numericos.docx
3/9
function[x,k]=newton(x,t1,t2)k=0; y=x+1;whileabs(f(x))>=t1 & abs(y-x)>=t2
y=x;x=x-f(x)/df(x);k=k+1;ifk>10000
break;end
functionNewton3(f,x,tol)disp('=========METODO DE NEWTON=========')g=diff(f);c='x'-f/g;%fprintf('g(x)=%c',pretty(c)')%pretty(c);f=inline(f);
c=inline(c);g=inline(g);k=-1;ifg(x)==0
disp('Dar otro valo inicial')return
endfprintf('\n k | x g(x) error\n')disp('-------------------------------------------')while1
8/11/2019 programas de metodos numericos.docx
4/9
Falsa posicionfunctionfalsaposicion1 (f,a,b,tol)f=inline(f);iff(a)*f(b)tol
c=b-(f(b)*(b-a)/(f(b)-f(a));fprintf('\n % 3d %5.7f %5.7f\n',I,a,b,c);ifI>100
break;endiff(a)*f(c)=e
c=[(f(a)*b)-(f(b)*a)]/(f(a)-f(b));iff(c)==0
break;endiff(a)*f(c)
8/11/2019 programas de metodos numericos.docx
5/9
iff(a)*f(b)tol
c=b-(f(b)*(b-a)/(f(b)-f(a));fprintf('\n % 3d %5.7f %5.7f\n',I,a,b,c);ifI>100
break;endiff(a)*f(c)
8/11/2019 programas de metodos numericos.docx
6/9
punto fijofunctionpuntofijo1(g,x,tol)g=inline(g);k=-1;disp('=======Metodo del punto fijo=======')fprintf(' k | x | g(x) | error |\n')disp('------------')while5
8/11/2019 programas de metodos numericos.docx
7/9
Secantefunction[x1,k]=secante(x0,x1,t1,t2)k=0;
whileabs(f(x1))>=t1 & abs(x1-x0)>=t2aux=x1;x1=x1-(f(x1)*(x1-x0))/(f(x1)-f(x0));x0=aux;k=k+1;
end
function[x1,k]=secantemed(x0,x1,t1,t2)k=0;whileabs(f(x1))>=t1 & abs(x1-x0)>=t2
aux=x1;x1=x1-(f(x1)*(x1-x0))/(f(x1)-f(x0));x0=aux;k=k+1;ifk>1000
error('parece que no converge');end
end
8/11/2019 programas de metodos numericos.docx
8/9
Sistema lineales
%matrizA=[5 -8 9 11;0 2 -7 13;0 0 1 17;0 0 0 9 ]B=[1 -2 30 -8
Gauss
functionx=GAUSS(A,B)[m,n]=size(A);fork=1:n
fori=k+1:nz=A(i,k)/A(k,k);
A(i,k)=0;forj=k+1:n
A(i,j)=A(i,j)-z*A(k,j);
endB(i)=B(i)-z*B(k);end
enddisp(A);x=zeros(n,1);x(n)=B(n)/A(n,n);fork=n-1:-1:1
s=0;forj=k+1:n
s=s+A(k,j)*x(j);endx(k)=(B(k)-s)/A(k,k);
end
8/11/2019 programas de metodos numericos.docx
9/9
Solucin de un sistema triangular superior
functionx=RSTS(A,b)[m,n]=size(A);x=zeros(n,1);
x(n)=b(n)/A(n,n);fork=n-1:-1:1s=0;forj=k+1:n
s=s+A(k,j)*x(j);endx(k)=(b(k)-s)/A(k,k);
end