Clase 5 Ecuaciones diferenciales ordinarias

Método RK4 vectorial

x→i+1=x→i+16(k→1+2k→2+2k→3+k→4)k→1=hf→(ti;x→i)k→2=hf→(ti+12h;x→i+12k→1)k→3=hf→(ti+12h;x→i+12k→2)k→4=hf→(ti+h;x→i+k→3)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 RK4 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 + (1/2)*h, x0 + (1/2)*k1)
    k3 = h*f(t0 + (1/2)*h, x0 + (1/2)*k2)
    k4 = h*f(t0 + h, x0 + k3)
    x1 = x0 + (1/6)*(k1 + 2*k2 + 2*k3 + k4)
    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" k3 = {np.round(k3,4)}")
    print(f" k4 = {np.round(k4,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.1395 0.691 ]
 k3 = [0.0349 0.1727 0.7028]
 k4 = [0.0864 0.3514 0.7048]
 x1 = [2.026  0.1626 0.6751]
 t1 = 1.5

Iteración 2:
 k1 = [0.0813 0.3375 0.7015]
 k2 = [0.1657 0.5129 0.581 ]
 k3 = [0.2095 0.4828 0.5785]
 k4 = [0.3227 0.6268 0.317 ]
 x2 = [2.2184 0.6553 1.2313]
 t2 = 2.0

Iteración 3:
 k1 = [0.3276 0.6157 0.3273]
 k2 = [0.4815 0.6975 0.0408]
 k3 = [0.502  0.6258 0.0091]
 k4 = [ 0.6405  0.6202 -0.031 ]
 x3 = [2.7077 1.3023 1.2973]
 t3 = 2.5

Iteración 4:
 k1 = [ 0.6512  0.6486 -0.0338]
 k2 = [0.8133 0.6402 0.2872]
 k3 = [0.8112 0.7205 0.334 ]
 k4 = [1.0114 0.8157 0.2389]
 x4 = [3.5263 1.9999 1.5386]
 t4 = 3.0

Iteración 5:
 k1 = [1.     0.7693 0.233 ]
 k2 = [ 1.1923  0.8275 -0.0749]
 k3 = [ 1.2069  0.7506 -0.0343]
 k4 = [1.3753 0.7521 0.1787]
 x5 = [4.7218 2.7795 1.5708]
 t5 = 3.5

Iteración 6:
 k1 = [1.3898 0.7854 0.1859]
 k2 = [ 1.5861  0.8319 -0.014 ]
 k3 = [1.5977 0.7819 0.0494]
 k4 = [ 1.7807  0.8101 -0.2146]
 x6 = [6.3115 3.5834 1.5778]
 t6 = 4.0