In [1]:
%matplotlib inline
In [2]:
from scipy.integrate import odeint
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
In [3]:
# System parameters (determined by system definition)
# NOTE: These are not necessarily correct for your case
alpha = 0.5
beta = 1.4
gamma = 0.1
"""
func accepts a vector of variables l_vec and returns a
vector of derivatives with respect to t
here, v is defined as dv/dt
"""
def func(l_vec,t):
l,v = l_vec
return [v,(alpha/l)-beta-(gamma*v)]
# Initial conditions (units?)
# NOTE: These are not necessarily correct for your case
l_0 = 0.1
v_0 = 0.1
y0 = [l_0,v_0] # initial conditions vector
t_output = np.arange(0,50,0.1) # get vector of t from 0 to 50, with 0.1 step
y_result = odeint(func,y0,t_output)
# y_result is the vector of values, starting from y0, integrated according to our constitutive function, func
In [4]:
# Plot setup, 10 by 6
fig = plt.figure(figsize=(10,6))
# 111 is a layout indicator; only need to worry if we have multiple subplots
ax = fig.add_subplot(111)
# unpack the values from our solution
ll,vv = y_result.T
# plot the figure and set the title and labels
ax.plot(t_output,ll)
ax.set_title('Piston Time Evolution')
ax.set_xlabel('Time (seconds)')
ax.set_ylabel('Length (m)')
Out[4]:
In [4]:
In [4]: