Está en la página 1de 25

Métodos Numéricos en Ecuaciones Diferenciales

Segunda Evaluación Formativa

Nombre: Juan Francisco Gracia Sercado Matrícula: 13-003-1349

1. Aplicar los métodos de Adams-Bashforth de orden 2 y de Adams-Moulton de orden


4 con h  0.1 y h  0.01 para aproximar en t  1.2 a las soluciones del problema
de valores iniciales
 x '  2 x  8 y  2, x  0   2

 y '  4 y  16t , y  0   2
Calcular la solución exacta del sistema para obtener el error en cada paso.

Solución:

Solución.
Parte Homogénea:
De la forma matricial 𝑋′𝐻 = 𝐴𝑋
2 8
𝐴=( ) , 𝑙𝑜𝑠 𝑣𝑎𝑙𝑜𝑟𝑒𝑠 𝑝𝑟𝑜𝑝𝑖𝑜𝑠 𝑠𝑜𝑛 𝜆1 = 2, 𝜆2 = 4. Vectores asociados a los
0 4
1 4
valores propios 𝑉1 = ( ) ; 𝑉2 = ( )
0 1
𝑥 1 4
Solución de la parte Homogénea (𝑦) = 𝐶1 ( ) 𝑒 2𝑡 + 𝐶2 ( ) 𝑒 4𝑡
0 1
Parte particular:
𝑎1 𝑏 𝑥 = 𝑎1 𝑡 + 𝑏1 𝑥 ′ = 𝑎1
Sea(𝑎 ) 𝑡 + ( 1 ) , 𝑡𝑒𝑛𝑒𝑚𝑜𝑠 𝑞𝑢𝑒 , 𝑑𝑒𝑟𝑖𝑣𝑎𝑛𝑑𝑜 𝑟𝑒𝑠𝑝𝑒𝑐𝑡𝑜 𝑎 𝑡, 𝑜𝑏𝑡𝑒𝑛𝑒𝑚𝑜𝑠 ′ .
2 𝑏2 𝑦 = 𝑎2 𝑡 + 𝑏2 𝑦 = 𝑎2

𝑥 ′ = 𝑎1 𝑎 = 2(𝑎1 𝑡 + 𝑏1 ) + 8(𝑎2 𝑡 + 𝑏2 ) + 2
Sustituyendo → 1
𝑦 ′ = 𝑎2 𝑎2 = 4(𝑎2 𝑡 + 𝑏2 ) + 16𝑡,

𝑎1 = (2𝑎1 𝑡 + 8𝑎2 𝑡) + 2𝑏1 + 8𝑏2 + 2


Realizando un poco de álgebra
𝑎2 = (4𝑎2 𝑡 + 16𝑡) + 4𝑏2 ,
Tenemos:
𝑎1 = 2𝑏1 + 8𝑏2 + 2 Ecuación particular:
𝑎2 = 4𝑏2 a1 = 16
2𝑎1 + 8𝑎2 = 0 𝑥 16 11
a 2 = −4 (𝑦 ) = ( ) 𝑡 + ( )
4𝑎2 + 16 = 0 −4 −1

b1 = 11

b2 = −1

Aplicando condiciones iniciales encontramos la solución general:


𝑥 1 4 16 11
(𝑦) = −25 ( ) 𝑒 2𝑡 + 3 ( ) 𝑒 4𝑡 + ( ) 𝑡 + ( )
0 1 −4 −1

1
ABAMSys(0,-2,2,0.1,12,2)

ABAMSys(0,-2,2,0.01,120,2)

2
2. Utiliza el método del disparo para resolver la ecuación diferencial

44
1
𝑥 ′′ = (32 + 2𝑡 3 − 𝑥 ′ ) + 𝑥, con condiciones de frontera
8
x 1  17 , x  3  . Halla
3
el valor de y0 para que la solución llegue a la condición final con una
8
precisión de 10 . Utiliza h  0.001 . Calcula los errores relativos en cada
iteración.

Solución:

𝑥′ = 𝑦
Sea 𝑥 ′ = 𝑦 , 𝑦 ′ = 𝑥 ′′ . 𝑇𝑒𝑛𝑒𝑚𝑜𝑠 𝑒𝑙 𝑠𝑖𝑠𝑡𝑒𝑚𝑎 𝑑𝑒 𝑒𝑐𝑢𝑎𝑐𝑖𝑜𝑛𝑒𝑠 𝑑𝑖𝑓𝑒𝑟𝑒𝑛𝑐𝑖𝑎𝑙𝑒𝑠 { 1
𝑦′ = (32 + 2𝑡 3 − 𝑦) + 𝑥
8

Resultados.

Utilizamos el método de disparos para encontrar el intervalo [a,b] y utilizar el


método de bisección disparos. En el método de disparos hacemos variar el
parámetro y.

Disparos1(1,17,-100,0.001,3000)

3
Disparos1(1,17,10,0.001,3000)

El intervalo [a,b] donde converge el método de bisección disparos es [-100,10].

El método de bisección disparos se utiliza para encontrar la “y” exacta para el


método del disparo.

biseccionDisparos1 (-100,10,0.00001,100)
La solución hallada en 24 iteraciones es c=-18.336263298988
Con y=-18.336263298988, utilizamos Disparos1(1,17,-18.336263298988,0.001,2000)

4
3. Resuelve el problema anterior mediante un sistema de diferencias finitas.
Utiliza x  0.001 . Calcula los errores relativos en cada iteración.

Solución:
𝑥𝑖+1 −𝑥𝑖−1
𝑥′ =
2∆𝑥
Sea { 𝑥𝑖+1 −2𝑥𝑖 +𝑥𝑖−1
𝑥 ′′ =
∆𝑥 2

1
Sustituyendo primeras y segundas diferencias en 𝑥 ′′ = (32 + 2𝑡 3 − 𝑥 ′ ) + 𝑥
8

𝑥𝑖+1 −2𝑥𝑖 +𝑥𝑖−1 1 𝑥𝑖+1 −𝑥𝑖−1


= (32 + 2𝑡 3 − ) + 𝑥𝑖
∆𝑥 2 8 2∆𝑥

Realizando el álgebra tenemos:

𝑡𝑖3
𝑥𝑖+1 (16∆𝑥 + ∆𝑥 2 ) + 𝑥𝑖 (−16∆𝑥 3 − 32∆𝑥) + 𝑥𝑖−1 (16∆𝑥 − 32∆𝑥 2 ) = 16∆𝑥 3 (4 + )
4

Solución Octave:

5
4. Utiliza el método del disparo para resolver la ecuación diferencial no lineal

𝑥 ′′ = (1 − 𝑥 2 )𝑥′ − 𝑥, con condiciones de frontera x  0  1 , x 10  20 . Halla el


valor de y0 para que la solución llegue a la condición final con una precisión
8
de 10 . Utiliza h  0.01 . Calcula los errores relativos en cada iteración,
comparando la solución aproximada con la solución numérica obtenida mediante el
método de RK4.

Solución:

𝑥′ = 𝑦
Sea 𝑥 ′ = 𝑦 , 𝑦 ′ = 𝑥 ′′ . 𝑇𝑒𝑛𝑒𝑚𝑜𝑠 𝑒𝑙 𝑠𝑖𝑠𝑡𝑒𝑚𝑎 𝑑𝑒 𝑒𝑐𝑢𝑎𝑐𝑖𝑜𝑛𝑒𝑠 𝑑𝑖𝑓𝑒𝑟𝑒𝑛𝑐𝑖𝑎𝑙𝑒𝑠 { ′
𝑦 = (1 − 𝑥 2 )𝑦 − 𝑥

Resultados.

Utilizamos el método de disparos para encontrar el intervalo [a,b] y utilizar el


método de bisección disparos. En el método de disparos hacemos variar el
parámetro y.

Disparos2(0,1,2500,0.01,1000)

6
Disparos2(0,1,3500,0.01,1000)

El intervalo [a,b] donde converge el método de bisección disparos es


[2500,3500].

El método de bisección disparos se utiliza para encontrar la “y” exacta para el


método del disparo.

biseccionDisparos1 (2500,3500,0.00001,50)

La solucion hallada en 27 iteraciones es c=2849.640540778637

7
Utilizamos el método de Disparos2(0,1, 2849.640540778637,0.001,10000)

8
Apéndice.

Métodos de Adams-Bashforth y de Adams-Moulton

function ABAMSys(t0,x0,y0,h,n,grado)

t(1)=t0;

xExacta(1)=x0;

yExacta(1)=y0;

xAB(1)=x0;

yAB(1)=y0;

xAM(1)=x0;

yAB(1)=y0;

xRK4(1)=x0;

yRK4(1)=y0;

errAB(1)=0;

errAM(1)=0;

errRK4(1)=0;

for k=1:grado

t(k)=t0-(grado-k)*h;

t(k);

xExacta(k)=xSolExacta(t(k));

xAB(k)=xSolExacta(t(k));

xAM(k)=xSolExacta(t(k));

xRK4(k)=xSolExacta(t(k));

yExacta(k)=ySolExacta(t(k));

yAB(k)=ySolExacta(t(k));

yAM(k)=ySolExacta(t(k));

yRK4(k)=ySolExacta(t(k));

end

for k=grado:n+1+grado

t(k+1)=t(k)+h;

xExacta(k+1)=xSolExacta(t(k+1));

yExacta(k+1)=ySolExacta(t(k+1));

[xAB(k+1),yAB(k+1)]=metAB(k,t,xAB,yAB,h,grado);

[xAM(k+1),yAM(k+1)]=metAM(k,t,xAM,yAM,h,grado);

[xRK4(k+1),yRK4(k+1)]=metRK4Sys(t(k),xRK4(k),yRK4(k),h);

9
end

fprintf('%3s %15.12s %15.12s %15.12s %15.12s\n','k','x(k)','Error ABashforth','Error AMoulton','Error RK4')

for k=1:n+2+grado

err1=sqrt((xAB(k)-xExacta(k))^2+(yAB(k)-yExacta(k))^2);

err2=sqrt((xAM(k)-xExacta(k))^2+(yAM(k)-yExacta(k))^2);

err3=sqrt((xRK4(k)-xExacta(k))^2+(yRK4(k)-yExacta(k))^2);

errAB(k)=err1/norm(xAB(k),yAB(k));

errAM(k)=err2/norm(xAM(k),yAM(k));

errRK4(k)=err3/norm(xRK4(k),yRK4(k));

fprintf('%3d %15.12f %15.12f %15.12f %15.12f\n',k,t(k),errAB(k),errAM(k),errRK4(k))

end

clf()

subplot(3,3,1)

title('xExacta vs xAB')

hold on

plot(t,xExacta)

plot(t,xExacta,'o')

plot(t,xAB)

plot(t,xAB,'+')

subplot(3,3,2)

title('xExacta vs xAM')

hold on

plot(t,xExacta)

plot(t,xExacta,'o')

plot(t,xAM)

plot(t,xAM,'+')

subplot(3,3,3)

title('xExacta vs xRK4')

hold on

plot(t,xExacta)

plot(t,xExacta,'o')

plot(t,xRK4)

plot(t,xRK4,'+')

subplot(3,3,4)

title('yExacta vs yAB')

hold on

10
plot(t,yExacta)

plot(t,yExacta,'*')

plot(t,yAB)

plot(t,yAB,'o')

subplot(3,3,5)

title('yExacta vs yAM')

hold on

plot(t,yExacta)

plot(t,yExacta,'*')

plot(t,yAM)

plot(t,yAM,'+')

subplot(3,3,6)

title('yExacta vs yRK4')

hold on

plot(t,yExacta)

plot(t,yExacta,'o')

plot(t,yRK4)

plot(t,yRK4,'p')

subplot(3,3,7)

hold on

title('Grafica de los errores AB')

plot(t,errAB)

%plot(t,errAB,'+')

subplot(3,3,8)

title('Grafica de los errores AM')

hold on

plot(t,errAM)

%plot(t,errAM,'*')

subplot(3,3,9)

title('Grafica de los errores RK4')

hold on

plot(t,errRK4)

%plot(t,errRK4,'o')

end

function [xsal,ysal]=metAB(k,t,x,y,h,grado) %% Metodo de Adams-Bashforth

11
if grado==1

xsal=x(k)+h*f1(t(k),x(k),y(k));

ysal=y(k)+h*f2(t(k),x(k),y(k));

elseif grado==2

xsal=x(k)+h/2*(3*f1(t(k),x(k),y(k))-f1(t(k-1),x(k-1),y(k-1)));

ysal=y(k)+h/2*(3*f2(t(k),x(k),y(k))-f2(t(k-1),x(k-1),y(k-1)));

elseif grado==3

xsal=x(k)+h/12*(23*f1(t(k),x(k),y(k))-16*f1(t(k-1),x(k-1),y(k-1))+5*f1(t(k-2),x(k-2),y(k-2)));

ysal=y(k)+h/12*(23*f2(t(k),x(k),y(k))-16*f2(t(k-1),x(k-1),y(k-1))+5*f2(t(k-2),x(k-2),y(k-2)));

elseif grado==4

xsal=x(k)+h/24*(55*f1(t(k),x(k),y(k))-59*f1(t(k-1),x(k-1),y(k-1))+37*f1(t(k-2),x(k-2),y(k-2))-9*f1(t(k-3),x(k-
3),y(k-3)));

ysal=y(k)+h/24*(55*f2(t(k),x(k),y(k))-59*f2(t(k-1),x(k-1),y(k-1))+37*f2(t(k-2),x(k-2),y(k-2))-9*f2(t(k-3),x(k-
3),y(k-3)));

elseif grado==5

xsal=x(k)+h/720*(1901*f1(t(k),x(k),y(k))-2774*f1(t(k-1),x(k-1),y(k-1))+2616*f1(t(k-2),x(k-2),y(k-2))-
1274*f1(t(k-3),x(k-3),y(k-3))+251*f1(t(k-4),x(k-4),y(k-4)));

ysal=y(k)+h/720*(1901*f2(t(k),x(k),y(k))-2774*f2(t(k-1),x(k-1),y(k-1))+2616*f2(t(k-2),x(k-2),y(k-2))-
1274*f2(t(k-3),x(k-3),y(k-3))+251*f2(t(k-4),x(k-4),y(k-4)));

elseif grado==6

xsal=x(k)+h/1440*(4277*f1(t(k),x(k),y(k))-7923*f1(t(k-1),x(k-1),y(k-1))+9982*f1(t(k-2),x(k-2),y(k-2))-
7298*f1(t(k-3),x(k-3),y(k-3))+2877*f1(t(k-4),x(k-4),y(k-4))-475*f1(t(k-5),x(k-5),y(k-5)));

ysal=y(k)+h/1440*(4277*f2(t(k),x(k),y(k))-7923*f2(t(k-1),x(k-1),y(k-1))+9982*f2(t(k-2),x(k-2),y(k-2))-
7298*f2(t(k-3),x(k-3),y(k-3))+2877*f2(t(k-4),x(k-4),y(k-4))-475*f2(t(k-5),x(k-5),y(k-5)))

elseif grado==7

xtemp=198721*f1(t(k),x(k),y(k))-447288*f1(t(k-1),x(k-1),y(k-1))+705549*f1(t(k-2),x(k-2),y(k-2));

xtemp=xtemp-688256*f1(t(k-3),x(k-3),y(k-3))+407139*f1(t(k-4),x(k-4),y(k-4));

xtemp=xtemp-134472*f1(t(k-5),x(k-5),y(k-5))+19087*f1(t(k-6),x(k-6),y(k-6));

xsal=x(k)+h/60480*xtemp;

ytemp=198721*f2(t(k),x(k),y(k))-447288*f2(t(k-1),x(k-1),y(k-1))+705549*f2(t(k-2),x(k-2),y(k-2));

ytemp=ytemp-688256*f2(t(k-3),x(k-3),y(k-3))+407139*f2(t(k-4),x(k-4),y(k-4));

ytemp=ytemp-134472*f2(t(k-5),x(k-5),y(k-5))+19087*f2(t(k-6),x(k-6),y(k-6));

ysal=y(k)+h/60480*ytemp;

end

end

function [xsal,ysal]=metAM(k,t,x,y,h,grado) %% Metodo de Adams-Moulton

if grado>=2 & grado<=6

for j=1:30

12
xPred=metAB(k,t,x,y,h,grado);

x(k+1)=xPred;

yPred=metAB(k,t,x,y,h,grado);

y(k+1)=yPred;

end

end

if grado==1

xsal=x(k)+h*f1(t(k),x(k),y(k));

ysal=y(k)+h*f2(t(k),x(k),y(k));

elseif grado==2

xsal=x(k)+h/2*(f1(t(k-1),x(k+1),y(k+1))+f1(t(k-1),x(k),y(k)));

ysal=x(k)+h/2*(f2(t(k-1),x(k+1),y(k+1))+f2(t(k-1),x(k),y(k)));

elseif grado==3

xsal=x(k)+h/12*(5*f1(t(k+1),x(k+1),y(k+1))+8*f1(t(k),x(k),y(k))-f1(t(k-1),x(k-1),y(k-1)));

ysal=y(k)+h/12*(5*f2(t(k+1),x(k+1),y(k+1))+8*f2(t(k),x(k),y(k))-f2(t(k-1),x(k-1),y(k-1)));

elseif grado==4

xsal=x(k)+h/24*(9*f1(t(k+1),x(k+1),y(k+1))+19*f1(t(k),x(k),y(k))-5*f1(t(k-1),x(k-1),y(k-1))+f1(t(k-2),x(k-
2),y(k-2)));

ysal=y(k)+h/24*(9*f2(t(k+1),x(k+1),y(k+1))+19*f2(t(k),x(k),y(k))-5*f2(t(k-1),x(k-1),y(k-1))+f2(t(k-2),x(k-
2),y(k-2)));

elseif grado==5

xsal=x(k)+h/720*(251*f1(t(k+1),x(k+1),y(k+1))+646*f1(t(k),x(k),y(k))-264*f1(t(k-1),x(k-1),y(k-1))+106*f1(t(k-
2),x(k-2),y(k-2))-19*f1(t(k-3),x(k-3),y(k-3)));

ysal=y(k)+h/720*(251*f2(t(k+1),x(k+1),y(k+1))+646*f2(t(k),x(k),y(k))-264*f2(t(k-1),x(k-1),y(k-1))+106*f2(t(k-
2),x(k-2),y(k-2))-19*f2(t(k-3),x(k-3),y(k-3)));

elseif grado>=6

xsal=x(k)+h/1440*(475*f1(t(k+1),x(k+1),y(k+1))+1427*f1(t(k),x(k),y(k))-798*f1(t(k-1),x(k-1),y(k-
1))+482*f1(t(k-2),x(k-2),y(k-2))-173*f1(t(k-3),x(k-3),y(k-3))+27*f1(t(k-4),x(k-4),y(k-4)));

ysal=y(k)+h/1440*(475*f2(t(k+1),x(k+1),y(k+1))+1427*f2(t(k),x(k),y(k))-798*f2(t(k-1),x(k-1),y(k-
1))+482*f2(t(k-2),x(k-2),y(k-2))-173*f2(t(k-3),x(k-3),y(k-3))+27*f2(t(k-4),x(k-4),y(k-4)));

end

end

function [RK4x,RK4y]=metRK4Sys(t0,x0,y0,h)

k11=f1(t0,x0,y0);

k12=f2(t0,x0,y0);

k21=f1(t0+h/2,x0+k11*h/2,y0+k12*h/2);

k22=f2(t0+h/2,x0+k11*h/2,y0+k12*h/2);

k31=f1(t0+h/2,x0+k21*h/2,y0+k22*h/2);

k32=f2(t0+h/2,x0+k21*h/2,y0+k22*h/2);

13
k41=f1(t0+h,x0+k31*h,y0+k32*h);

k42=f2(t0+h,x0+k31*h,y0+k32*h);

RK4x=x0+h/6*(k11+2*k21+2*k31+k41);

RK4y=y0+h/6*(k12+2*k22+2*k32+k42);

endfunction

function [x1]=f1(t,x,y)

x1=2*x+8*y+2;

endfunction

function [y1]=f2(t,x,y)

y1=4*y+16*t;

endfunction

function [sal]=xSolExacta(t)

sal=-25*exp(2*t)+12*exp(4*t)+16*t+11;

endfunction

function [sal]=ySolExacta(t)

sal=3*exp(4*t)-4*t-1;

endfunction

14
Método de Disparo
function Disparos1(t0,x0,y0,h,n)

t(1)=t0;

x(1)=x0; function [t,x]=difFinitasExamen(t0,x0,tf,xf,n)

more off

dx=(tf-t0)/n;

k0=16*dx-dx^2;

k1=-32*dx-16*dx^3;

k2=16*dx+dx^2;

A=tridiag(k0,k1,k2,n);

b=zeros(n-1,1);

b(1,1)=x0;

for k=2:n-1

tk=t0+(k-1)*dx;

b(k,1)=16*dx^3*(4+tk^3/4);

end

b(n,1)=xf;

x=A\b;

fprintf('\n')

fprintf('%4s %12s %12s\n','#','t(k)','x(k)');

for k=1:n

t(k)=t0+(k-1)*dx;

fprintf('%4d %12.8f %12.8f\n',k,t(k),x(k))

end

fprintf('\n')

clf()

hold on

plot(t,x)

plot(t,x,'*;difFinitas;')

end

function [A]=tridiag(lower,diag,upper,n)

A=eye(n,n);

for k=2:n-1

A(k,k)=diag;

end

15
for k=2:n-1

A(k,k-1)=lower;

A(k,k+1)=upper;

end

end

y(1)=y0;

for k=1:n-1

t(k+1)=t(k)+h;

[x(k+1),y(k+1)]=metRk4Sys(t(k),x(k),y(k),h);

end %cierra el fork

fprintf("\n%3s %10s %10s %10s\n","n","t","x","y")

for k=1:n

fprintf("%3d %10.7f %10.7f %10.7f\n",k-1,t(k),x(k),y(k));

end % cierra el for k

clf()

hold on

subplot(2,2,1)

plot (t,x)

title ('Curso temporal t vs x(t)')

subplot (2,2,2)

plot (t,y)

title ('curso temporal t vs y(t)')

subplot(2,2,3)

plot (x,y)

title('Retrato fase x(t) vs y(t)')

subplot (2,2,4)

plot3 (x,y,t)

title ('Espacio de soluciones x vs y vs t')

end %termina la funcion principal

function [xSal,ySal]=metRk4Sys(t0,x0,y0,h)

k1=f1(t0,x0,y0);

M1=f2(t0,x0,y0);

k2=f1(t0+h/2,x0+(h*k1)/2,y0+(h*M1)/2);

M2=f2(t0+h/2,x0+(h*k1)/2,y0+(h*M1)/2);

k3=f1(t0+h/2,x0+(h*k2)/2,y0+(h*M2)/2);

M3=f2(t0+h/2,x0+(h*k2)/2,y0+(h*M2)/2);

16
k4=f1(t0+h,x0+h*k3,y0+h*M3);

M4=f2(t0+h,x0+h*k3,y0+h*M3);

xSal=x0+(h/6)*(k1+2*k2+2*k3+k4);

ySal=y0+(h/6)*(M1+2*M2+2*M3+M4);

end

function [x1]=f1(t,x,y)

x1=y;

end

function [y1]=f2(t,x,y)

%y1=x-(y/8)+(t^3)/4+4;

y1=1/8*(32+2*t^3-y)+x;

end

function Disparos2(t0,x0,y0,h,n)

t(1)=t0;

x(1)=x0;

y(1)=y0;

for k=1:n

t(k+1)=t(k)+h;

[x(k+1),y(k+1)]=metRk4Sys(t(k),x(k),y(k),h);

end %cierra el fork

fprintf("\n%3s %10s %10s %10s\n","n","t","x","y")

for k=1:n+1

fprintf("%3d %10.7f %10.7f %10.7f\n",k-1,t(k),x(k),y(k));

end % cierra el for k

clf()

hold on

subplot(2,2,1)

plot (t,x)

title ('Curso temporal t vs x(t)')

subplot (2,2,2)

plot (t,y)

title ('curso temporal t vs y(t)')

subplot(2,2,3)

plot (x,y)

title('Retrato fase x(t) vs y(t)')

17
subplot (2,2,4)

plot3 (x,y,t)

title ('Espacio de soluciones x vs y vs t')

end %termina la funcion principal

function [xSal,ySal]=metRk4Sys(t0,x0,y0,h)

k1=f1(t0,x0,y0);

M1=f2(t0,x0,y0);

k2=f1(t0+h/2,x0+(h*k1)/2,y0+(h*M1)/2);

M2=f2(t0+h/2,x0+(h*k1)/2,y0+(h*M1)/2);

k3=f1(t0+h/2,x0+(h*k2)/2,y0+(h*M2)/2);

M3=f2(t0+h/2,x0+(h*k2)/2,y0+(h*M2)/2);

k4=f1(t0+h,x0+h*k3,y0+h*M3);

M4=f2(t0+h,x0+h*k3,y0+h*M3);

xSal=x0+(h/6)*(k1+2*k2+2*k3+k4);

ySal=y0+(h/6)*(M1+2*M2+2*M3+M4);

end

function [x1]=f1(t,x,y)

x1=y;

end

function [y1]=f2(t,x,y)

%y1=x-x^3-y;

y1=(1-x^2)*y-x;

end

18
Método de bisección disparos

function biseccionDisparos1(a, b, TOL, N)

traza(a,b,100)

if f(a)*f(b)>0

fprintf("\nNo se cumplen las condiciones para aplicar el metodo de biseccion...\n")

else

i=1;

FA=f(a);

fprintf("\n\n %3s %15s %15s %15s %15s\n","i","a","c","b","error")

while (i<=N)

c=(a+b)/2;

FC=f(c);

fprintf("%3d %15.12f %15.12f %15.12f %15.12f\n",i,a,c,b,(b-a)/2)

if ((FC==0) | (abs(b-a)/2<TOL))

fprintf("\nLa solucion hallada en %d iteraciones es c=%15.12f\n",i,c)

break

end

i=i+1;

if (FA*FC>0)

a=c;

FA=FC;

else

b=c;

end

end

if (i>N)

fprintf("\nEl metodo fallo después de %d iteraciones...\n",N)

end

end

end

function [y]=f(x)

t0=1;

x0=17;

19
y0=x;

y=RK4SysDisparos(t0,x0,y0,0.001,2000)-44/3;

end

function traza(a, b, n)

incX=(b-a)/n;

for i=1:n+1

x(i)=a+(i-1)*incX;

y(i)=f(x(i));

ejeX(i)=0;

end

clf()

plot(x,y,x,ejeX)

end

function [sal]=RK4SysDisparos(t0,x0,y0,h,n)

t(1)=t0;

x(1)=x0;

y(1)=y0;

for k=1:n

t(k+1)=t(k)+h;

[x(k+1),y(k+1)]=metRk4Sys(t(k),x(k),y(k),h);

end %cierra el for k

sal=x(n+1);

end %termina la funcion principal

function [xSal,ySal]=metRk4Sys(t0,x0,y0,h)

k1=f1(t0,x0,y0);

M1=f2(t0,x0,y0);

k2=f1(t0+h/2,x0+(h*k1)/2,y0+(h*M1)/2);

M2=f2(t0+h/2,x0+(h*k1)/2,y0+(h*M1)/2);

k3=f1(t0+h/2,x0+(h*k2)/2,y0+(h*M2)/2);

M3=f2(t0+h/2,x0+(h*k2)/2,y0+(h*M2)/2);

k4=f1(t0+h,x0+h*k3,y0+h*M3);

M4=f2(t0+h,x0+h*k3,y0+h*M3);

20
xSal=x0+(h/6)*(k1+2*k2+2*k3+k4);

ySal=y0+(h/6)*(M1+2*M2+2*M3+M4);

end

function [sal]=f1(t,x,y)

sal=y;

end

function [sal]=f2(t,x,y)

sal=1/8*(32+2*t^3-y)+x;

end

function biseccionDisparos2(a, b, TOL, N)

%traza(a,b,100)

FA=f(a)

FB=f(b)

if FA*FB>0

fprintf("\nNo se cumplen las condiciones para aplicar el metodo de biseccion...\n")

else

i=1;

FA=f(a);

fprintf("\n\n %3s %15s %15s %15s %15s\n","i","a","c","b","error")

while (i<=N)

c=(a+b)/2;

FC=f(c);

fprintf("%3d %15.12f %15.12f %15.12f %15.12f\n",i,a,c,b,(b-a)/2)

if ((FC==0) | (abs(b-a)/2<TOL))

fprintf("\nLa solucion hallada en %d iteraciones es c=%15.12f\n",i,c)

break

end

i=i+1;

if (FA*FC>0)

a=c;

FA=FC;

else

b=c;

21
end

end

if (i>N)

fprintf("\nEl metodo fallo después de %d iteraciones...\n",N)

end

end

end

function [y]=f(x)

t0=0;

x0=1;

y0=x;

y=RK4SysDisparos(t0,x0,y0,0.001,10000)-20;

end

function traza(a, b, n)

incX=(b-a)/n;

for i=1:n+1

x(i)=a+(i-1)*incX;

y(i)=f(x(i));

ejeX(i)=0;

end

clf()

plot(x,y,x,ejeX)

end

function [sal]=RK4SysDisparos(t0,x0,y0,h,n)

t(1)=t0;

x(1)=x0;

y(1)=y0;

for k=1:n

t(k+1)=t(k)+h;

[x(k+1),y(k+1)]=metRk4Sys(t(k),x(k),y(k),h);

end %cierra el for k

22
sal=x(n+1);

end %termina la funcion principal

function [xSal,ySal]=metRk4Sys(t0,x0,y0,h)

k1=f1(t0,x0,y0);

M1=f2(t0,x0,y0);

k2=f1(t0+h/2,x0+(h*k1)/2,y0+(h*M1)/2);

M2=f2(t0+h/2,x0+(h*k1)/2,y0+(h*M1)/2);

k3=f1(t0+h/2,x0+(h*k2)/2,y0+(h*M2)/2);

M3=f2(t0+h/2,x0+(h*k2)/2,y0+(h*M2)/2);

k4=f1(t0+h,x0+h*k3,y0+h*M3);

M4=f2(t0+h,x0+h*k3,y0+h*M3);

xSal=x0+(h/6)*(k1+2*k2+2*k3+k4);

ySal=y0+(h/6)*(M1+2*M2+2*M3+M4);

end

function [x1]=f1(t,x,y)

x1=y;

end

function [y1]=f2(t,x,y)

%y1=x-x^3-y;

y1=(1-x^2)*y-x;

end

23
Método de diferencias finitas

function [t,x]=difFinitasExamen(t0,x0,tf,xf,n)

more off

dx=(tf-t0)/n;

k0=16*dx-dx^2;

k1=-32*dx-16*dx^3;

k2=16*dx+dx^2;

A=tridiag(k0,k1,k2,n);

b=zeros(n-1,1);

b(1,1)=x0;

for k=2:n-1

tk=t0+(k-1)*dx;

b(k,1)=16*dx^3*(4+tk^3/4);

end

b(n,1)=xf;

x=A\b;

fprintf('\n')

fprintf('%4s %12s %12s\n','#','t(k)','x(k)');

for k=1:n

t(k)=t0+(k-1)*dx;

fprintf('%4d %12.8f %12.8f\n',k,t(k),x(k))

end

fprintf('\n')

clf()

hold on

plot(t,x)

plot(t,x,'*;difFinitas;')

end

function [A]=tridiag(lower,diag,upper,n)

A=eye(n,n);

for k=2:n-1

A(k,k)=diag;

end

24
for k=2:n-1

A(k,k-1)=lower;

A(k,k+1)=upper;

end

end

25

También podría gustarte