-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpwa_dsfunc.m
99 lines (83 loc) · 2.53 KB
/
pwa_dsfunc.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
92
93
94
95
96
97
98
99
function [sys,x0,str,ts] = pwa_sfunc(t,x,u,flag,pwadsys,SimParam)
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts]=mdlInitializeSizes(pwadsys,SimParam);
%%%%%%%%%%%%%%%
% Update %
%%%%%%%%%%%%%%%
case 2,
sys=mdlUpdate(t,x,u,pwadsys,SimParam);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys=mdlOutputs(t,x,u);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case { 1, 4, 9 },
sys = [];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end pwa_dsfunc
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
function [sys,x0,str,ts]=mdlInitializeSizes(pwadsys,SimParam)
n = length(pwadsys.Abar{1})-1;
m = size(pwadsys.Bbar{1},2);
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = n;
sizes.NumOutputs = n;
sizes.NumInputs = m;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = SimParam.x0;
str = [];
ts = [pwadsys.Ts 0]; % Sample period of Ts seconds
% end mdlInitializeSizes
%
%=============================================================================
% mdlUpdate
% Return the update for the discrete states.
%=============================================================================
%
function sys=mdlUpdate(t,x,u,pwadsys,SimParam)
xbar = [x;1];
if strcmp(lower(SimParam.sys),'linear'),
Ri = pwa_region(pwadsys.xcl,pwadsys);
sysbar = pwadsys.Abar{Ri}*xbar+pwadsys.Bbar{Ri}*u;
sys = sysbar(1:end-1);
elseif strcmp(lower(SimParam.sys),'pwa'),
Ri = pwa_region(x,pwadsys);
if isnan(Ri),
set_param(gcs,'SimulationCommand','stop');
sys=0*x;
else
sysbar = pwadsys.Abar{Ri}*xbar+pwadsys.Bbar{Ri}*u;
sys = sysbar(1:end-1);
end
else
error('SimParam.sys is undefined.');
end
% end mdlUpdate
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
sys = x;
% end mdlOutputs