s3Eva_IIT2009_T2 Valor inicial Runge-Kutta 4to orden dy/dx

Ejercicio: 3Eva_IIT2009_T2 Valor inicial Runge-Kutta 4to orden dy/dx

La ecuación del problema:

(1-x^2)y' - xy = x (1-x^2)

se despeja:

(1-x^2)y' = x (1-x^2) - xy y' = x - \frac{xy}{(1-x^2)}

con valores iniciales x0 = 0 , y0 = 2, h = 0.1 en el intervalo [0, 0.5]

Usando Runge-Kutta de 2do Orden

Iteración 1

K_1 = h y'(0,2) = (0.1)\Big[0 - \frac{0 (2)}{(1-(0^2))}\Big] = 0 K_2 = h y'(0+0.1, 2 + 0) = (0.1)\Big[0.1 - \frac{0.1 (2+0)}{(1-(0.1^2))}\Big] = 0.0302 y_1= 2 + \frac{0+0.0302}{2} = 2.0151 x_1 = x_0 + h = 0 + 0.1 = 0.1

Iteración 2

K_1 = h y'(0.1,2.0151) = (0.1)\Big[0.1 - \frac{0.1 (2.0151)}{(1-(0.1^2))}\Big] = 0.0304 K_2 = h y'(0.1+0.1, 2.0151 + 0.0304) = (0.1)\Big[0.2 - \frac{0.2 (2.0151 + 0.0304)}{(1-(0.2^2))}\Big] = 0.0626 y_2 = 2.0151 + \frac{0.0304+0.0626}{2} = 2.0616 x_2 = x_1 + h = 0.1 + 0.1 = 0.2

Iteración 3

K_1 = h y'(0.2,2.0616) = (0.1)\Big[0.2 - \frac{0.2 (2.0616)}{(1-(0.2^2))}\Big] = 0.0629 K_2 = h y'(0.2+0.1, 2.0616 + 0.0629) = (0.1)\Big[0.3 - \frac{0.3 (2.0616 + 0.0629)}{(1-(0.3^2))}\Big] = 0.1 y_3 = 2.0151 + \frac{0.0629+0.1}{2} = 2.1431 x_3 = x_2 + h = 0.2 + 0.1 = 0.3

siguiendo el algoritmo se completa la tabla:

 [xi,    yi,    K1,    K2    ]
[[0.     2.     0.     0.    ]
 [0.1    2.0151 0.     0.0302]
 [0.2    2.0616 0.0304 0.0626]
 [0.3    2.1431 0.0629 0.1   ]
 [0.4    2.2668 0.1007 0.1468]
 [0.5    2.4463 0.1479 0.211 ]]

Algoritmo en Python

# 3Eva_IIT2009_T2 Valor inicial Runge-Kutta 4to orden dy/dx
import numpy as np
import matplotlib.pyplot as plt

# INGRESO
d1y = lambda x,y : x + x*y/(1-x**2)
x0 = 0
y0 = 2
h = 0.1
a = 0
b = 1/2

# PROCEDIMIENTO
muestras = int((b -a)/h)+1
tabla = np.zeros(shape=(muestras,4),dtype=float)

i = 0
xi = x0
yi = y0
tabla[i,:] = [xi,yi,0,0]

i = i+1
while not(i>=muestras):
    K1 = h* d1y(xi,yi)
    K2 = h* d1y(xi+h,yi+K1)
    yi = yi + (K1+K2)/2
    xi = xi +h
    tabla[i,:] = [xi,yi,K1,K2]
    i = i+1
# vector para gráfica
xg = tabla[:,0]
yg = tabla[:,1]

# SALIDA
# muestra 4 decimales
np.set_printoptions(precision=4)
print(' [xi, yi, K1, K2]')
print(tabla)

# Gráfica
plt.plot(xg,yg)
plt.xlabel('xi')
plt.ylabel('yi')
plt.grid()
plt.show()

Tarea: Realizar con Runge-Kutta de 4to Orden