linear 1D oscillator w damping, realizations with stochastic parameters
In [7]:
import sklearn
import numpy as np
from numpy import linalg as LA
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import seaborn as sb
%matplotlib inline
In [301]:
Nrealiz = 50
Nt = 200
T = 20
t_ = np.linspace(0,T,Nt) # time grid
K = 10 ## omega0 = sqrt(k/m)
C = 0.2
sigk = 2.0
sigc = 0.0
# state: u = [x, xdot]
x0 = 1
xdot0 = 0.0
#u0 = np.array([x0,xdot0]) # initial condition
In [304]:
def lin_damp_osc_stoch(k0,c0,x0,xdot0,sigk,sigc,t_):
def randn1(mu,sig,N):
return mu + sig*np.random.randn(N)
def randu1(mu,sig,N):
return mu + sig*(np.random.rand(N) -1/2)
k = randu1(k0,sigk,1)
c = randu1(c0,sigc,1)
A = np.array([[0,1],[-k,-c]]) # system mtx
def f(u,t):
return A.dot(u)
soln = odeint(f, u0, t_)
xpos = soln[:, 0]
xvel = soln[:, 1]
return xpos
In [305]:
xpos = [lin_damp_osc_stoch(K,C,x0,xdot0,sigk,sigc,t_) for _ in range(Nrealiz)]
#xpos = x[:,1]
# plt.figure()
# ax = plt.subplot(111)
cis = np.linspace(95,10,4)
# sb.tsplot(xpos,t_, err_style="ci_band", ci=cis)
# sb.tsplot(xpos, t_, estimator=np.mean, err_style='unit_traces')
sb.set_style("white")
sb.tsplot(xpos, t_, estimator=np.mean, err_style='unit_traces', color="muted", linewidth=0, err_kws={"alpha": .05})
plt.plot(t_,xpos[2],'r')
# sb.tsplot(xpos, err_style='ci_bars')
# plt.figure()
# plt.plot(t_,xpos[2])
Out[305]:
In [ ]:
np.std