Last active
August 29, 2015 14:12
-
-
Save myjr52/5306730fc71b194e201f to your computer and use it in GitHub Desktop.
integrate ode in Python: ode in separate file
This file contains hidden or 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
def mass_spring_damper(state, t, k_c_set): | |
x, x_dot = state | |
k_spring, c_damper = k_c_set | |
f = [x_dot, | |
-k_spring*x - c_damper*x_dot] | |
return f |
This file contains hidden or 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 functions | |
from numpy import linspace | |
from scipy.integrate import odeint | |
#import pylab as pyl | |
import matplotlib.pyplot as plt | |
# define constants | |
init_cond = [0.3, -0.1] | |
t_init = 0.0 | |
t_final = 10.0 | |
time_step = 0.005 | |
num_data =int((t_final-t_init)/time_step) | |
k_spring = 0.1 | |
c_damper = 0.5 | |
import msd_ode | |
# integrate | |
t_all = linspace(t_init, t_final, num_data) | |
y_all = odeint(msd_ode.mass_spring_damper, init_cond, t_all, | |
args=([k_spring, c_damper],)) | |
# plots | |
fig = plt.figure() | |
plt.plot(t_all,y_all[:,0],'b-') | |
plt.plot(t_all,y_all[:,1],'r--') | |
plt.legend(['x [m]','dx/dt [m/s]']) | |
plt.xlabel('time [s]') | |
plt.ylabel('state') | |
plt.savefig('state.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment