Beruflich Dokumente
Kultur Dokumente
SOLUCION DE
EJERCICIOS METODOS
NUMERICOS
Interpolacin y Mtodos Iterativos.
Ing. SAUL PEREZ PEREZ.
f ( x) , sean
fx=cosx .............................................................................................. 8
Programa 1....................................................................................................... 11
b fx=1+ x ............................................................................................... 12
Programa 2.................................................................................................... 15
c fx=lnx ................................................................................................. 16
d fx=tanx .................................................................................................. 16
Programa 3....................................................................................................... 19
2. Obtenga las tres primeras iteraciones de los mtodos de Jacobi y GaussSeidel para el siguiente sistema de ecuaciones, usando X (0)=0..................20
a) METODO DE JACOBI..........................................................................20
Ilustracin 4................................................................................................ 20
Programa 4....................................................................................................... 23
Ilustracin 5................................................................................................ 24
Programa 4....................................................................................................... 26
Ilustracin 6................................................................................................ 27
Ilustracin 7................................................................................................ 27
Ilustracin 8................................................................................................ 28
Programa 5....................................................................................................... 29
Ilustracin 9................................................................................................ 30
Ilustracin 10.............................................................................................. 30
Ilustracin 11.............................................................................................. 31
Programa 6................................................................................................. 32
las
funciones
dadas
f ( x) ,
sean
f ( x )=cos ( x )
a)
Grado 1
xi,
en la
f ( x)
0
1
xi
1
0.82533
5
0.6
2
0.62160
9
0.9
Tabla 1
P ( x )=L 0f ( x 0 ) + L1f ( x 1 )
Donde los valores de L, se muestran en la formula siguiente y los otros
dos corresponden a
F ( 0 ) =F ( x 0 ) y F ( 0.6 )=F ( x 1 )
P ( x )=
x0.6
x0
( 1 )+
( 0.825335 )
00.6
0.60
P ( x )=1.666666 x+ 1+ 1.375558 x
P ( x )=0.291108 x +1
Como se ve en este caso solo se toman los valores para I=0, I=1
Se evala
f (0.45)
|0.9004470.869001
|=0.034
0.900447
Er =
function Lagrange(x,y,xi);
p=0;
x=input('INGRESE VECTOR DE x: ')
xi=input('INGRESE VALOR xi: ')
y=input('INGRESE VECTOR y: ')
syms t;
n=length(x);
yi=zeros(size(xi));
for i=1:n
z=ones(size(xi));
L=1;
for j=1:n
if i~=j
L=L*(t-x(j))/(x(i)-x(j));
z=z.*(xi-x(j))/(x(i)-x(j));
end
end
yi=yi+z*y(i);
p=p+L*y(i);
p=simplify(p);
end
polinomio=p
yi
En MatLab
INGRESE VECTOR DE x: [0 0.6]
x=
0
0.6000
0.8253
polinomio =
1 - (7866212289146675*t)/27021597764222976
yi =
0.8690
P ( x )=
F ( 0.9 ) =F (x 2)
( x0.6 ) (x0.9)
( x0 )( x0.9)
( x0 ) ( x0.6 )
( 1 ) +
( 0.825335 )+
( 0.621609)
( 00.6 ) (00.9)
( 0.60 )( 0.60.9)
( 0.90 ) ( 0.90.6 )
P ( x )=0.431088 x 0.032455 x+ 1
Con el polinomio obtenido se pasa a evaluar
P(0.45)
F( 0.45)
se obtiene el error.
Rn=f ( x ) P ( x )
|0.9004470.898099
|=0.0026
0.900447
Er =
En MatLab
INGRESE VECTOR DE x: [0 0.6 0.9]
x=
0
0.6000
0.9000
0.8253
0.6216
polinomio =
- (17472990774278275*t^2)/40532396646334464 (877015979435365*t)/27021597764222976 + 1
yi =
0.8981
Solucin Excel.
Programa 1
b f ( x )= 1+ x
Grado 1
Construccin de la tabla con los valores, donde se evala el punto
indicado desde f(0) hasta f(2).
I
f ( x)
0
1
xi
1
1.26491
1
0.6
2
1.37840
4
0.9
Tabla 2
f ( 0.45 ) = 1+ x=1.204159
Teniendo en cuenta estos valores se pasa a encontrar el polinomio que
queda de la siguiente manera:
P ( x )=L 0f ( x 0 ) + L1f ( x 1 )
P ( x )=
x0.6
x0
( 1 )+
( 1.264911 )
00.6
0.60
P ( x )=1.666666 x+ 1+ 2.108185 x
P ( x )=0.441579 x +1
Como se ve en este caso solo se toman los valores para I=0, I=1
Se evala
P(0.45)
|1.2041591.198683
|=0.0045
1.204159
Er =
EN MatLab
Se procede a calcular el polinomio de interpolacin por medio de un cdigo en
MatLab.
INGRESE VECTOR DE x: [0 0.6]
x=
0
0.6000
1.2649
polinomio =
(5965265404431725*t)/13510798882111488 + 1
yi =
1.1987
Grado 2
En el anterior solo se usa L0 y L1, en este caso se utilizan los puntos para
realizar la interpolacin como muestra la siguiente formula.
P ( x )=
( x0.6 ) (x0.9)
( x0 )( x0.9)
( x0 ) ( x0.6 )
( 1 ) +
( 1.264911 )+
(1.378404 )
( 00.6 ) (00.9)
( 0.60 )( 0.60.9)
( 0.90 ) ( 0.90.6 )
P ( x )=
P(0.45)
|1.2041591.203421
|=6.110
1.204159
Er =
En MatLab
Utilizando el programa en MatLab Encontramos los resultados para la
interpolacin con los valores de la tabla 2.
INGRESE VECTOR DE x: [0 0.6 0.9]
x=
0
0.6000
0.9000
1.2649
polinomio =
1.3784
(4269975396700625*t^2)/60798594969501696
(19603786371975425*t)/40532396646334464 + 1
yi =
1.2034
Solucin Excel.
Programa 2
c f ( x ) =ln ( x )
Grado 1
Construccin de la tabla con los valores.
I
f ( x)
16,11809
57
xi
0,00000
01
1
0.51082
5
0.6
2
0.10536
0
0.9
Tabla 3
f (0.45) , no
Grado 1
Construccin de la tabla con los valores.
P ( x )=L 0f ( x 0 ) + L1f x 1
P ( x )=
x0.6
x0.0000001
(16.1180957 ) +
(0.510825 )
0.00000010.6
0.60.0000001
P ( x )=26.012122 x16.1150983
Como se ve en este caso solo se toman los valores para I=0, I=1
Se evala
P(0.45)
Rn=f ( x ) P ( x )
|0.7985076+ 4.409643|
=4.52235
|0.7985076|
Er =
Solucin en MatLab
INGRESE VECTOR DE x: [10^-7 0.6]
x=
0.0000
0.6000
yi =
-4.4126
Grado 2
En este punto se toman los tres valores de la tabla 4 y con ellos se calculan
los valores de L.
P ( x )=
( x0.6 ) (x0.9)
(16.1180957 ) +
( x 107 ) (x0.9)
( x107 ) ( x0.6 )
(
0.510825
)
+
(
( 0.6107 ) (0.60.9)
( 0.9107 ) ( 0.90.6 )
P(0.45)
2.5762877
|0.7985076+
|=2.2263
0.7985076
Er =
Solucin en MatLab.
INGRESE VECTOR DE x: [10^-7 0.6 0.9]
x=
0.0000
0.6000
0.9000
d f ( x )=tan ( x )
Grado 1
Construccin de la tabla con los valores.
I
f ( x)
0
0
xi
1
0.68413
6
0.6
2
1.26018
2
0.9
Tabla 4
P ( x )=L 0f ( x 0 ) + L1f ( x 1 )
P ( x )=
x0.6
x 0
( 0 ) +
( 0.684136 )
00.6
0.60
P ( x )=1.140226
Como se ve en este caso solo se toman los valores para I=0, I=1
Se evala
P(0.45)
|0.4830550.513102
|=0.06
0.483055
Er =
En MatLab.
Se toman los valores de la tabla 4 y se ingresan en el programa.
INGRESE VECTOR DE x: [0 0.6]
x=
0.6000
0.6841
polinomio =
(10270248782235805*t)/9007199254740992
yi =
0.5131
Grado 2
En este punto se toman los tres valores de la tabla 4 y con ellos se calculan
los valores de L.
P ( x )=
P ( x )=
( x0.6 ) (x0.9)
( x0 ) (x0.9)
( x0 )( x0.6 )
( 0 ) +
( 0.684136 ) +
(1.260182)
( 00.6 ) (00.9)
( 0.60 ) (0.60.9)
( 0.90 )( 0.90.6 )
x 2+ 0.9 x (
x 2 +0.6 x (
1.264911 )+
1.378404 )
0.18
0.27
P ( x )=3.800755 x 2+3.42068 x +4.667340 x 22.800404 x
2
P ( x )=0.866585 x +0.620176
Ahora Se evala
P(0.45)
|0.4830550.454607
|=0.058
0.483055
Er =
En MatLab
0.6000
0.9000
0.6841
1.2602
polinomio =
(5*t*(4683303260501750*t
3352167313040433))/27021597764222976
yi =
0.4546
En Excel
Programa 3
4 x 1 +x 2x 3+ x 4=2
x 1+ 4 x 2x 3x 4 =1
x 1x 2 +5 x3 + x 4 =0
x 1x 2+ x 3 +3 x 4=1
a) METODO DE JACOBI.
Convergencia:
4
1
-1
1
1
4
-1
-1
-1
-1
5
1
1
-1
1
3
4 >11+1 ; 4 >1
4 >111; 4>1
5>11+1 ; 5>1
3>11+1 ; 3>1
La matriz es triangular dominante. Por esta razn se puede pasar a realizar
los clculos correspondientes de la matriz.
Se pasa a realizar el despeje para cada variable, es decir
para cada una de las ecuaciones anteriores.
x1 , x2 , x3 y x4 ,
siguiente manera:
Ilustracin 4
x=[0 0 0 0]
Lo que se hace es reemplazar el vector de ceros y obtener el nuevo valor de las
x:
Primera iteracin:
Una vez se tiene el nuevo vector este se reemplaza en cada una de las
ecuaciones.
Segunda Iteracin:
Tol=0.520833+ 0.647965=0.1271
x=zeros(n,1);
mmax=input('ingrese el numero maximo de iteraciones mmax=')
if nargin>2
mmax=varargin{1};
end
if nargin>3
eps1=varargin{2};
end
if nargin>4
eps2=varargin{3};
end
if nargin>5
x(:)=varargin{4}; %x es un vector columna
end
errores=zeros(1,mmax);
d=diag(a);
r=(b-a*x);
normab=norm(b);
for m=1:mmax
x=x+r./d;
r=(b-a*x); % residuo
normar=norm(r);
errores(m)=normar;
if (normar<eps1+eps2*normab)
break
end
end
errores=errores(1:m)
if (m==mmax) & nargout<=3
disp('numero maximo de iteraciones sobrepasado')
end
% salida
if (nargout>1)
varargout{1}=m; % no de iteraciones
end
if (nargout>2)
varargout{2}=errores; % diferencia entre iteraciones
end
if (nargout>3)
% flag: 1 si hay convergencia, 0 en caso contrario
if m==mmax
varargout{3}=0;
else
varargout{3}=1;
end
end
return
En MatLab.
Ingresamos los valores en MatLab en el programa. Estos valores se ingresan
tipo matriz para ser resueltos.
ingrese a=[4 1 -1 1; 1 4 -1 -1; -1 -1 5 1; 1 -1 1 3]
0.6933
0.4430
En Excel.
Programa 4
b) GAUSS-SEIDEL.
Se realizan los mismos pasos hasta la
x 2 y as sucesivamente.
X =c+ Bx
Ilustracin 5
Primera iteracin:
Tol=0.624999+0.710416=0.085
|0.7104160.624999|
=0.1202351
|0.710416|
Norma=
mmax=varargin{1};
end
if nargin>3
eps1=varargin{2};
end
if nargin>4
eps2=varargin{3};
end
if nargin>5
x(:)=varargin{4}; %x es un vector columna
end
errores=zeros(1,mmax);
l=tril(a);
r=(b-a*x);
normab=norm(b);
for m=1:mmax
x=x+l\r;
r=(b-a*x); % residuo
normar=norm(r);
errores(m)=normar;
if (normar<eps1+eps2*normab)
break
end
end
errores=errores(1:m);
if (m==mmax) & nargout<=3
disp('numero maximo de iteraciones sobrepasado')
end
% salida
if (nargout>1)
varargout{1}=m; % no de iteraciones
end
if (nargout>2)
varargout{2}=errores; % diferencia entre iteraciones
end
if (nargout>3)
% flag: 1 si hay convergencia, 0 en caso contrario
if m==mmax
varargout{3}=0;
else
varargout{3}=1;
end
end
return
En MatLab
Se ingresa a, que es la matriz de coeficientes, b que es la matriz de
resultado y el numero de iteraciones.
Ingrese la matriz a=[4 1 -1 1; 1 4 -1 -1; -1 -1 5 1; 1 -1 1 3]
Ingrese la matriz b=[-2; -1; 0; 1]
0.3614
0.1207
En Excel
Programa 4
105 en la
4 x 1 +x 2x 3+ x 4=2
5.
x 1+ 4 x 2x 3x 4 =1
6.
x 1x 2 +5 x3 + x 4 =0
7.
x 1x 2+ x 3 +3 x 4=1
a. W=1.1
Lo primero que se hace es sacar el Gauss-Seidel y a partir de l se saca
las ecuaciones para SOR.
Ilustracin 6
Ahora se realizo para SOR donde se multiplica por el w dado cada uno de
los coeficientes y el vector de resultado y se coloca en el valor de
despeje el valor del punto que acompaa, es decir, el 0.1 en este caso.
La matriz Sor quedo de la siguiente forma:
Ilustracin 7
x k1=0.3 (0.55 )0.275 (0.12375 )+ 0.275 (0.148225 ) 0.275 ( 0.577307 )0.55= 0,66049
Tol=0.66049+ 0.74313=0.08
Y no es menor que la tolerancia dada por lo cual se debe seguir iterando.
|0.753424671+0.753425|
7
=4.3666710
|0.753424671|
Norma=
Programa MatLab.
function x= relax( a,b,Nmax,w,x0)
%Metodo Sor programa que resuelve sistemas de ecuaciones lineales por
medio
%del metodo de relajacion segun la ecuacion siguinte a*x=b.
% Donde a es la matriz de coeficientes correspondiente al metodo b es el
% vector de resultado Nmax es le numero de iteraciones, w es el factor de
% relajacion y x0 es el vector inicial.
a=input('ingrese matriz a:');
b=input('ingrese vector b:');
Nmax=input('ingrese Nmax:');
eps=5e-6;
w=input('ingrese el valor w=');
x0=input('ingrese vector x0=');
D=diag(diag(a));
L=-tril(a,-1);
U=-triu(a,1);
C=inv(D-w*L);
B=C*((1-w)*D+w*U);
f=w*C*b;
x1=B*x0+f;
k=1;
while norm(x1-x0)>=eps
x0=x1;
x1=B*x0+f;
k=k+1;
if k>Nmax
ps='No converge'
break;
end
end
ss=x1
Solucin MatLab
ingrese matriz a:[4 1 -1 1; 1 4 -1 -1; -1 -1 5 1; 1 -1 1 3]
ingrese vector b:[-2; -1; 0; 1]
ingrese Nmax:20
ingrese el valor w=1.1
ingrese vector x0=[0; 0; 0; 0]
ss =
-0.7534
0.0411
-0.2808
0.6918
Programa 5
b. W=1.3
Lo primero que se hace es sacar el Gauss-Seidel y a partir de l se saca
las ecuaciones para SOR.
Ilustracin 8
Ilustracin 9
Tercera Iteracin
Tol=0.705220.76401=0.05878
Y no es menor que la tolerancia dada por lo cual se debe seguir iterando, el
resto de iteraciones se muestran en la siguiente tabla, con su respectiva
norma.
|0.7534238760.753430709|
=9.06844106
|0.753423876|
Norma=
Ilustracin 8
Solucin MatLab.
ingrese matriz a:[4 1 -1 1; 1 4 -1 -1; -1 -1 5 1; 1 -1 1 3]
ingrese vector b:[-2; -1; 0; 1]
ingrese Nmax:20
ingrese el valor w=1.3
ingrese vector x0=[0; 0; 0; 0]
ss =
-0.7534
0.0411
-0.2808
0.6918
Programa 6