Clase 5 Ecuaciones diferenciales ordinarias

Método RK2 vectorial

x→i+1=x→i+12(k→1+k→2)k→1=hf→(ti;x→i)k→2=hf→(ti+h;x→i+k→1)ti+1=ti+h

Se resuelve el problema de prueba de tercer orden

3tx⃛−e−tx¨−x˙−sin⁡(t)x+4cos⁡(tx)=0,x(1)=2,x˙(1)=0,x¨(1)=0

en t∈[1,4] con h=0.5, escrito en forma vectorial:

x→˙=[x2x313t(e−tx3+x2+sin⁡(t)x1−4cos⁡(tx1))],x→0=[200]
Celda
import numpy as np

#Método RK2 vectorial

#Función
def f(t, x):
    x1 = x[0]
    x2 = x[1]
    x3 = x[2]
    return np.array([x2, x3, (1/(3*t))*(np.exp(-t)*x3 + x2 + np.sin(t)*x1 - 4*np.cos(t*x1))])

#Datos del problema
h = 0.5
t0 = 1
x0 = np.array([2.0, 0.0, 0.0])
tn = 4
n = int(np.round((tn-t0)/h,0))

for i in range(n):
    k1 = h*f(t0, x0)
    k2 = h*f(t0 + h, x0 + k1)
    x1 = x0 + (1/2)*(k1 + k2)
    t1 = t0 + h

    t0 = t1
    x0 = x1

    print(f"Iteración {i+1}:")
    print(f" k1 = {np.round(k1,4)}")
    print(f" k2 = {np.round(k2,4)}")
    print(f" x{i+1} = {np.round(x1,4)}")
    print(f" t{i+1} = {np.round(t1,4)} \n")
Iteración 1:
 k1 = [0.     0.     0.5579]
 k2 = [0.     0.279  0.6755]
 x1 = [2.     0.1395 0.6167]
 t1 = 1.5

Iteración 2:
 k1 = [0.0697 0.3084 0.6924]
 k2 = [0.2239 0.6546 0.3896]
 x2 = [2.1468 0.6209 1.1577]
 t2 = 2.0

Iteración 3:
 k1 = [0.3105 0.5789 0.363 ]
 k2 = [ 0.5999  0.7604 -0.0777]
 x3 = [2.602  1.2906 1.3004]
 t3 = 2.5

Iteración 4:
 k1 = [ 0.6453  0.6502 -0.0632]
 k2 = [0.9704 0.6186 0.3478]
 x4 = [3.4099 1.925  1.4427]
 t4 = 3.0

Iteración 5:
 k1 = [0.9625 0.7214 0.2917]
 k2 = [1.3232 0.8672 0.2306]
 x5 = [4.5527 2.7193 1.7039]
 t5 = 3.5

Iteración 6:
 k1 = [1.3596 0.8519 0.2415]
 k2 = [ 1.7856  0.9727 -0.0507]
 x6 = [6.1253 3.6316 1.7993]
 t6 = 4.0