-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathparametric_oscillator.py
33 lines (29 loc) · 1.08 KB
/
parametric_oscillator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# parametric_oscillator.py
# -------------------------------------------------------------------------
# Define a parametric function that accepts 4 parameters then integrate it
# using odeint.
# -------------------------------------------------------------------------
import numpy as np
from scipy.integrate import odeint
def F(y, t, spring_constant=1.0, mass=1.0):
"""
Return derivatives for harmonic oscillator:
y'' = -(k/m) * y
y = displacement in [m]
k = spring_constant in [N/m]
m = mass in [kg]
"""
dy = [0, 0] # array to store derivatives
dy[0] = y[1]
dy[1] = -(spring_constant/mass) * y[0]
return dy
# -------------------------------------------------------------------------
# Integrate parametric function using two different methods.
# -------------------------------------------------------------------------
y0 = (1.0, 0.0) # initial conditions
t = np.linspace(0, 10, 101) # times at which y(t) will be evaluated
# Method 1 -- dummy function
def G(y, t): return F(y, t, 2.0, 0.5)
yA = odeint(G, y0, t)
# Method 2 -- keywords
yB = odeint(F, y0, t, args=(2.0, 0.5))