Clase 5 Ecuaciones diferenciales ordinarias

Método de Runge-Kutta de cuarto orden (RK4) escalar

Este método se expresa como:

yi+1=yi+16(k1+2k2+2k3+k4)k1=hf(xi,yi)k2=hf(xi+12h,yi+12k1)k3=hf(xi+12h,yi+12k2)k4=hf(xi+h,yi+k3)xi+1=xi+h

Se resuelve el problema de prueba y′=x+2xy, y(0)=1, en x∈[0,1] con h=0.1, y al final se compara con la solución exacta y(x)=3ex2−12.

Celda
import numpy as np

#Método de RK4 escalar

#Función 
def f(x,y):
    return x + 2*x*y

#Datos del problema
h = 0.1
x0 = 0
y0 = 1
xn = 1
n = int(np.round((xn - x0)/h,0))

for i in range(n):
    k1 = h*f(x0, y0)
    k2 = h*f(x0 + (1/2)*h, y0 + (1/2)*k1)
    k3 = h*f(x0 + (1/2)*h, y0 + (1/2)*k2)
    k4 = h*f(x0 + h, y0 + k3)
    y1 = y0 + (1/6)*(k1 + 2*k2 + 2*k3 + k4)
    x1 = x0 + h

    x0 = x1
    y0 = y1
    
    print(f"{i+1}.- k1 = {k1:.4f}, k2 = {k2:.4f}, k3 = {k3:.4f}, k4 = {k4:.4f}, x{i+1} = {x1:.4f}, y{i+1} = {y1:.4f}")
    
print(f"\nEl resultado con RK4 es y{n} = {y1:.4f}, y se obtuvo con {i+1} iteraciones.")

#Comparación con la solución exacta
y_exacta = (3*np.exp(xn**2) - 1)/2
print(f"La solución exacta es y({xn}) = {y_exacta:.4f}, por lo que el error es {abs(y_exacta - y1):.4e}.")
1.- k1 = 0.0000, k2 = 0.0150, k3 = 0.0151, k4 = 0.0303, x1 = 0.1000, y1 = 1.0151
2.- k1 = 0.0303, k2 = 0.0459, k3 = 0.0461, k4 = 0.0624, x2 = 0.2000, y2 = 1.0612
3.- k1 = 0.0624, k2 = 0.0796, k3 = 0.0801, k4 = 0.0985, x3 = 0.3000, y3 = 1.1413
4.- k1 = 0.0985, k2 = 0.1183, k3 = 0.1190, k4 = 0.1408, x4 = 0.4000, y4 = 1.2603
5.- k1 = 0.1408, k2 = 0.1648, k3 = 0.1658, k4 = 0.1926, x5 = 0.5000, y5 = 1.4260
6.- k1 = 0.1926, k2 = 0.2225, k3 = 0.2241, k4 = 0.2580, x6 = 0.6000, y6 = 1.6500
7.- k1 = 0.2580, k2 = 0.2963, k3 = 0.2988, k4 = 0.3428, x7 = 0.7000, y7 = 1.9485
8.- k1 = 0.3428, k2 = 0.3930, k3 = 0.3967, k4 = 0.4552, x8 = 0.8000, y8 = 2.3447
9.- k1 = 0.4552, k2 = 0.5223, k3 = 0.5280, k4 = 0.6071, x9 = 0.9000, y9 = 2.8719
10.- k1 = 0.6069, k2 = 0.6983, k3 = 0.7070, k4 = 0.8158, x10 = 1.0000, y10 = 3.5774

El resultado con RK4 es y10 = 3.5774, y se obtuvo con 10 iteraciones.
La solución exacta es y(1) = 3.5774, por lo que el error es 1.7480e-05.

Comparación del orden de RK2 y RK4

Con el mismo tamaño de paso, RK4 es mucho más exacto que RK2. Además, el error global de un método de orden p es proporcional a hp: si h se reduce a la mitad, el error de RK2 se reduce aproximadamente 4 veces (22) y el de RK4 aproximadamente 16 veces (24). Para comprobarlo, encapsulamos ambos métodos en funciones y calculamos el error en x=1 con distintos valores de h.

Celda
def y_real(x):
    return (3*np.exp(x**2) - 1)/2

def rk2_escalar(f, x0, y0, xn, h):
    n = int(np.round((xn-x0)/h,0))
    for i in range(n):
        k1 = h*f(x0, y0)
        k2 = h*f(x0 + h, y0 + k1)
        y0 = y0 + (1/2)*(k1 + k2)
        x0 = x0 + h
    return y0

def rk4_escalar(f, x0, y0, xn, h):
    n = int(np.round((xn-x0)/h,0))
    for i in range(n):
        k1 = h*f(x0, y0)
        k2 = h*f(x0 + 0.5*h, y0 + 0.5*k1)
        k3 = h*f(x0 + 0.5*h, y0 + 0.5*k2)
        k4 = h*f(x0 + h, y0 + k3)
        y0 = y0 + (1/6)*(k1 + 2*k2 + 2*k3 + k4)
        x0 = x0 + h
    return y0

e2_ant = None
e4_ant = None
print("    h     |  error RK2  | cociente |  error RK4  | cociente")
for h in [0.1, 0.05, 0.025, 0.0125]:
    e2 = abs(y_real(1) - rk2_escalar(f, 0, 1, 1, h))
    e4 = abs(y_real(1) - rk4_escalar(f, 0, 1, 1, h))
    c2 = f"{e2_ant/e2:8.2f}" if e2_ant else "    -   "
    c4 = f"{e4_ant/e4:8.2f}" if e4_ant else "    -   "
    print(f" {h:<8} |  {e2:.3e}  | {c2} |  {e4:.3e}  | {c4}")
    e2_ant = e2
    e4_ant = e4
    h     |  error RK2  | cociente |  error RK4  | cociente
 0.1      |  1.384e-02  |     -    |  1.748e-05  |     -
 0.05     |  3.438e-03  |     4.02 |  1.117e-06  |    15.65
 0.025    |  8.551e-04  |     4.02 |  7.039e-08  |    15.87
 0.0125   |  2.131e-04  |     4.01 |  4.413e-09  |    15.95