复制代码
1
2
3
4使用MATLAB的S-function,可以在simulink里使用S-function模块,然后里面是调用一个m代码文件,可以任意命名。 MATLAB给的模板文件叫sfuntmpl.m,一般可以用edit sfuntmpl打开,不行的话打开安装目录toolboxsimulinkblocks里面就有sfuntmpl.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251function [sys,x0,str,ts,simStateCompliance] = sfuntmpl(t,x,u,flag) %SFUNTMPL General MATLAB S-Function Template % With MATLAB S-functions, you can define you own ordinary differential % equations (ODEs), discrete system equations, and/or just about % any type of algorithm to be used within a Simulink block diagram. % % MATLAB S-function 的语法是: % [SYS,X0,STR,TS,SIMSTATECOMPLIANCE] = SFUNC(T,X,U,FLAG,P1,...,Pn) % % SFUNC在给定的时间点T的返回值取决于FLAG的值,当下的状态向量, X % 和当下的输入,U % % FLAG RESULT 描述 % ----- ------ -------------------------------------------- % 0 [SIZES,X0,STR,TS] 初始化,包含系统的各种Size的信息在SYS里 % 初始状态信息在X0里,状态的命令在STR里 % 采样时间在TS里. % 1 DX 返回SYS里的连续状态变量微分 % 2 DS 更新离散的状态SYS = X(n+1) % 3 Y 返回SYS的输出. % 4 TNEXT 根据不同采样时间返回下一个系统的时间点 % % 5 保留未定义 (root finding). % 9 [] 终止, 清理现场SYS=[]. % % % 状态向量 X 和 X0 包括连续状态变量和离散状态变量 % % 可选参数, P1,...,Pn 可以供 S-function 使用and % 也可以在任意FLAG条件下使用 % % 当 SFUNC 在FLAG = 0情况调用, 函数要输出以下信息,也就是你写程序要输入的信息: % % SYS(1) = 连续状态变量个数 % SYS(2) = 离散状态变量个数 % SYS(3) = 输出变量个数 % SYS(4) = 输入U的个数 % 这前四个任意一个都可以被定义为-1,-1就意味着他们的维度是动态的 % 在其它FLAG的值里调用的时候它的长度就等于输入U的维度 % SYS(5) = 为 root finding(可能和系统零极点有关)保留. 定义为0. % SYS(6) = 直接转移标志 flag (1=yes, 0=no). 如果 s-function % 在 FLAG=3 调用过程中有直接转移项会用到. 把这项置零那么在FLAG=3的过程中不会有U, % 如果你不遵守这条,置零还用了U,结果将不符预期。 % SYS(7) = 采样时间的个数. 代表TS里的行数。 % % % X0 = 初始状态 或者 [] 代表没有状态变量. % % STR = 状态命令字符,通常定义为 []. % % TS = m-乘-2 矩阵 包含采样时间(周期, 偏移量) m=采样时间个数 % TS的格式一定是: % % TS = [0 0, : 连续采样时间 % 0 1, : 连续,但是在某个步骤修改的采样时间 % % PERIOD OFFSET, : Discrete sample time where % PERIOD > 0 & OFFSET < PERIOD. % -2 0]; : Variable step discrete sample time % where FLAG=4 is used to get time of % next hit. % % 采样时间可以不止一个,他们可以单调递增。 % 当你使用超过1个采样时间时,你要确定 % abs(round((T-OFFSET)/PERIOD) - (T-OFFSET)/PERIOD) % 小于容差,一般是 1e-8. 容差大小取决于采样和仿真次数。 % % % 你也可以通过继承驱动模块里的采样时间。 % 对于在小步骤里改变的采样时间,这个操作要 % 定义 SYS(7) = 1 和 TS = [-1 0]. % 对于在小步骤里保持的采样时间,定义 % SYS(7) = 1 和TS = [-1 1]. % % SIMSTATECOMPLIANCE = 声明如何存储整个仿真的状态 % % 它的值可以是: 'DefaultSimState', % 'HasNoSimState' or 'DisallowSimState'. 如果没声明,它就会被置为'UknownSimState'. % Copyright 1990-2010 The MathWorks, Inc. % % The following outlines the general structure of an S-function. % switch flag, %%%%%%%%%%%%%%%%%% % 初始化 % %%%%%%%%%%%%%%%%%% case 0, [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes; %%%%%%%%%%%%%%% % 微分 % %%%%%%%%%%%%%%% case 1, sys=mdlDerivatives(t,x,u); %%%%%%%%%% % 更新 % %%%%%%%%%% case 2, sys=mdlUpdate(t,x,u); %%%%%%%%%%% % 输出 % %%%%%%%%%%% case 3, sys=mdlOutputs(t,x,u); %%%%%%%%%%%%%%%%%%%%%%% % 获得下个时间点 % %%%%%%%%%%%%%%%%%%%%%%% case 4, sys=mdlGetTimeOfNextVarHit(t,x,u); %%%%%%%%%%%%% % 终止 % %%%%%%%%%%%%% case 9, sys=mdlTerminate(t,x,u); %%%%%%%%%%%%%%%%%%%% % 预期之外的 flags % %%%%%%%%%%%%%%%%%%%% otherwise DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag)); end % end sfuntmpl % %============================================================================= % mdlInitializeSizes % Return the sizes, initial conditions, and sample times for the S-function. %============================================================================= % function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes % % call simsizes for a sizes structure, fill it in and convert it to a % sizes array. % % Note that in this example, the values are hard coded. This is not a % recommended practice as the characteristics of the block are typically % defined by the S-function parameters. %这里定义输入变量个数,输出变量个数,直接输出与否,采样时间个数 sizes = simsizes; sizes.NumContStates = 0;%连续状态变量个数 sizes.NumDiscStates = 0;%离散状态变量个数 sizes.NumOutputs = 0;%输出状态变量个数 sizes.NumInputs = 0;%输入状态变量个数 sizes.DirFeedthrough = 1;%直接输出与否 sizes.NumSampleTimes = 1; % 至少一个采样时间 sys = simsizes(sizes); % % initialize the initial conditions % x0 = [];%这里定义变量初始值 % % str is always an empty matrix % str = []; % % initialize the array of sample times % ts = [0 0];%见上面对ts的说明 % Specify the block simStateCompliance. The allowed values are: % 'UnknownSimState', < 默认设置 % 'DefaultSimState', < 和built-in block一样 % 'HasNoSimState', < 没有仿真状态 % 'DisallowSimState' < 储存状态出错 simStateCompliance = 'UnknownSimState'; % end mdlInitializeSizes % %============================================================================= % mdlDerivatives % Return the derivatives for the continuous states. %============================================================================= % function sys=mdlDerivatives(t,x,u) sys = [];%这里写系统微分方程,比如sys = [0 0 1 0;0 0 0 1;-(K+K2)/M K2/M -B/M 0;K2/m2 -K2/m2 0 0]*x+[0;0;1/M;0]*u; % end mdlDerivatives % %============================================================================= % mdlUpdate % Handle discrete state updates, sample time hits, and major time step % requirements. %============================================================================= % function sys=mdlUpdate(t,x,u) sys = []; % end mdlUpdate % %============================================================================= % mdlOutputs % Return the block outputs. %============================================================================= % function sys=mdlOutputs(t,x,u) sys = [];%系统输出,比如sys=x(1);输出第一个变量 % end mdlOutputs % %============================================================================= % mdlGetTimeOfNextVarHit % Return the time of the next hit for this block. Note that the result is % absolute time. Note that this function is only used when you specify a % variable discrete-time sample time [-2 0] in the sample time array in % mdlInitializeSizes. %============================================================================= % function sys=mdlGetTimeOfNextVarHit(t,x,u) sampleTime = 1; % Example, set the next hit to be one second later. sys = t + sampleTime; % end mdlGetTimeOfNextVarHit % %============================================================================= % mdlTerminate % Perform any end of simulation tasks. %============================================================================= % function sys=mdlTerminate(t,x,u) sys = []; % end mdlTerminate
最后
以上就是舒心冬日最近收集整理的关于Matlab S-function详解的全部内容,更多相关Matlab内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复