-
Notifications
You must be signed in to change notification settings - Fork 5
/
pwa_sfunc.m
114 lines (99 loc) · 2.95 KB
/
pwa_sfunc.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function [sys,x0,str,ts] = pwa_sfunc(t,x,u,flag,nlsys,pwasys,SimParam)
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts]=mdlInitializeSizes(pwasys,SimParam);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDerivatives(t,x,u,nlsys,pwasys,SimParam);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,
sys=mdlOutputs(t,x,u);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case { 2, 4, 9 },
sys = [];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end pwa_sfunc
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
function [sys,x0,str,ts]=mdlInitializeSizes(pwasys,SimParam)
n = length(pwasys.Abar{1})-1;
m = size(pwasys.Bbar{1},2);
sizes = simsizes;
sizes.NumContStates = n;
sizes.NumDiscStates = 0;
sizes.NumOutputs = n;
sizes.NumInputs = m;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = SimParam.x0;
str = [];
ts = [0 0];
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u,nlsys,pwasys,SimParam)
xbar = [x;1];
if strcmp(lower(SimParam.sys),'linear'),
Ri = pwa_region(pwasys.xcl,pwasys);
sysbar = pwasys.Abar{Ri}*xbar+pwasys.Bbar{Ri}*u;
sys = sysbar(1:end-1);
elseif strcmp(lower(SimParam.sys),'pwa'),
Ri = pwa_region(x,pwasys);
if Ri==0,
set_param(gcs,'SimulationCommand','stop');
sys=0*x;
else
sysbar = pwasys.Abar{Ri,1}*xbar+pwasys.Bbar{Ri,1}*u;
sys = sysbar(1:end-1);
end
elseif strcmp(lower(SimParam.sys),'nonlinear'),
if isfield(nlsys,'Parameters'),
Param = nlsys.Parameters; % Set the system parameters
else
Param = [];
end
fx = pwa_nlfun(nlsys,x',Param)';
B = nlsys.B;
try
B = B+0*B;
catch
Bfun.Handle = nlsys.B;
Bfun.xcl = nlsys.xcl;
B = pwa_nlfun(Bfun,x',Param)';
end
sys = nlsys.A*x+nlsys.a+fx+B*u;
else
error('SimParam.sys is undefined.');
end
% end mdlDerivatives
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)
sys = x;
% end mdlOutputs