import numpy as np
import matplotlib.pyplot as plt
# Programa #07
#Física Computacional (2:00-3:00 pm)
#Elaborado por Nalleli Iridian Ávila García
#Este programa calcula mediante el método de Runge Kutta la ecuación diferencial dy/dx=2xy
#Bajo las condiciones y(0)=1, en donde x [0,1], con n=5.
# Definir la función
def f(x, y):
return 2*x*y
# Método de Runge-Kutta de cuarto orden
def runge_kutta(x0, y0, xn, n):
h = (xn-x0)/float(n)
x = np.linspace(x0, xn, n+1)
y = np.zeros(n+1)
y[0] = y0
for i in range(n):
k1 = h * f(x[i], y[i])
k2 = h * f(x[i] + 0.5*h, y[i] + 0.5*k1)
k3 = h * f(x[i] + 0.5*h, y[i] + 0.5*k2)
k4 = h * f(x[i] + h, y[i] + k3)
y[i+1] = y[i] + (k1 + 2*k2 + 2*k3 + k4)/6
return x, y
# Parámetros
x0 = 0
y0 = 1
xn = 1
n = 5
# Calcular y trazar la solución
x, y = runge_kutta(x0, y0, xn, n)
plt.plot(x, y, 'o-', label='Runge-Kutta')
plt.title('Solución de f(x,y)=2xy con Runge-Kutta')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True)
plt.show()
# Imprimir los resultados
for i in range(n+1):
print(f"x = {x[i]:.4f}, y = {y[i]:.4f}")
To embed this project on your website, copy the following code and paste it into your website's HTML: