1515# Nonlinear system
1616# ----------------
1717#
18- # `Duffing equation <https://en.wikipedia.org/wiki/Duffing_equation>` is used to
18+ # `Duffing equation <https://en.wikipedia.org/wiki/Duffing_equation>`_ is used to
1919# generate simulated data. The mathematical model is given by
2020#
2121# .. math::
@@ -82,15 +82,18 @@ def auto_duffing_equation(y, t):
8282dur = 10
8383n_samples = 1000
8484
85+ rng = np .random .default_rng (12345 )
86+ e_train = rng .normal (0 , 0.0002 , n_samples )
87+ e_test = rng .normal (0 , 0.0002 , n_samples )
8588t = np .linspace (0 , dur , n_samples )
8689
8790sol = odeint (duffing_equation , [0.6 , 0.8 ], t )
8891u_train = 2.5 * np .cos (2 * np .pi * t ).reshape (- 1 , 1 )
89- y_train = sol [:, 0 ]
92+ y_train = sol [:, 0 ] + e_train
9093
91- sol = odeint (auto_duffing_equation , [0.6 , - 0.8 ], t )
94+ sol = odeint (duffing_equation , [0.6 , - 0.8 ], t )
9295u_test = 2.5 * np .cos (2 * np .pi * t ).reshape (- 1 , 1 )
93- y_test = sol [:, 0 ]
96+ y_test = sol [:, 0 ]+ e_test
9497
9598# %%
9699# One-step-head VS. multi-step-ahead NARX
@@ -105,12 +108,12 @@ def auto_duffing_equation(y, t):
105108
106109from fastcan .narx import make_narx
107110
108- max_delay = 2
111+ max_delay = 3
109112
110113narx_model = make_narx (
111114 X = u_train ,
112115 y = y_train ,
113- n_terms_to_select = 10 ,
116+ n_terms_to_select = 5 ,
114117 max_delay = max_delay ,
115118 poly_degree = 3 ,
116119 verbose = 0 ,
@@ -130,7 +133,7 @@ def plot_prediction(ax, t, y_true, y_pred, title):
130133y_train_osa_pred = narx_model .predict (u_train , y_init = y_train [:max_delay ])
131134y_test_osa_pred = narx_model .predict (u_test , y_init = y_test [:max_delay ])
132135
133- narx_model .fit (u_train , y_train , coef_init = "one_step_ahead" , method = "Nelder-Mead" )
136+ narx_model .fit (u_train , y_train , coef_init = "one_step_ahead" )
134137y_train_msa_pred = narx_model .predict (u_train , y_init = y_train [:max_delay ])
135138y_test_msa_pred = narx_model .predict (u_test , y_init = y_test [:max_delay ])
136139
@@ -159,7 +162,7 @@ def plot_prediction(ax, t, y_true, y_pred, title):
159162narx_model = make_narx (
160163 X = u_all ,
161164 y = y_all ,
162- n_terms_to_select = 10 ,
165+ n_terms_to_select = 5 ,
163166 max_delay = max_delay ,
164167 poly_degree = 3 ,
165168 verbose = 0 ,
@@ -169,7 +172,7 @@ def plot_prediction(ax, t, y_true, y_pred, title):
169172y_train_osa_pred = narx_model .predict (u_train , y_init = y_train [:max_delay ])
170173y_test_osa_pred = narx_model .predict (u_test , y_init = y_test [:max_delay ])
171174
172- narx_model .fit (u_all , y_all , coef_init = "one_step_ahead" , method = "Nelder-Mead" )
175+ narx_model .fit (u_all , y_all , coef_init = "one_step_ahead" )
173176y_train_msa_pred = narx_model .predict (u_train , y_init = y_train [:max_delay ])
174177y_test_msa_pred = narx_model .predict (u_test , y_init = y_test [:max_delay ])
175178
0 commit comments