Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Grupo: 13
UNAM / FI / DCB
Semestre 2021-1
Tarea 8
Carrillo Martínez Cesar, Mendoza Luna Diego, Romero Martínez Eduardo
1. Resultados
1. Resuelviendo el siguiente problema de valor inicial
y
xy 0 + y = e x , y(1) = 1, 1 ≤ x ≤ 1.9
a) Euler con h=0.1
b) Método mejorado de Euler con h = 0.1
c) RK4 clásico con h = 0.3
a) Euler
b) Heun
c) RK4
2
AN/Tarea 3 Carrillo, Mendoza, Romero
2 t
2u + u + 3u = sin( ), u(0) = 0, u(1) = 1
3 2
a) Método de disparo con RK4 considerando un paso de h = 0.05
b) Método de diferencias finitas considerando ∆t = 0.25
3
AN/Tarea 3 Carrillo, Mendoza, Romero
4
AN/Tarea 3 Carrillo, Mendoza, Romero
3. Desarrollo
5
AN/Tarea 3 Carrillo, Mendoza, Romero
6
AN/Tarea 3 Carrillo, Mendoza, Romero
7
AN/Tarea 3 Carrillo, Mendoza, Romero
8
AN/Tarea 3 Carrillo, Mendoza, Romero
15
16 figure
17 plot(x,uEuler(1,:),’bs’,x,uHeun(1,:),’md’,x,uRK4(1,:),’gp’)
18 xlabel(’Variable x’,’interpreter’,’latex’,’Fontsize’,16)
19 ylabel(’y(x)’,’interpreter’,’latex’,’Fontsize’,16)
20 legend({[’EulerS h=’ num2str(h)],[’HeunS h=’ num2str(h)],...
21 [’RK4S h=’ num2str(h)]}, ’interpreter’,’latex’,’Fontsize’,12,’Location’,’be
Método de disparo.
1
2 clc
3 clear all
4 close all
5 %Método de disparo
6 f=@(t,x) [x(2); ((2/3)*(sin(t/2))-x(2)-3*x(1))/2];
7 t=linspace(0,1,20);
8 % Primer adivinanza
9 x0_adv1=[0;0];
10 x_adv1=RK4sist(f,t,x0_adv1)
11 u_10_adv1=x_adv1(1,end)
12
13 % Segunda adivinanza
14 x0_adv2=[0;2];
15 x_adv2=RK4sist(f,t,x0_adv2)
16 u_10_adv2=x_adv2(1,end)
17
18 yi=InterLagrange([u_10_adv1 u_10_adv2],[0 2],1)
19
20 x0_real=[0;yi];
21 x_pvi=RK4sist(f,t,x0_real)
22 u_10_real=x_pvi(1,end)
23
24 %figure
25 %plot(t,x_adv1(1,:),t,x_adv2(1,:),t,x_pvi(1,:))
26
27 % Método de diferencias finitas
28 A=[-61 34 0;
29 30 -61 34;
30 0 30 -61;]
31
32 b=[0.1649; 0.2442; -33.6844];
33
34 u2_5 = ResultCrout(A,b)
35 tDF = 0:0.25:1;
36 solDF = [0 u2_5’ 1];
9
AN/Tarea 3 Carrillo, Mendoza, Romero
37
38 figure(’Name’,’Ejercicio 3 ’)
39 plot(t,x_pvi(1,:),’bp’,tDF,solDF,’rd’,’Linewidth’,2,’MarkerSize’,8)
40 legend({[’Método Disparo h=0.05’],[’Diferencias Finitas \Deltat=0.25’]},’Loc
Códigos utilizados.
RK3 clásico
1 function y =RK3clasico(f,x,y0)
2 % f es la función => y’=f(x,y)
3 % x es el vector de valores donde se resuelve la ecuación diferencial
4 % y0 es el valor inicial y0=y(x0)
5 y = 0*x;
6 y(1) = y0;
7 h = x(2)-x(1);
8 n = length(x);
9 for i=1:n-1
10 k1 = f( x(i) , y(i));
11 k2 = f( x(i)+1/2*h, y(i)+1/2*k1*h);
12 k3 = f( x(i)+h , y(i)-k1*h+2*k2*h );
13
14 y(i+1) = y(i) + 1/6*h*( k1+4*k2+k3 );
15 end
16
17 end
Crout
1 function [u2_5]=ResultCrout(A,b)
2 [L,U]=Crout(A);
3 n=size(A,1); %Tamaño de la matriz
4 y=zeros(n,1); %Vector lleno de ceros
5 y(1)=b(1)/L(1); %El valor de y1
6 for i=2:n
7 y(i)=(b(i)-L(i,1:i-1)*y(1:i-1))/L(i,i); %Los valores de y(2:n)
8 end
9 u2_5=zeros(n,1); %Se crea un vector x con una columna de ceros
10 u2_5(n)=y(n); %Se comienza con el ultimo valor de x
11
12 for i=n-1:-1:1 %Sustitución hacia atras
13 u2_5(i)=(y(i)-U(i,i+1:n)*u2_5(i+1:n)); %Formula para las x restantes
14 end
15 end
16
17 function [L,U]=Crout(A) %Método LU
18 n=size(A,1); %Determina el tamaño de matriz A
19 U=eye(n); %Matriz identidad con la dimensión de A
10
AN/Tarea 3 Carrillo, Mendoza, Romero
20
21 for i=1:n
22 if i==1
23 L(1,1)=A(1,1); %valor inicial ya dado
24 for j=2:n %cuando un valor j es 2 se obtiene un valor L y U
25 U(1,j)=A(1,j)/L(1,1);
26 L(j,1)=A(j,1);
27 end
28 else
29 %Para un valor de la diagonal L
30 L(i,i)=A(i,i)-L(i,1:i-1)*U(1:i-1,i);
31
32 %Para los valores de L y U restantes
33 for j=i+1:n
34 L(j,i)=A(j,i)-L(j,1:i-1)*U(1:i-1,i);
35 U(i,j)=(A(i,j)-L(i,1:i-1)*U(1:i-1,j))/L(i,i);
36 end
37 end
38 end
39 end
RK4
1 function u=RK4sist(f,x,u0)
2 % f es un vector de funciones
3 % x es el vector con los valores en el dominio
4 % u0 es el vector de condiciones iniciales
5
6 u(:,1)=u0; % Primer columna es el vector de condiciones iniciales
7 h= x(2) - x(1); % Paso
8 n=length(x);
9
10 for i=1:n-1
11 k1= f(x(i), u(:,i));
12 k2= f(x(i)+h/2, u(:,i)+(h*k1)/2);
13 k3= f(x(i)+h/2, u(:,i)+(h*k2)/2);
14 k4= f(x(i)+h, u(:,i)+ h*k3);
15
16 u(:,i+1)= u(:,i) + h*(k1+ 2*k2+ 2*k3+ k4)/6;
17 end
18 end
Lagrange
1 function yi=InterLagrange(x,y,xi)
2 % x es el vector conocido de x’s
3 % y es el vector conocido de y’s
11
AN/Tarea 3 Carrillo, Mendoza, Romero
Heun
1 function u = HeunS_EDO(f,x,u0)
2 % f es un vector de funciones
3 % x es el vector con los valores en el dominio
4 % u0 es el vector de condiciones iniciales
5
6 u(:,1) = u0; % Primer columna es el vector de condiciones iniciales
7 h= x(2) - x(1); % Paso
8 n= length(x);
9
10 for i=1:n-1
11 k1 = f( x(i) , u(:,i) );
12 k2 = f( x(i)+h , u(:,i)+h*k1 );
13 u(:,i+1) = u(:,i) + h*(k1+k2)/2;
14 end
15
16 end
Euler EDO
function y = EulerEDO(f,x,y0)
% f es la función => y’=f(x,y)
% x es el vector de valores donde se resuelve la ecuación diferencial
% y0 es el valor inicial y0=y(x0)
y = 0*x;
y(1) = y0;
h = x(2)-x(1);
n = length(x);
for i= 1:n-1
12
AN/Tarea 3 Carrillo, Mendoza, Romero
end
Euler Sis
1 function u=EulerSis(f,x,u0)
2 % f es un vector de funciones
3 % x es el vector con los valores en el dominio
4 % u0 es el vector de condiciones iniciales
5
6 u(:,1)=u0; % Primer columna es el vector de condiciones iniciales
7 h=0.1; % Paso
8 n=length(x);
9
10 for i=1:n-1
11 u(:,i+1) = u(:,i) +h*f( x(i), u(:,i));
12 end
13 end
13