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
|
function [sys,x0,str,ts] =csfunc(t,x,u,flag)
% CSFUNC An example M-file S-function for defining a syste
% continuous state equations:
% Generate a continuous linear system:
A=[0.09 0.01 1 0];
B=[1 7 0 2];
C=[0 2 1 5];
D=[0;0;0;0];
% Dispatch the flag.
%
switch flag,
case 0
[sys,x0,str,ts]=mdlInitializeSizes(t,x,u,flag); % Initialization
case 1
sys = mdlDerivatives(t,x,u,A,B,C,D); % Calculate derivatives
case 3
sys =mdlOutputs(t,x,u,A,B,C,D); % Calculate outputs
case { 2, 4, 9 } % Unused flags
sys = [];
otherwise
error(['Unhandled flag = ',num2str(flag)]); % Error handling
end
% End of csfunc.
% ==============================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the
% S-function.
% % ==============================================================
%
function [sys,x0,str,ts] =mdlInitializeSizes(t,x,u,flag)
%
% Call simsizes for a sizes structure, fill it in and convert it
% to a sizes array.
%
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1; % Matrix D is nonempty.
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0=(8:10)';
% Initialize the initial conditions.
%
x0 = zeros(2,1);
%
% str is an empty matrix.
%
str = [];
%
% Initialize the array of sample times; in this example the sample
% time is continuous, so set ts to 0 and its offset to 0.
%
ts = [0 0];
% End of mdlInitializeSizes.
%
%==============================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%==============================================================
function sys = mdlDerivatives(t,x,u,A,B,C,D)
sys =A*x+B*u;
% End of mdlDerivatives.
%
%==============================================================
% mdlOutputs
% Return the block outputs.
%==============================================================
%
function sys =mdlOutputs(t,x,u,A,B,C,D)
sys =C*x+D*u;
% End of mdlOutputs. |
Partager