-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Cleanup differential equations examples. #22043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
from numpy import sin, cos | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import scipy.integrate as integrate | ||
import matplotlib.animation as animation | ||
from collections import deque | ||
|
||
|
@@ -22,13 +21,13 @@ | |
L = L1 + L2 # maximal length of the combined pendulum | ||
M1 = 1.0 # mass of pendulum 1 in kg | ||
M2 = 1.0 # mass of pendulum 2 in kg | ||
t_stop = 5 # how many seconds to simulate | ||
t_stop = 2.5 # how many seconds to simulate | ||
history_len = 500 # how many trajectory points to display | ||
|
||
|
||
def derivs(state, t): | ||
|
||
def derivs(t, state): | ||
dydx = np.zeros_like(state) | ||
|
||
dydx[0] = state[1] | ||
|
||
delta = state[2] - state[0] | ||
|
@@ -51,7 +50,7 @@ def derivs(state, t): | |
return dydx | ||
|
||
# create a time array from 0..t_stop sampled at 0.02 second steps | ||
dt = 0.02 | ||
dt = 0.01 | ||
t = np.arange(0, t_stop, dt) | ||
|
||
# th1 and th2 are the initial angles (degrees) | ||
|
@@ -64,8 +63,15 @@ def derivs(state, t): | |
# initial state | ||
state = np.radians([th1, w1, th2, w2]) | ||
|
||
# integrate your ODE using scipy.integrate. | ||
y = integrate.odeint(derivs, state, t) | ||
# integrate the ODE using Euler's method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes the example longer and IMHO a little harder to understand. Does this outweigh the benefit of not depending on scipy in this example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only other use of scipy in the docs can also easily be removed (I have another patch ready for that) so we could entirely drop the dependency on scipy for building the docs, which may or may not be a useful thing (e.g., with the yearly release cycle of cpython, I fear that we're always going to have a small period between having scipy wheels available after the new cpython release, and scipy is significantly harder to build from source (e.g., due to fortran sources) than other dependencies, even numpy). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think dropping SciPy would be pretty helpful if we don't need it. I think its a pretty heavy dependency to carry around for a couple of examples. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have oscillated back and forth on having a scipy documentation dependencies a couple of times. I do not have a strong preference either way. |
||
y = np.empty((len(t), 4)) | ||
y[0] = state | ||
for i in range(1, len(t)): | ||
y[i] = y[i - 1] + derivs(t[i - 1], y[i - 1]) * dt | ||
|
||
# A more accurate estimate could be obtained e.g. using scipy: | ||
# | ||
# y = scipy.integrate.solve_ivp(derivs, t[[0, -1]], state, t_eval=t).y.T | ||
|
||
x1 = L1*sin(y[:, 0]) | ||
y1 = -L1*cos(y[:, 0]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check if this is really necessary. This doubles the number of frames and thus likely the size of the generated file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decreased the end time by a factor of two as well; the shorter time step does appear to improve the accuracy.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But that means that you're only going half the path from before. Please check if that's still a reasonable path length. I'm afraid that might be a bit short.
I can imagine that the shorter time step improves accuracy. But does that matter here? Alternatively, one could also render only every second step, but that adds additional compelity to the code and I'm not sure that's worth it.
Of course, one could also change to another example like a regular single pendulum. That's simpler to calculate and one could have a continuous cyclic animation with, a period of e.g. 2s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did check, the path length is fine.