MATLAB/simulink S-函数编程基础

使用M-文件编写S-函数,实现正余弦波的叠加。

编写m文件:sinAddcos.m

function[sys, x0, str, ts] = sinAddcos(t,x,u,flag)
%% Create a s function to add the sine wave and cosine wave
% t         --simulated time
% x         --inner simulated block
% u         --simulated output
% flag      --operation flage
% sys       --S function output
% x0        --S function initial states
% str       --temp empty vector
% ts        --(2 X 2)time sample matrix,[:,1] sampling time and [:,2] time shifting
 
switch flag,
  case 0,
        [sys, x0, str, ts] = 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);
  otherwise
        error(['Unhandled flag = ', num2str(flag)])
end
 
function [sys, x0, str, ts] = mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 1;
sizes.NumOutputs = 1;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [0];
str = [];
ts = [0 0];
 
function sys = mdlDerivatives(t, x, u)
        sys = [];
function sys = mdlUpdate(t, x, u)
        sys = x;
function sys = mdlOutputs(t, x, u)
        sys = u(1) + u(2);
function sys = mdlGetTimeOfNextVarHit(t, x, u)
        sampleTime = 1;
        sys = t + sampleTime;
function sys = mdlTerminate(t, x,u)
        sys = [];

simulink建模与验证(同一文件目录下添加sinAddcos.m文件):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值