-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_sysid.m
96 lines (76 loc) · 4.06 KB
/
example_sysid.m
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
% example_sysid.m
%
% This script trains a linear, bilinear, and nonlinear Koopman model from
% the included 'arm-3link-markers-noload-50trials_train-10_val-5.mat' data file,
% then generate a plot showing the predictions generated by each model compared to
% a single validation trial. You can change the parameters of the model(s)
% generated by modifying the Name/Value arguments into Ksysid each time it is
% called.
clear Ksysid_linear Ksysid_bilinear Ksysid_nonlinear;
%% gather training data (need to prepare data file before running this)
% load in training and validation data for 3-link planar arm
datafile_path = ['datafiles' , filesep , 'arm-3link-markers-noload-50trials_train-10_val-5.mat'];
data4sysid = load( datafile_path );
%% identify Koopman realizations
% linear realization
Ksysid_linear = Ksysid( data4sysid ,...
'model_type' , 'linear' ,... % model type (linear, bilinear, or nonlinear)
'time_type' , 'discrete' , ... % 'discrete' or 'continuous'
'obs_type' , { 'poly' } ,... % type of basis functions
'obs_degree' , [ 3 ] ,... % "degree" of basis functions
'snapshots' , Inf ,... % Number of snapshot pairs (Inf to use all available data)
'lasso' , [ Inf ] ,... % L1 regularization term (Inf for least-squares sol.)
'delays' , 0 ,... % Numer of state/input delays
'dim_red' , true); % Should dimensional reduction be performed?
Ksysid_linear = Ksysid_linear.train_models;
% bilinear realization
Ksysid_bilinear = Ksysid( data4sysid ,...
'model_type' , 'bilinear' ,... % model type (linear, bilinear, or nonlinear)
'time_type' , 'discrete' , ... % 'discrete' or 'continuous'
'obs_type' , { 'poly' } ,... % type of basis functions
'obs_degree' , [ 3 ] ,... % "degree" of basis functions
'snapshots' , Inf ,... % Number of snapshot pairs (Inf to use all available data)
'lasso' , [ Inf ] ,... % L1 regularization term (Inf for least-squares sol.)
'delays' , 0 ,... % Numer of state/input delays
'dim_red' , true); % Should dimensional reduction be performed?
Ksysid_bilinear = Ksysid_bilinear.train_models;
% nonlinear realization
Ksysid_nonlinear = Ksysid( data4sysid ,...
'model_type' , 'nonlinear' ,... % model type (linear, bilinear, or nonlinear)
'time_type' , 'discrete' , ... % 'discrete' or 'continuous'
'obs_type' , { 'poly' } ,... % type of basis functions
'obs_degree' , [ 3 ] ,... % "degree" of basis functions
'snapshots' , Inf ,... % Number of snapshot pairs (Inf to use all available data)
'lasso' , [ Inf ] ,... % L1 regularization term (Inf for least-squares sol.)
'delays' , 0 ,... % Numer of state/input delays
'dim_red' , true); % Should dimensional reduction be performed?
Ksysid_nonlinear = Ksysid_nonlinear.train_models;
%% plot model predictions vs. validation data
val_data = Ksysid_linear.valdata{1}; % select a validation trial
% simulate model realizations
results_linear = Ksysid_linear.val_model( Ksysid_linear.model , val_data );
results_bilinear = Ksysid_bilinear.val_BLmodel( Ksysid_bilinear.model , val_data );
results_nonlinear = Ksysid_nonlinear.val_NLmodel( Ksysid_nonlinear.model , val_data );
% plot the results (just end effector position)
figure;
subplot(2,1,1) % x-coordinate
hold on;
plot( val_data.t , val_data.y(:,5) );
plot( val_data.t , results_linear.sim.y(:,5) )
plot( val_data.t , results_bilinear.sim.y(:,5) )
plot( val_data.t , results_nonlinear.sim.y(:,5) )
hold off;
xlabel('Time (s)')
ylabel('x (normalized)')
grid on;
subplot(2,1,2) % y-coordinate
hold on;
plot( val_data.t , val_data.y(:,6) );
plot( val_data.t , results_linear.sim.y(:,6) )
plot( val_data.t , results_bilinear.sim.y(:,6) )
plot( val_data.t , results_nonlinear.sim.y(:,6) )
hold off;
xlabel('Time (s)')
ylabel('y (normalized)')
grid on;
legend({ 'Ground truth' , 'Linear model' , 'Bilinear model' , 'Nonlinear model' });