def NLAR_sim(params, S, S_lag): '''simulate 1 step of a non-linear AR(2) model of speed params = ps, pa, pt, ptv, std ''' ps, pa, pt, ptv, std = params A = (S-S_lag)/dt Tnl = torque_law(S)/damp - S Tnl_lag = torque_law(S_lag)/damp - S_lag Tvar = (Tnl - Tnl_lag)/dt innov = rnd.normal(scale=innov_std, size=S.shape) # dynimical equation: S_next = ps*S + pa*A + pt*Tnl + ptv*Tvar + innov return S_next
# Remove extremal ticks: ax1t.yaxis.get_major_locator().set_params(prune='both') # extremal speed ticks ax2t.yaxis.get_major_locator().set_params(prune='both') # extremal power ticks ax2t.xaxis.get_major_locator().set_params(prune='both') # extremal time ticks ax2tz.xaxis.get_major_locator().set_params(prune='both') # extremal time ticks ### Phase portrait fig = plt.figure('phase portrait') ax = fig.add_subplot(111, title=u'Phase portrait (Ω,a) of the SEAREV\n' 'with leveled viscous torque PTO', xlabel='speed (rad/s)', ylabel=u'accel (rad/s²)') s = np.linspace(-1, 1, 500) ax.plot(s, torque_law(s) / damp, 'r', label=u'T(Ω)/damp') ax.plot(s, s, 'r--', label=u'Ω') ax.plot(speed, accel, '-', lw=0.3) ax.legend(loc='upper right') # 3D phase portrait: #mlab.plot3d(angle, speed, accel, tube_radius=None, opacity=0.3, color=(0,0,1)) # 3D auto-regression plot: #from mayavi import mlab #mlab.points3d(angle[:-1], speed[:-1], speed[1:], mode='point') plt.show()
ar_coef, innov_var = ar_acf_fit(speed, 2, maxlags = ar_fitlags) print('Speed AR(2) model from acf fit on %.0f s:' % (ar_fitlags*dt)) print(' * AR coef: '+ str(ar_coef)) print(' * innovation sd: {:g}'.format(np.sqrt(innov_var))) # speed series simulation: model = {'ar':ar_coef, 'ma':[]} innov_gen = lambda size: np.random.normal(scale = np.sqrt(innov_var), size=size) speed_sim = armaToolbox.arima_sim(model, n_pts, innov_gen = innov_gen) accel_sim = np.zeros(n_pts) accel_sim[:-1] = np.diff(speed_sim)/dt power_sim = speed_sim*torque_law(speed_sim)/1e6 ### State space stochastic model (VAR kind of) ################################# # state variables: X = np.vstack((speed, accel)) p1, p2 = ar_coef # Conversion of AR2 to state-space T_AR2 = np.array([[p1+p2, -dt*p2], [(p1+p2-1)/dt, -p2]]) T = T_AR2 fitlags = 200 def pred_objective(T): '''mean square prediction error at horizon `fitlags`''' T = T.reshape((2,2)) X_prev = X
# Remove extremal ticks: ax1t.yaxis.get_major_locator().set_params(prune='both') # extremal speed ticks ax2t.yaxis.get_major_locator().set_params(prune='both') # extremal power ticks ax2t.xaxis.get_major_locator().set_params(prune='both') # extremal time ticks ax2tz.xaxis.get_major_locator().set_params(prune='both') # extremal time ticks ### Phase portrait fig = plt.figure('phase portrait') ax = fig.add_subplot(111, title=u'Phase portrait (Ω,a) of the SEAREV\n' 'with leveled viscous torque PTO', xlabel='speed (rad/s)', ylabel=u'accel (rad/s²)') s = np.linspace(-1,1, 500) ax.plot(s, torque_law(s)/damp, 'r', label=u'T(Ω)/damp') ax.plot(s, s, 'r--', label=u'Ω') ax.plot(speed, accel, '-', lw=0.3) ax.legend(loc='upper right') # 3D phase portrait: #mlab.plot3d(angle, speed, accel, tube_radius=None, opacity=0.3, color=(0,0,1)) # 3D auto-regression plot: #from mayavi import mlab #mlab.points3d(angle[:-1], speed[:-1], speed[1:], mode='point')