当前位置: 首页 > news >正文

ODE45函数——中间变量提取,时变量参数,加速仿真以及运行进度条

System define

The example system:
G ( s ) = U ( s ) s 3 − 6 s 2 + 11 s − 6 G(s)=\frac{U(s)}{s^3-6s^2+11s-6} G(s)=s36s2+11s6U(s)
The state-space model:
d x 1 d t = x 2 d x 2 d t = x 3 d x 3 d t = 6 x 1 − 11 x 2 + 6 x 3 + u \begin{aligned} \frac{dx_1}{dt} &= x_2 \\ \frac{dx_2}{dt} &= x_3 \\ \frac{dx_3}{dt} &= 6x_1 -11 x_2 + 6x_3 + u \end{aligned} dtdx1dtdx2dtdx3=x2=x3=6x111x2+6x3+u

Section 1: Basic usage (A unit step response)

Note: By limiting the points of time span, the simulation speed can be fastened

tspan = [0,5]; % Simulation time span
% tspan = linspace(0,5,100); % Simulation time span with limited points
y0    = [0;0;0]; % Initial value
[t, y] = ode45(@(t, y) sysOdeFunc(t, y), tspan, y0);
plot(t,y(:,1))function dydt = sysOdeFunc(t,y)%---  Unpack variables ---x1 = y(1);x2 = y(2);x3 = y(3);%--- Controller define ---u  = 1;%--- Disturbance define ---d  = 1;%--- System define ---dx1dt   = x2;dx2dt   = x3;dx3dt   = 6*x1 - 11*x2 + 6*x3 + u + d;%--- Ode array output ----dydt = [dx1dt;dx2dt;dx3dt];
end

Section 2: Argument dependent on time

Case 1: Implicit form (Only sampled value is known)

tspan = [0,5]; % Simulation time span
% tspan = linspace(0,5,100); % Simulation time span with limited points
y0    = [0;0;0]; % Initial value
ft    = linspace(0,5,25); % Here simulate a sampled value and assume it is governed by f(t)
f     = ft.^2 - ft - 3; % Here simulate a sampled value and assume it is governed by f(t)
[t, y] = ode45(@(t, y) sysOdeFunc(t, y, ft, f), tspan, y0);
plot(t,y(:,1))function dydt = sysOdeFunc(t, y, ft, f)%---  Unpack variables ---x1 = y(1);x2 = y(2);x3 = y(3);%--- Controller define ---u  = 1;%--- Disturbance define ---d = interp1(ft, f, t)%--- System define ---dx1dt   = x2;dx2dt   = x3;dx3dt   = 6*x1 - 11*x2 + 6*x3 + u + d;%--- Ode array output ----dydt = [dx1dt;dx2dt;dx3dt];
end

Case 2: Explicit form (Function expression is known)

tspan = [0,5]; % Simulation time span
% tspan = linspace(0,5,100); % Simulation time span with limited points
y0    = [0;0;0]; % Initial value
d     = @(t)sin(t);
[t, y] = ode45(@(t, y) sysOdeFunc(t, y,d), tspan, y0);
plot(t,y(:,1))function dydt = sysOdeFunc(t,y,d)%---  Unpack variables ---x1 = y(1);x2 = y(2);x3 = y(3);%--- Controller define ---u  = 1;%--- Disturbance define ---% No need %--- System define ---dx1dt   = x2;dx2dt   = x3;dx3dt   = 6*x1 - 11*x2 + 6*x3 + u + d(t);%--- Ode array output ----dydt = [dx1dt;dx2dt;dx3dt];
end

Section 3: Output additional variable

tspan = [0,5]; % Simulation time span
% tspan = linspace(0,5,100); % Simulation time span with limited points
y0    = [0;0;0]; % Initial value
d     = @(t)sin(t);
[t, y] = ode45(@(t, y) sysOdeFunc(t, y, d), tspan, y0);%--- Additional variables extraction ----for k = 1:max(size(t))[~, uout(k,:)] = sysOdeFunc(t(k), y(k,:), d);
endplot(t,y(:,1)), title('System state')
figure
plot(t,uout), title('Additional variable')function [dydt,u] = sysOdeFunc(t,y,d)%---  Unpack variables ---x1 = y(1);x2 = y(2);x3 = y(3);%--- Controller define ---u  = sin(t);%--- Disturbance define ---% No need %--- System define ---dx1dt   = x2;dx2dt   = x3;dx3dt   = 6*x1 - 11*x2 + 6*x3 + u + d(t);%--- Ode array output ----dydt = [dx1dt;dx2dt;dx3dt];
end

Section 4: Add the ODE process and abort selection

The layout would be like below:

在这里插入图片描述

The code is as below

tspan = [0,5]; % Simulation time span
% tspan = linspace(0,5,100); % Simulation time span with limited points
y0    = [0;0;0]; % Initial value
odeopts = odeset('OutputFcn', @odeprog, 'Events',@odeabort);
[t, y] = ode45(@(t, y) sysOdeFunc(t, y), tspan, y0, odeopts);plot(t,y(:,1))function dydt = sysOdeFunc(t,y)%---  Unpack variables ---x1 = y(1);x2 = y(2);x3 = y(3);%--- Controller define ---u  = 1;%--- Disturbance define ---d = 1;%--- System define ---dx1dt   = x2;dx2dt   = x3;dx3dt   = 6*x1 - 11*x2 + 6*x3 + u + d;%--- Ode array output ----dydt = [dx1dt;dx2dt;dx3dt];
end

Two functions are defined as follows:

The function of odeprog 1:

function status = odeprog(t,y,flag,varargin)
%status = odebarplot(t,y,flag,varargin)
%   ODE progress display function with interrupt control
%   Displays a vertical bar plot that fills as the simulation
%   nears its completion.  Also displays time ellapsed and estimates
%   time remaining in the simulation.  To avoid computation burden
%   refreshes are limited to every 0.5 seconds.
%
% Tim Franklin
% Virginia Tech
% Jesse Norris
% Wake Forrest
% May 2006global odeprogglobvarif nargin < 3 || isempty(flag) if(etime(clock,odeprogglobvar(8:13))>0.5)tfin=odeprogglobvar(1);sstrt=odeprogglobvar(2:7);figure(95); perc=t(end)/tfin;area([t(end) tfin-t(end);t(end) tfin-t(end)]);title([num2str(perc*100) '%']);set(findobj('Tag','eltime'),'String',etimev(clock,sstrt));set(findobj('Tag','esttime'),'String',etimev(etime(clock,sstrt)/perc*(1-perc)));odeprogglobvar(8:13)=clock;end
elseswitch(flag)case 'init'  odeprogglobvar=zeros(1,13);odeprogglobvar(1)=t(end);odeprogglobvar(2:7)=clock;odeprogglobvar(8:13)=clock;tfin=odeprogglobvar(1);sstrt=odeprogglobvar(2:7);figure(95); set(gcf,'Position',[4,40,100,500]);axes('Position',[0.5,0.25,0.25,0.6]);axis([1,2,0,tfin]);set(gca,'XTickLabel',[],'NextPlot','replacechildren');ylabel('Simulation Progress - Time (s)');title('0%');area([0 tfin;0 tfin]);uicontrol('Style', 'pushbutton', 'String', 'Abort','Position', [7 460 90 30], 'Callback', 'close(gcf)')uicontrol('Style', 'text', 'String', 'Ellapsed Time','Position', [7 100 90 15])uicontrol('Style', 'text', 'Tag', 'eltime', 'String', etimev(clock,sstrt),'Position', [7 80 90 15])uicontrol('Style', 'text', 'String', 'Time Remaining','Position', [7 60 90 15])uicontrol('Style', 'text', 'Tag', 'esttime', 'String', num2str(inf),'Position', [7 40 90 15])pause(0.1);case 'done'    if(ishandle(95))close(95);endend
end
status = 0;
drawnow;function [S] = etimev(t1,t0)
%ETIMEV  Verbose Elapsed time.
%   ETIMEV(T1,T0) returns string of the days, hours, minutes, seconds that have elapsed 
%   between vectors T1 and T0.  The two vectors must be six elements long, in
%   the format returned by CLOCK:
%
%       T = [Year Month Day Hour Minute Second]
%   OR
%   ETIMEV(t), t in seconds
if(exist('t1')&exist('t0')&length(t1)>2&length(t0)>2)t=etime(t1,t0);     %Time in secondsif(t<0)t=-t;end
elseif(length(t1)==1)t=t1;
elset=0;
end
days=floor(t/(24*60*60));
t=t-days*24*60*60;
hours=floor(t/(60*60));
t=t-hours*60*60;
mins=floor(t/60);
t=floor(t-mins*60);
if(days>0)S=[num2str(days) 'd ' num2str(hours) 'h ' num2str(mins) 'm ' num2str(t) 's'];
elseif(hours>0)S=[num2str(hours) 'h ' num2str(mins) 'm ' num2str(t) 's'];
elseif(mins>0)S=[num2str(mins) 'm ' num2str(t) 's'];
elseS=[num2str(t) 's'];
end

The function of odeabort1:

function [value,isterminal,direction]=odeabort(t,S,varargin)%Other Events Set Here...ie:
% value(2)=max(abs(S(1:18)))-pi/2;%Test to see if 'simstop' box is closed
value(1)=double(ishandle(95));
isterminal=[1];
direction=[0];

  1. https://www.mathworks.com/matlabcentral/fileexchange/9904-ode-progress-bar-and-interrupt ↩︎ ↩︎


http://www.mrgr.cn/news/47769.html

相关文章:

  • C#语言的数据库编程
  • 操作系统之文件的逻辑结构
  • 什么是负载均衡?NGINX是如何实现负载均衡的?
  • SpringBoot | 基于 MyBatis 的分页与模糊查询的开发模板
  • 案例研究:UML用例图中的结账系统
  • CDP集成Hudi实战-Hive
  • CLIP——多模态预训练模型介绍
  • 【Linux系统编程】第三十弹---软硬链接与动静态库的深入探索
  • BERT的中文问答系统14
  • C++网络编程之套接字基础
  • 大模型推荐LLM4Rec调研2024
  • 浅谈云原生--微服务、CICD、Serverless、服务网格
  • 『Mysql进阶』Mysql SQL语句性能分析(七)
  • 代码随想录算法训练营Day31 | 455.分发饼干、376.摆动序列、53.最大子数组和
  • 2025年第九届绿色能源与应用国际会议(ICGEA 2025)即将召开!
  • 书店系统小程序的设计
  • 二叉树系列 10/11
  • 如何成为一名认证的低代码开发师?考证和培训指南!
  • TARA详解
  • LESS、SASS 与 SCSS 预处理器详解
  • LLM大模型怎样进行数据和质量测试
  • 在线拍卖|基于springBoot的在线拍卖系统设计与实现(附项目源码+论文+数据库)
  • C++:vector(题目篇)
  • 【Spring】Bean的生命周期
  • 【软件设计师】68道高频考题(附答案),无非就是考这些知识
  • 消防安全小程序推动社会消防安全意识提升