请问使用 Simulink 中的 S-Function 如何实现以下代码的功能 (VCO) ?(How can I implement the following code (VCO) using S-Function in Simulink?)
이전 댓글 표시

以上用 Matlab 语言写的大概的逻辑;请问如何用 S-Function 实现该功能(Voltage Controlled Osillator);关键点是变步长;不知道什么原因,我用 S-Function 写的代码总是报错;我写的 S-Function 代码,如下:
// The above approximate logic written in Matlab language; please ask how to implement the function (Voltage Controlled Osillator) with S-Function; the key point is to change the step size; I don't know what the reason is, the code I write with S-Function always reports an error; the S-Function code I wrote, is as follows:
function [sys,x0,str,ts,simStateCompliance] = voltageControlledOsillator(t,x,u,flag,v1,v2,f1,f2)
switch flag,
% Initialization
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
% Derivatives
case 1,
sys=mdlDerivatives(t,x,u);
% Update
case 2,
sys=mdlUpdate(t,x,u);
% Outputs
case 3,
sys=mdlOutputs(t,x,u);
% GetTimeOfNextVarHit
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u,v1,v2,f1,f2);
% Terminate
case 9,
sys=mdlTerminate(t,x,u);
% Unexpected flags
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
%=============================================================================
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [-2 0]; % -2 means vriable step for simulating (Mr. Luo)
simStateCompliance = 'UnknownSimState';
%=============================================================================
function sys=mdlDerivatives(t,x,u)
sys = [];
%=============================================================================
function sys=mdlUpdate(t,x,u)
sys = [];
%=============================================================================
function sys=mdlOutputs(t,x,u)
sys = ~sys;
%=============================================================================
function sys=mdlGetTimeOfNextVarHit(t,x,u,v1,v2,f1,f2)
fs = (u-v1)*(f1-f2)/(v1-v2) + f1;
sampleTime = 1/fs/2;
sys = t + sampleTime;
%=============================================================================
function sys=mdlTerminate(t,x,u)
sys = [];
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Block and Blockset Authoring에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!