基于S函數的simulink仿真

基于S函數的simulink仿真

S函數可以用計算機語言來描述動態系統。在控制系統設計中,S函數可以用來描述控制算法、自適應算法和模型動力學方程

S函數中使用文本方式輸入公式和方程,適合復雜動態系統的數學描述,并且在仿真過程中可以對仿真參數進行更精確的描述、

1.1 S函數簡介

S函數是系統函數(system function)的簡稱。可以用MATLAB代碼、C、C++等語言來編寫S函數。

1.2 S函數的使用步驟

步驟如下:

  1. 創建S函數源文件
  2. 在動態系統的simulink模型框圖中添加S-function模塊,并且進行正確設置
  3. 在simulink模型框圖中按照定義好的功能連接輸入輸出端口

1.3 S函數的基本功能及重要參數設定

S函數的基本功能及重要參數設定如下:

  1. S函數功能模塊:各種功能模塊完成不同的任務,這些功能模塊(函數)稱為仿真例程或回調函數(call - back functions),包括初始化(initialization)、導數(mdlDerivative)、輸出(mdlOutput)等。
  2. NumContStates表示S - 函數描述的模塊中連續狀態的個數。
  3. NumDiscStates表示離散狀態的個數。
  4. NumOutputs和NumInputs分別表示模塊輸出和輸入的個數。
  5. 直接饋通(dirFeedthrough)為輸入信號是否在輸出端出現的標識,取值為0或1。例如,形如 y = k × u y = k×u y=k×u的系統需要輸入(即直接反饋),其中, u u u是輸入, k k k是增益, y y y是輸出,形如等式 y = x , x ˙ = u y = x,\dot{x}=u y=x,x˙=u的系統不需要輸入(即不存在直接反饋),其中, x x x是狀態, u u u是輸入, y y y為輸出。
  6. NumSampleTimes為模塊采樣周期的個數,S函數支持多采樣周期的系統。 除了sys外,還應設置系統的初始狀態變量 x 0 x_0 x0?、說明變量str和采樣周期變量 t s t_s ts? t s t_s ts?變量為雙列矩陣,其中每一行對應一個采樣周期。對連續系統和單個采樣周期的系統來說,該變量為 [ t 1 , t 2 ] [t_1,t_2] [t1?,t2?] t 1 t_1 t1?為采樣周期, t 1 = ? 1 t_1 = - 1 t1?=?1表示繼承輸入信號的采樣周期, t 2 t_2 t2?為偏移量,一般取為0。對連續系統來說, t s t_s ts?取為 [ ? 1 , 0 ] [-1,0] [?1,0]

1.4 S函數描述實例

在控制系統設計中,S函數可以用于控制器、自適應律和模型描述。

以模型 J θ ¨ = u + d ( t ) J\ddot{\theta}=u+d(t) Jθ¨=u+d(t)為例,其中, u u u為控制輸入, d ( t ) d(t) d(t)為加在控制輸入端的擾動,模型輸出為 θ 和 θ ˙ \theta和\dot{\theta} θθ˙,即轉動角度和角速度, J J J為轉動慣量,該模型可以描述如下:
x ˙ 1 = x 2 x ˙ 2 = 1 J ( u + d ( t ) ) \begin{align*} \dot{x}_1&=x_2\\ \dot{x}_2&=\frac{1}{J}(u + d(t)) \end{align*} x˙1?x˙2??=x2?=J1?(u+d(t))?
其中: x 1 = θ , x 2 = θ ˙ x_1=\theta ,x_2=\dot{\theta} x1?=θ,x2?=θ˙

1 首先,初始化Initialization函數

采用S函數來描述動力學方程,可選取1輸人2輸出系統,如果角度和角速度的初始值取零,則模型初始化參數寫為[0,0],模型初始化S函數描述如下:(見模板)
在這里插入圖片描述

2 微分方程描述的mdlDerivative函數

該函數可用于描述微分方程并實現數值求解。在控制系統中,可以采樣該函數來描述被控對象和自適應律等,并通過Simulink環境下選擇數值分析方法來實現對模型的數值求解

J = 2 , d ( t ) = s i n t J=2,d(t)=sint J=2d(t)=sint,則采用S函數可以實現模型角度和角速度的求解,描述如下:

function sys=mdlDerivatives(t,x,u)J=2;
dt=sin(t);
ut=u(1);
sys(1)=x(2);
sys(2)=1/J*(ut+dt);sys = [dx1;dx2];

在這里插入圖片描述

3 用于輸出的mdlOutput函數

S函數的mdlOutput函數通常用于描述控制器或模型的輸出。采用S函數的mdlOutput模塊來描述模型角度和角速度的輸出:

function sys=mdlOutputs(t,x,u)sys(1) = x(1);
sys(2) = x(2);

在這里插入圖片描述

最后,給出S函數模板

function [sys,x0,str,ts,simStateCompliance] = plant(t,x,u,flag,pa)
%SFUNTMPL General MATLAB S-Function Template
%   With MATLAB S-functions, you can define you own ordinary differential
%   equations (ODEs), discrete system equations, and/or just about
%   any type of algorithm to be used within a Simulink block diagram.
%
%   The general form of an MATLAB S-function syntax is:
%       [SYS,X0,STR,TS,SIMSTATECOMPLIANCE] = SFUNC(T,X,U,FLAG,P1,...,Pn)
%
%   What is returned by SFUNC at a given point in time, T, depends on the
%   value of the FLAG, the current state vector, X, and the current
%   input vector, U.
%
%   FLAG   RESULT             DESCRIPTION
%   -----  ------             --------------------------------------------
%   0      [SIZES,X0,STR,TS]  Initialization, return system sizes in SYS,
%                             initial state in X0, state ordering strings
%                             in STR, and sample times in TS.
%   1      DX                 Return continuous state derivatives in SYS.
%   2      DS                 Update discrete states SYS = X(n+1)
%   3      Y                  Return outputs in SYS.
%   4      TNEXT              Return next time hit for variable step sample
%                             time in SYS.
%   5                         Reserved for future (root finding).
%   9      []                 Termination, perform any cleanup SYS=[].
%
%
%   The state vectors, X and X0 consists of continuous states followed
%   by discrete states.
%
%   Optional parameters, P1,...,Pn can be provided to the S-function and
%   used during any FLAG operation.
%
%   When SFUNC is called with FLAG = 0, the following information
%   should be returned:
%
%      SYS(1) = Number of continuous states.
%      SYS(2) = Number of discrete states.
%      SYS(3) = Number of outputs.
%      SYS(4) = Number of inputs.
%               Any of the first four elements in SYS can be specified
%               as -1 indicating that they are dynamically sized. The
%               actual length for all other flags will be equal to the
%               length of the input, U.
%      SYS(5) = Reserved for root finding. Must be zero.
%      SYS(6) = Direct feedthrough flag (1=yes, 0=no). The s-function
%               has direct feedthrough if U is used during the FLAG=3
%               call. Setting this to 0 is akin to making a promise that
%               U will not be used during FLAG=3. If you break the promise
%               then unpredictable results will occur.
%      SYS(7) = Number of sample times. This is the number of rows in TS.
%
%
%      X0     = Initial state conditions or [] if no states.
%
%      STR    = State ordering strings which is generally specified as [].
%
%      TS     = An m-by-2 matrix containing the sample time
%               (period, offset) information. Where m = number of sample
%               times. The ordering of the sample times must be:
%
%               TS = [0      0,      : Continuous sample time.
%                     0      1,      : Continuous, but fixed in minor step
%                                      sample time.
%                     PERIOD OFFSET, : Discrete sample time where
%                                      PERIOD > 0 & OFFSET < PERIOD.
%                     -2     0];     : Variable step discrete sample time
%                                      where FLAG=4 is used to get time of
%                                      next hit.
%
%               There can be more than one sample time providing
%               they are ordered such that they are monotonically
%               increasing. Only the needed sample times should be
%               specified in TS. When specifying more than one
%               sample time, you must check for sample hits explicitly by
%               seeing if
%                  abs(round((T-OFFSET)/PERIOD) - (T-OFFSET)/PERIOD)
%               is within a specified tolerance, generally 1e-8. This
%               tolerance is dependent upon your model's sampling times
%               and simulation time.
%
%               You can also specify that the sample time of the S-function
%               is inherited from the driving block. For functions which
%               change during minor steps, this is done by
%               specifying SYS(7) = 1 and TS = [-1 0]. For functions which
%               are held during minor steps, this is done by specifying
%               SYS(7) = 1 and TS = [-1 1].
%
%      SIMSTATECOMPLIANCE = Specifices how to handle this block when saving and
%                           restoring the complete simulation state of the
%                           model. The allowed values are: 'DefaultSimState',
%                           'HasNoSimState' or 'DisallowSimState'. If this value
%                           is not speficified, then the block's compliance with
%                           simState feature is set to 'UknownSimState'.%   Copyright 1990-2010 The MathWorks, Inc.%
% The following outlines the general structure of an S-function.
%
switch flag,%%%%%%%%%%%%%%%%%%% Initialization %%%%%%%%%%%%%%%%%%%case 0,[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;%%%%%%%%%%%%%%%% Derivatives %%%%%%%%%%%%%%%%case 1,sys=mdlDerivatives(t,x,u,pa);%%%%%%%%%%% Update %%%%%%%%%%%case 2,sys=mdlUpdate(t,x,u);%%%%%%%%%%%% Outputs %%%%%%%%%%%%case 3,sys=mdlOutputs(t,x,u);%%%%%%%%%%%%%%%%%%%%%%%% GetTimeOfNextVarHit %%%%%%%%%%%%%%%%%%%%%%%%case 4,sys=mdlGetTimeOfNextVarHit(t,x,u);%%%%%%%%%%%%%% Terminate %%%%%%%%%%%%%%case 9,sys=mdlTerminate(t,x,u);%%%%%%%%%%%%%%%%%%%%% Unexpected flags %%%%%%%%%%%%%%%%%%%%%otherwiseDAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));end% end sfuntmpl%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded.  This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes;sizes.NumContStates  = 2;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 2;
sizes.NumInputs      = 1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;   % at least one sample time is neededsys = simsizes(sizes);%
% initialize the initial conditions
%
x0  = [0,0];%
% str is always an empty matrix
%
str = [];%
% initialize the array of sample times
%
ts  = [0 0];% Specify the block simStateCompliance. The allowed values are:
%    'UnknownSimState', < The default setting; warn and assume DefaultSimState
%    'DefaultSimState', < Same sim state as a built-in block
%    'HasNoSimState',   < No sim state
%    'DisallowSimState' < Error out when saving or restoring the model sim state
simStateCompliance = 'UnknownSimState';% end mdlInitializeSizes%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u,pa)
k=pa.k;
m=pa.m;x1=x(1);
x2=x(2);dx1=x2;
dx2=-k/m*x1^3+u/m;sys = [dx1;dx2];% end mdlDerivatives%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys=mdlUpdate(t,x,u)sys = [];% end mdlUpdate%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u)sys = x;% end mdlOutputs%
%=============================================================================
% mdlGetTimeOfNextVarHit
% 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
% mdlInitializeSizes.
%=============================================================================
%
function sys=mdlGetTimeOfNextVarHit(t,x,u)sampleTime = 1;    %  Example, set the next hit to be one second later.
sys = t + sampleTime;% end mdlGetTimeOfNextVarHit%
%=============================================================================
% mdlTerminate
% Perform any end of simulation tasks.
%=============================================================================
%
function sys=mdlTerminate(t,x,u)sys = [];% end mdlTerminate

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/74399.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/74399.shtml
英文地址,請注明出處:http://en.pswp.cn/web/74399.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

做題記錄:和為K的子數組

來自leetcode 560 前言 自己只會暴力&#xff0c;這里就是記錄一下前綴和哈希表的做法&#xff0c;來自靈神的前綴和哈希表&#xff1a;從兩次遍歷到一次遍歷&#xff0c;附變形題 正文 首先&#xff0c;這道題無法使用滑動窗口&#xff0c;因為滑動窗口需要滿足單調性&am…

淺淺嘗試Numpy的函數:

1.numpy.empty: numpy.empty方法用來創建一個指定形狀&#xff08;shape&#xff09;&#xff0c;數據類型&#xff08;dtype&#xff09;且未被初始化的數組&#xff1a; numpy.empty(shape,dtype float,order C) 參數說明&#xff1a; shape:數組形狀。 dtype:數據類型&am…

IM基本設計思路與有序ID的重要性

文章目錄 概要問題解析思考問題數據基礎讀取寫入總結 概要 說起IM程序我們都不陌生&#xff0c;本篇文章我們就為如何實現一個IM做一個簡單的整體方案設計以及基本的數據結構 問題解析 我們先不上一大堆牛逼哄哄的中間件。 我們先從實現角度&#xff0c;來講講設計思路。 從…

數據結構學習

鏈表 單鏈表 頭插 將x插到下標是k的點后面 將下標是k的點后面的點刪掉 代碼 // head 表示頭結點的下標 // e[i] 表示節點i的值 // ne[i] 表示節點i的next指針是多少 // idx 存儲當前已經用到了哪個點// 初始化 void init() {head -1;idx 0; }// 將x插到頭結點 void add_to_…

0.DJI-PSDK開發準備及資料說明(基于DJI經緯M300RTK和M350RTK無人機上使用)

0.DJI-PSDK開發準備及資料說明&#xff08;基于DJI經緯M300RTK和M350RTK無人機上使用&#xff09; 【資料名稱】 DJI經緯M300RTK和M350RTK無人機二次開發資料包。資料包在最下方的百度網盤 一、引言 在進行大疆無人機負載開發的過程中&#xff0c;我整理出一系列有價值的資…

Linux內核TCP/IP協議棧中的設計模式:從面向對象到系統級軟件的跨界實踐

引言 設計模式(Design Patterns)自GoF(Gang of Four)在1994年提出以來,已成為軟件工程領域的核心概念。盡管其經典定義基于面向對象編程(OOP),但設計模式的本質是解決復雜問題的經驗總結,而非局限于特定編程范式。本文以Linux內核的TCP/IP協議棧為例,探討設計模式在…

第十四屆藍橋杯大賽軟件賽省賽C/C++ 大學 B 組(部分題解)

文章目錄 前言日期統計題意&#xff1a; 冶煉金屬題意&#xff1a; 島嶼個數題意&#xff1a; 子串簡寫題意&#xff1a; 整數刪除題意&#xff1a; 總結 前言 一年一度的&#x1f3c0;杯馬上就要開始了&#xff0c;為了取得更好的成績&#xff0c;好名字寫了下前年2023年藍橋…

處理JWT Token失效需求

JWT 本身是無狀態的&#xff0c;這意味著服務器不會保存任何關于 Token 的狀態信息。但為了支持 JWT 的狀態管理&#xff08;例如&#xff1a;強制使某些 Token 失效&#xff09;&#xff0c;可以借助 Redis 這樣的外部存儲來維護一個黑名單或白名單。 安裝必要的 NuGet 包 首…

PHP代碼審計-01

&#x1f338; 連接方式 PHP Mysql連接方式&#xff1a; Mysql&#xff08;廢棄&#xff09;MysqliPDO &#x1f338; 常見過濾 intval/addslashes/mysql_real_escape mysqli_escape_string/mysqli_real_escape_string/mysqli::escape_string PDO::quote 參數化查詢 a…

SpringKafka錯誤處理:重試機制與死信隊列

文章目錄 引言一、Spring Kafka錯誤處理基礎二、配置重試機制三、死信隊列實現四、特定異常的處理策略五、整合事務與錯誤處理總結 引言 在構建基于Kafka的消息系統時&#xff0c;錯誤處理是確保系統可靠性和穩定性的關鍵因素。即使設計再完善的系統&#xff0c;在運行過程中也…

藍橋杯2024JavaB組的一道真題的解析

文章目錄 1.問題描述2.問題描述3.思路分析4.代碼分析 1.問題描述 這個是我很久之前寫的一個題目&#xff0c;當時研究了這個題目好久&#xff0c;發布了一篇題解&#xff0c;后來很多人點贊&#xff0c;我都沒有意識到這個問題的嚴重性&#xff0c;我甚至都在懷疑自己&#xf…

性能比拼: Go標準庫 vs Python FastAPI(第二輪)

本內容是對知名性能評測博主 Anton Putra Python (FastAPI) vs Go (Golang) (Round 2) Performance Benchmark 內容的翻譯與整理, 有適當刪減, 相關指標和結論以原作為準 介紹 這是第二輪關于 FastAPI 和 Golang 的對比測試。我幾天前運行了前一次的基準測試&#xff0c;到目…

DeepSeek與ChatGPT的優勢對比:選擇合適的工具來提升工作效率

選DeepSeek還是ChatGPT&#xff1f;這就像問火鍋和披薩哪個香&#xff01; "到底該用DeepSeek還是ChatGPT?” 這個問題最近在互聯網圈吵翻天!其實這就跟選手機系統-樣&#xff0c;安卓黨iOS黨都能說出一萬條理由&#xff0c;但真正重要的是你拿它來干啥&#xff01;&am…

Python爬蟲第4節-請求庫urllib的request模塊使用

目錄 前言&#xff1a;基本庫urllib的使用 一、urlopen方法 二、Request類 三、高級用法 前言&#xff1a;基本庫urllib的使用 開始學習爬蟲時&#xff0c;第一步就是要模擬瀏覽器給服務器發送請求。這個時候&#xff0c;你可能會有很多問題&#xff1a;該從哪里開始做呢&a…

Vue3 Pinia Store使用示例

代碼示例&#xff1a; import { defineStore } from "pinia"; // 導入 Pinia 的 defineStore 方法 import { ref } from "vue"; // 導入 Vue 的響應式 API ref import { type Menu } from "/interface"; // 導入自定義的 Menu 類型/…

JavaScript逆向魔法:Chrome開發者工具探秘之旅

在前端開發和安全研究領域&#xff0c;JavaScript逆向工程是一項關鍵技能。它涉及分析和理解代碼的執行流程、數據結構和邏輯&#xff0c;以發現潛在的安全漏洞、提取核心算法或實現功能兼容。本文將結合Chrome開發者工具的調試功能&#xff0c;并通過具體示例幫助你更好地理解…

Qt基礎:資源文件

資源文件 1. 資源文件2. 資源文件創建 1. 資源文件 資源文件顧名思義就是一個存儲資源的文件&#xff0c;在Qt中引入資源文件好處在于他能提高應用程序的部署效率并且減少一些錯誤的發生。 在程序編譯過程中&#xff0c; 添加到資源文件中的文件也會以二進制的形式被打包到可執…

Agent TARS與Manus的正面競爭

Agent TARS 是 Manus 的直接競爭對手&#xff0c;兩者在 AI Agent 領域形成了顯著的技術與生態對抗。 一、技術架構與功能定位的競爭 集成化架構 vs 模塊化設計 Agent TARS 基于字節跳動的 UI-TARS 視覺語言模型&#xff0c;將視覺感知、推理、接地&#xff08;grounding&#…

使用ssh連接上開發板

最后我發現了問題&#xff0c;我忘記指定用戶名了&#xff0c;在mobaXterm上左上角打開會話&#xff0c;點擊ssh&#xff0c;然后輸入要連接的開發板主機的ip地址&#xff0c;關鍵在這里&#xff0c;要指定你要連接的開發板的系統中存在的用戶&#xff0c;因為通過ssh連接一個設…

【性能優化點滴】odygrd/quill在編譯期做了哪些優化

Quill 是一個高性能的 C 日志庫&#xff0c;它在編譯器層面進行了大量優化以確保極低的運行時開銷。以下是 Quill 在編譯器優化方面的關鍵技術和實現細節&#xff1a; 1. 編譯時字符串解析與格式校驗 Quill 在編譯時完成格式字符串的解析和校驗&#xff0c;避免運行時開銷&…