Você está na página 1de 13

Universidad Anhuac Mayab

Mtodos Numricos
Profesor: Juan Luis Lpez
Paulina Baqueiro Hadad
Cecilia Rene Gutirrez Oropeza
Mara del Mar Ibarra Ruiseor

Trabajo Final

CDIGOS DE MATLAB PARA LOS


MTODOS ESTUDIADOS

Mrida, Yucatn a 20 de noviembre del 2015.

Tabla de contenido
MTODOS NUMRICOS............................................................3
ALGORITMO DE MTODO DE BISECCIN...............................................3
ALGORTIMO DE MTODO NEWTON RAPHSON.......................................4
ALGORITMO DE MTODO DE LA SECANTE.............................................5
ALGORTIMO SERIE DE FIBONACCI..........................................................5
ALGORITMO DE FACTORIZACION LU......................................................6
ALGORITMO PARA ELIMINACIN GAUSSIANA SIN PIVOTEO....................7
ALGORITMO PARA ELIMINACION GAUSSIANA CON PIVOTEO..................8
ALGORITMO PARA MTODO CHOLESKY..................................................8
ALGORITMO PARA FACTORIZACIN QR..................................................8
LABORATORIOS.......................................................................9
LAB3 EJ1 :ALGORITMO PARA COMPARAR TIEMPO..................................9
LAB3 EJ1: RUTERO PLOT TIEMPOS.......................................................10
LAB3 EJ2: RUTERO PARA TIEMPOS DE REUSO DE A Y VARIOS B..........11
LAB 4...................................................................................................12
LAB 5...................................................................................................13

MTODOS NUMRICOS
ALGORITMO DE MTODO DE BISECCIN
function [ raiz ] = metbiseccion(m,n,o)
%Mediante el mtodo de biseccin se aproxima a la raz con un error
determinado
%por el usuario para la ecuacin no lineal de x
f=@(x)((m*x^2)+(n*x)+o)
a= input('ingrese el limite superior del intervalo:')
b= input('ingrese limite inferior del intervalo:')
err= input('ngrese el error admitido')
i=0;
e= 1000;
disp('ITERACION,
limite superior,
limite inferior,
while abs(e)>err
i=i+1;
k=i-1;
if i>1
x(k)=x(i-1);
end
x(i)=(a+b)/2;
raiz(i,1:4)=[i,a,b,x(i)];
if f(a)*f(x(i))<0
b=x(i);
else
a=x(i);
end
if i>1
e= f(x(i))-f(x(k));
end
format long
end

x(i)')

ALGORTIMO DE MTODO NEWTON RAPHSON


function [ raiz ] = NewtonRaphson( m,n,o )
%Para las funciones cuadraticas m*x^2 + n*x + o se obtiene la raz
mediante
%la convergencia por el mtodo de newton raphson
f=@(x)((m*x^2)+(n*x)+o)
df=@(x)((2*m*x)+n)
x(1)= input('ingrese el primer valor para aproximar:')
err= input('ingrese el error admitido')
i=1;
e= 1000;
disp('ITERACION,
X(i),
error)')
format long
while abs(e)>err
x(i+1)=x(i)-(f(x(i))/df(x(i)));
if i>1
e= f(x(i))-f(x(i-1));
end
raiz(i,1:3)=[i,x(i),e];
i=i+1;
end

ALGORITMO DE MTODO DE LA SECANTE


function [ raiz ] = Secante( m,n,o )
%Para las funciones cuadraticas m*x^2 + n*x + o se obtiene la raz
mediante
%la convergencia por el mtodo de la secante
f=@(x)((m*x^2)+(n*x)+o)
x(1)= input('ingrese el limite superior del intervalo:')
x(2)= input('ingrese limite inferior del intervalo:')
err= input('ngrese el error admitido')
e= 1000;
disp('ITERACION,
i=0;
k=1;

limite superior,

limite inferior,

while abs(e)>err
i=i+1;
x(k+2)=x(k+1)-(f(x(k+1))*((x(k+1)-x(k))/(f(x(k+1))-f(x(k)))));
if i>1
e= f(x(i))-f(x(i-1));
end
raiz(i,1:4)=[i,x(k),x(k+1),x(k+2)];
k=k+1;
end

ALGORTIMO SERIE DE FIBONACCI


function [ f ] = FIBONACCI( n )
%se muestran n valores de la serie de fibonacci
f=[1,1];
for i=3:n;
f=[f,(f(i-2)+f(i-1))];
end
disp('los valores de la serie de fibonacci que solicit son:')
end

x(i)')

ALGORITMO DE FACTORIZACION LU
function [ f ] = LU( n )
%factoriza a la matriz A en una matriz triangular superior y una inferior,
%se puede emplear para gauss o cholesky
a=input( 'Ingrese matriz A de coeficientes ')
b=input ( 'Ingrese vector b de resultados ')
m=diag(ones(n,1));
for k=1:n-1
for i=k+1:n
m(i,k)= a(i,k)/a(k,k);
for j=k:n
a(i,j)=a(i,j)-m(i,k)*a(k,j);
end
b(i)=b(i)-m(i,k)*b(k);
end
end
U=a
L=m
LporU=m*a
f=[L,U];
end

ALGORITMO PARA ELIMINACIN GAUSSIANA SIN PIVOTEO


Function x=MEG(n)
a=input( 'Ingrese matriz A de coeficientes ')
b=input ( 'Ingrese vector b de resultados ')
m=diag(ones(n,1));
for k=1:n-1
for i=k+1:n
m(i,k)= a(i,k)/a(k,k);
for j=k:n
a(i,j)=a(i,j)-m(i,k)*a(k,j);
end
b(i)=b(i)-m(i,k)*b(k);
end
end
U=a
L=m
LporU=m*a
x=zeros(n,1);
for r=n:-1:1
for s=n:-1:1
o(s)= a(r,s)*x(s);
end
x(r,1)= (1/a(r,r))*(b(r)-(sum(o)));
end
disp('
MATRIZ L DE A
, SOLUCION X, VECTOR B ')
MatrizAvectorSolucionyb=[a,x,transpose(b)]
disp(' EL VECTOR DE SOLUCIONES X: ')

ALGORITMO PARA ELIMINACION GAUSSIANA CON PIVOTEO


ALGORITMO PARA MTODO CHOLESKY
R=chol(A)

ALGORITMO PARA FACTORIZACIN QR


function [ x ] = factqr( A )
%gramm s factorizacion qr
%factoriza la matriz cuadrada o no cuadrada A en Q y en R triangular
%superior, para resolver de manera ms sencilla un sistema de
ecuaciones en
%el que se conoce b
[m, n] = size(A);
b=input ( 'Ingrese vector columna b de resultados ')
u=zeros(m,n);
q=zeros(m,n);
u(:,1)=A(:,1);
for i=2:n
u(:,i)=A(:,i);
resta=0;
for j=i:-1:2
resta=((u(:,j-1)'*A(:,i))*u(:,j-1)/(u(:,j-1)'*u(:,j-1)));
u(:,i)=u(:,i)-resta;
end
end
for i=1:n
q(:,i)=u(:,i)/(norm(u(:,i),2));
end
q
R=q'*A
c=q'*b
x=R\c;
end

LABORATORIOS
LAB3 EJ1 :ALGORITMO PARA COMPARAR TIEMPO
function [ tiempos ] = comparar_tiempos( nvec )
%funcin para comparar tiempos para resolver Ax=b
%mediante eliminacin gaussiana, calculo de la inversa y mtodo de
Cramer
%convirtiendo vector de entrada en vector columna con componentes
enteras
%nvec=floor (nvec(:));
%chequear que nvec solo contenga numeros naturales
if ~isnumeric(nvec) || any (nvec<=0) || any(isinf(nvec))
error ('entrada errnea')
end
tiempos=zeros(length(nvec),3);
for i=1: length(nvec)
n=nvec(i);
%Escribir aqu comandos para crear A y b
A= diag(2*ones(n,1))+diag(-1*ones(n-1,1),1)+diag(-1*ones(n-1,1),-1);
b= transpose(ones(length(1),n));
%eliminacin gaussiana
tic
x= A\b;
tiempos(i,1)=toc;
%clculo de la inversa
tic
x= inv(A)*b;
tiempos(i,2)=toc;
%Clculo con Cramer
tic
dA=det(A);
for k=1:length(b)
newA=[A(:,1:k-1) b A(:,k+1:length(b))];
x(k)=det(newA)/dA;
end
tiempos(i,3)=toc;
end

LAB3 EJ1: RUTERO PLOT TIEMPOS


tiempos = comparar_tiempos (10:10:100)
plot(tiempos(:,1),'o') %tiempos eliminacin Gaussiana
hold on
plot(tiempos(:,2),'x') %tiempos calculo de Inversa
plot(tiempos(:,3),'*') %tiempos mtodo cramer
legend('Gauss', 'Inversa', 'Cramer')

LAB3 EJ2: RUTERO PARA TIEMPOS DE REUSO DE A Y VARIOS B


%usando CPUTIME
A=rand(50);
%comprobar que A es invertible
while rank(A)~=50
A=rand(50);
end
B=rand(50,100);
t0=cputime;
X=A\B;
t1=cputime-t0
%inicializar matriz con soluciones de sistemas
Y=zeros(50,100);
t0=cputime;
for i=1:100
Y(:,i)=A\B(:,i);
end
t2=cputime-t0
dif=norm (X-Y, inf)
%TICTOC
A=rand(50);
%comprobar que A es invertible
while rank(A)~=50
A=rand(50);
end
B=rand(50,100);
tic;
X=A\B;
toc
%inicializar matriz con soluciones de sistemas
Y=zeros(50,100);
tic;
for i=1:100
Y(:,i)=A\B(:,i);
end
toc
dif=norm (X-Y, inf)

LAB 4
%Salmones en las celdas
%Se construye la matriz A segn las especificaciones del ejercicio 1 Lab
4
A=[.4 0 0 .2; 0 .4 .3 .2; 0 .3 .4 .2; .6 .3 .3 .4];
%Para determinar si la matriz A es invertible, comprobamos que el
produsto
%con su inversa genera una matriz identidad tamao n.
n=length(A);
if det(A)==0
'A no es invertible'
else
'se puede continuar, A es invertible'
b=1000*[12; 25; 26; 37];
'cada celda tena originalmente'
x=A\b
x= 25000*(ones(n,1));
'pasada una hora, cada celda tendr'
b=A*x
'pasadas tres horas, cada celda tendr'
b=A*(A*(A*x))
end

LAB 5
%Laboratorio 5. Se observan las funciones polyfit y polyval, en la que
%polyfit sirve para ajustar a una funcin de orden n los resultados, y
%polyval ubica los puntos generados a la funcion real.
x=0:1:10
y=sin(x);
p10=polyfit(x,y,10);
plot(x,y,'s');
hold on
x=linspace(0,10);
plot(x, sin(x),'b');
plot(x, polyval(p10,x),'r')
legend('puntos','f(x)=sin(x)','pol.interpolacion');
hold on
%Ejercicio 4
x=-5:1:5
fx= 1./(1+x.^2);
p6=polyfit(x,fx,6);
p10=polyfit(x,fx,10);
figure(3);
plot(x,fx,'o');
hold on
x=linspace(-5,5);
plot(x,1./(1+x.^2),'r');
plot(x, polyval(p6,x),'r')
plot(x, polyval(p10,x),'r')

Você também pode gostar