Last active
February 24, 2022 09:12
-
-
Save danielmk/d0e2d81383876d5ab9866eae54d0f006 to your computer and use it in GitHub Desktop.
odeint and solve_ivp results diverge for yet unknown reasons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.integrate import odeint, solve_ivp | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.mplot3d import Axes3D | |
def lorenz(t, state, sigma, beta, rho): | |
x, y, z = state | |
dx = sigma * (y - x) | |
dy = x * (rho - z) - y | |
dz = x * y - beta * z | |
return [dx, dy, dz] | |
sigma = 10.0 | |
beta = 8.0 / 3.0 | |
rho = 28.0 | |
p = (sigma, beta, rho) # Parameters of the system | |
y0 = [1.0, 1.0, 1.0] # Initial state of the system | |
t_span = (0.0, 40.0) | |
t = np.arange(0.0, 40.0, 0.01) | |
result_odeint = odeint(lorenz, y0, t, p, tfirst=True) | |
result_solve_ivp = solve_ivp(lorenz, t_span, y0, args=p, | |
method='LSODA', dense_output=True) | |
fig = plt.figure() | |
ax = fig.add_subplot(1, 2, 1, projection='3d') | |
ax.plot(result_odeint[:, 0], | |
result_odeint[:, 1], | |
result_odeint[:, 2]) | |
ax.set_title("odeint") | |
ax = fig.add_subplot(1, 2, 2, projection='3d') | |
ax.plot(result_solve_ivp.sol(t)[0], | |
result_solve_ivp.sol(t)[1], | |
result_solve_ivp.sol(t)[2]) | |
ax.set_title("solve_ivp LSODA") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment