📄 s_xmp2.m
字号:
function [sys,x0,str,ts] = s_xmp2(t,x,u,flag,M,m,l)% S-file example 2% This is an S-file subsystem that models a cart with % inverted pendulum. The cart and pendulum masses and % pendulum length are parameters that must be set in % the block dialog box.%% Based on sfuntmpl.m, supplied with SIMULINK% Copyright (c) 1990-96 by The MathWorks, Inc.%switch flag, case 0, % Initialization [sys,x0,str,ts]=mdlInitializeSizes; case 1, % Compute derivatives of continuous states sys=mdlDerivatives(t,x,u,M,m,l) ; case 2, sys=mdlUpdate(t,x,u); case 3, sys=mdlOutputs(t,x,u); % Compute output vector case 4, % Compute time of next sample sys=mdlGetTimeOfNextVarHit(t,x,u); case 9, % Finished. Do any needed sys=mdlTerminate(t,x,u); otherwise % Invalid input error(['Unhandled flag = ',num2str(flag)]);end%***********************************************************%* mdlInitializeSizes *%***********************************************************function [sys,x0,str,ts]=mdlInitializeSizes% Return the sizes of the system vectors, initial % conditions, and the sample times and offets.sizes = simsizes; % Create the sizes structuresizes.NumContStates = 4;sizes.NumDiscStates = 0;sizes.NumOutputs = 4;sizes.NumInputs = 1;sizes.DirFeedthrough = 0;sizes.NumSampleTimes = 1; % at least one sample time is neededsys = simsizes(sizes); % load sys with the sizes structurex0 = [0,0,0,0]; % Specify initial conditions for all states str = []; % str is always an empty matrixts = [0 0]; %initialize the array of sample times%***********************************************************%* mdlDerivatives *%***********************************************************function sys=mdlDerivatives(t,x,u,M,m,l)% Compute derivatives of continuous statesg = 9.8 ;Mass = [(M+m),m*l*cos(x(3));m*cos(x(3)),m*l] ;x_dot_dot = Mass\[m*l*x(4)^2*sin(x(3))+u ; m*g*sin(x(3))] ;sys = [x(2),x_dot_dot(1),x(4),x_dot_dot(2)] ;%***********************************************************%* mdlUpdate *%***********************************************************function sys=mdlUpdate(t,x,u)% Compute update for discrete states. If necessary, check for% sample time hits.sys = []; % Empty since this model has no discrete states.%***********************************************************%* mdlOutputs *%***********************************************************function sys=mdlOutputs(t,x,u)% Compute output vector given current state, time, and inputsys = x ;%***********************************************************%* mdlGetTimeOfNextVarHit *%***********************************************************function sys=mdlGetTimeOfNextVarHit(t,x,u)% 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 sampleTime = 1; sys = [] ;%************************************************************%* mdlTerminate *%************************************************************function sys=mdlTerminate(t,x,u)% Perform any necessary tasks at the end of the simulationsys = [];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -