Matlab·Simulink的使用—【S函数的创建与应用】
文章目錄
- (〇) 前言
- (一) S函數的結構及編輯
- (二)S函數的應用:
- ??(1)定義S函數
- ????????①初始化主函數
- ????????②初始化子函數
- ??(2)Simulink模型中使用S函數
- (三)特別注意*——解決初學者第一次應用S函數常碰到的錯誤
- (四)常見錯誤
(〇) 前言
S函數是什么:
????S函數是系統函數的英文縮寫,指采用一種設計語言(非圖形方式)去描述的一個功能模塊
如何編寫S函數:
????可以用Matlab的語言,也可以用其它編程語言入C,C++等
????我只介紹下常用的MATLAB自帶“語言”和S函數模板去編寫S函數
后文的源代碼
https://download.csdn.net/download/Nirvana_Tai/12321280
https://download.csdn.net/download/Nirvana_Tai/12321288
(一) S函數的結構及編輯
①在命令行窗口輸入命令: >>edit sfuntmpl.m
????即可打開模板文件
②模板文件sfuntmpl.m包含了一個主函數和六個子函數
【1】我們先看主函數:
??主函數首句——引導語句為:
function [sys,x0,str,ts,simStateCompliance] = sfuntmpl(t,x,u,flag)
?? ??其中,fname是S函數的函數名;
?? ??輸入形參t,x,y,flag分別為仿真時間,狀態向量,輸入向量和調用標志
?? ??輸出形參sys表示返回參數;x0是初始狀態值;str被設置為空陣;ts是一個兩列矩陣,一列為各狀態變量的采樣周期,另一列是相應采樣時間的偏移量。
?? ??【M文件中S函數是這么設置的】
??子函數的前綴為mdl,由flag來控制調用情況
常用的四種情況:
???Flag=0:調用初始化子函數 mdlInitializeSizes;
?? Flag=1:調用連續狀態更新子函數 mdlDerivatives(t,x,u);
?? Flag=2: 調用離散狀態更新子函數 mdlUpdate(t,x,u);
?? Flag=3: 調用輸出子函數 mdlOutputs(t,x,u);
(二)S函數的應用:
??舉個最簡單的栗子:y=kx+b
??(1)定義S函數
????????①初始化主函數
function [sys,x0,str,ts,simStateCompliance] = move(t,x,u,flag) 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);otherwiseDAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag)); end????????②初始化子函數
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes sizes = simsizes; sizes.NumContStates = 1; sizes.NumDiscStates = 0; sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 1; % at least one sample time is needed sys = simsizes(sizes); x0 = [0]; str = []; ts = [0 0]; simStateCompliance = 'UnknownSimState'; function sys=mdlDerivatives(t,x,u) sys = u; function sys=mdlUpdate(t,x,u) sys=[]; function sys=mdlOutputs(t,x,u) sys=x; function sys=mdlGetTimeOfNextVarHit(t,x,u) sampleTime = 1; % Example, set the next hit to be one second later. sys = t + sampleTime; function sys=mdlTerminate(t,x,u) sys = [];??(2)Simulink模型中使用S函數
????首先我們需要用模塊搭建出這個模型
????新手可能找不到器件,這里我簡單說一下:simulink標準模塊庫中,Sinks下有Scope,Sources下有Sine Wave,User-Defined Functions下有S-Function.連起來就可以了
????這里再說一下如何修改Scope的輸入端子數:雙擊打開Scope界面,點擊左上方設置,修改Input參數就可以了,記得修改完點Apply。
建立完模型如下:
得到結果如下:
????采樣時間:對于Simulink模型來說,解算器中的一個步長決定了整個模型最小的采樣時間間隔。
(三)特別注意*——解決初學者第一次應用S函數常碰到的錯誤
????第一次應用S函數時肯能會碰到很多問題,我就幾個常見問題總結接個需要注意的地方,希望大家能一次寫對。
????①注意此處 S函數名一定要和編寫的.m文件,否則會報錯
????②注意,一定要把S函數的.m文件和模型的.slx文件放在一個目錄下,確保點擊Edit時,可以找到并打開對應S函數M文件
????③注意,要把matlab的當前工作目錄修改為你這兩個文件保存的目錄下
(四)常見錯誤
①步長錯誤
Starting build procedure for model: test Build procedure for model:
‘test’ aborted due to an error. The specified code generation target
for model ‘test’ cannot be used with a variable-step solver.
Suggested Actions You may configure the solver options for a
fixed-step solver with an appropriate integration algorithm Select a
target that supports a variable-step solver, such as rsim.tlc You can
use ‘Embedded Coder Quick Start’ to help you generate code
遇到這種情況,在模型編輯窗口點擊設置,修改步長設置即可。
②類似于此,請檢查模型的搭建是否符合S函數的邏輯,檢查輸入輸出端。看S函數源代碼是否正確
再查看目錄是否一致,工作區是否在當前目錄
Generating code into build folder: D:\Study Playground\sample_grt_rtw
Unconnected input line found on ‘sample/Scope’ (input port: 1)
Component: Simulink | Category: Block warning Unconnected output line
found on ‘sample/Sine Wave’ (output port: 1) Component: Simulink |
Category: Block warning Invoking Target Language Compiler on
sample.rtw Using System Target File: D:\Baidu network disk
download\Matlab\rtw\c\grt\grt.tlc Loading TLC function libraries
Initial pass through model to cache user defined code Caching model
source code Writing header file sample.h Writing header file
sample_types.h Writing header file rtwtypes.h Writing header file
builtin_typeid_types.h Writing header file multiword_types.h Writing
source file sample.c Writing header file sample_private.h Writing
header file rtmodel.h Writing source file sample_data.c Writing header
file rt_nonfinite.h Writing source file rt_nonfinite.c Writing header
file rtGetInf.h Writing source file rtGetInf.c Writing header file
rtGetNaN.h Writing source file rtGetNaN.c TLC code generation
complete. Using toolchain: LCC-win64 v2.4.1 | gmake (64-bit Windows)
‘D:\Study Playground\sample_grt_rtw\sample.mk’ is up to date Building
‘sample’: D:\Baidu network disk download\Matlab\bin\win64\gmake -f
sample.mk all D:\Study Playground\sample_grt_rtw>set MATLAB=D:\Baidu
network disk download\Matlab D:\Study Playground\sample_grt_rtw>cd .
D:\Study Playground\sample_grt_rtw>if “” == “” (D:\Baidu network disk
download\Matlab\bin\win64\gmake -f sample.mk all ) else (D:\Baidu
network disk download\Matlab\bin\win64\gmake -f sample.mk )
‘D:\Baidu’ 不是內部或外部命令,也不是可運行的程序 或批處理文件。 D:\Study
Playground\sample_grt_rtw>echo The make command returned an error of
9009 The make command returned an error of 9009 D:\Study
Playground\sample_grt_rtw>An_error_occurred_during_the_call_to_make
‘An_error_occurred_during_the_call_to_make’ 不是內部或外部命令,也不是可運行的程序
或批處理文件。 Build procedure for model: ‘sample’ aborted due to an error.
Error(s) encountered while building “sample”: Failed to generate all
binary outputs.
????這樣基本上就可以保證運行,如果有其他問題,請自行查詢或者評論錯誤截圖。
總結
以上是生活随笔為你收集整理的Matlab·Simulink的使用—【S函数的创建与应用】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java弹框形式输入_java中点击一个
- 下一篇: 添加dubbo.xsd的方法