节省特定仿真时间
Saving a specific simulation time
我有一个在 simulink 中实现的双轨模型。为了控制速度,我使用了 PID 控制器,因此速度的输出如下所示:
现在我想实现一个 MATLAB 函数或 simulink 块来跟踪速度达到稳态行为的时间并将其放入某种存储中。我试图通过以下带有 MATLAB 函数块的 MATLAB 函数来实现类似的东西:
function y = fcn(t,v,dv,tv)
%#codegen
if (v==tv+0.01) & (dv<0)
y=t
end
t 是时钟信号,v 是速度,dv 是速度的一阶导数,tv 是目标速度。这个函数的问题是有"y is not defined on some execution paths"。你有什么想法如何使这项工作?
function y = fcn(t,v,dv,tv)
%#codegen
y = zeros(length(t),1); % Initialise the array
for ii = 1:length(t)
if (v==tv+0.01) & (dv<0)
y(ii)=t;
else
y(ii)=0;
end
end
y(y==0)=[];
end
两个变化:在y=t
之后添加了一个分号,强制它在每次设置时不打印它。其次,你的问题 else y=[];
,这意味着如果你不遵守你的 if
声明,y
将是一个空矩阵。
它现在会在您每次不遵守 if
声明时存储一个 0
。 y(y==0)=[];
行删除所有零,如果您希望 y
与输入变量的长度相同,请注释掉这一行。
function y = fcn(t,v,dv,tv)
%#codegen
y = zeros(length(t),1); % Initialise the array
ii=1;
while exist(t)
if (v==tv+0.01) & (dv<0)
y(ii)=t;
else
y(ii)=0;
end
ii = ii+1;
end
y(y==0)=[];
end
我在没有 MATLAB 函数的情况下使用 simulink 块 data store memory
及其 read
和 write
块解决了这个问题。从右下方输入的信号是瞬时速度。 if
语句是
(u1 >= 22.2) & (u1<=22.3) & (u2<0)
由于 simulink 使用时间步并且瞬时速度永远不会正好 22.2
,因此您不能使用 u1==22.2
在 SimuLink 中,为您的函数添加第二个输出和第五个输入。然后使用该新输出作为对函数的反馈。
function [y, output] = fcn(t,v,dv,tv,input)
y = 0;
output = input;
if (v == tv + 0.01) && (dv < 0)
y = t;
if (input == -1)
output = t;
end
end
将 output
附加到 IC
块,在其中将 input
初始值设置为 -1
或您要使用的任何值。然后将 IC
块附加到函数的 input
上。 output
会通过函数不断反馈。一旦设置好,它将永远保持其价值。
我有一个在 simulink 中实现的双轨模型。为了控制速度,我使用了 PID 控制器,因此速度的输出如下所示:
现在我想实现一个 MATLAB 函数或 simulink 块来跟踪速度达到稳态行为的时间并将其放入某种存储中。我试图通过以下带有 MATLAB 函数块的 MATLAB 函数来实现类似的东西:
function y = fcn(t,v,dv,tv)
%#codegen
if (v==tv+0.01) & (dv<0)
y=t
end
t 是时钟信号,v 是速度,dv 是速度的一阶导数,tv 是目标速度。这个函数的问题是有"y is not defined on some execution paths"。你有什么想法如何使这项工作?
function y = fcn(t,v,dv,tv)
%#codegen
y = zeros(length(t),1); % Initialise the array
for ii = 1:length(t)
if (v==tv+0.01) & (dv<0)
y(ii)=t;
else
y(ii)=0;
end
end
y(y==0)=[];
end
两个变化:在y=t
之后添加了一个分号,强制它在每次设置时不打印它。其次,你的问题 else y=[];
,这意味着如果你不遵守你的 if
声明,y
将是一个空矩阵。
它现在会在您每次不遵守 if
声明时存储一个 0
。 y(y==0)=[];
行删除所有零,如果您希望 y
与输入变量的长度相同,请注释掉这一行。
function y = fcn(t,v,dv,tv)
%#codegen
y = zeros(length(t),1); % Initialise the array
ii=1;
while exist(t)
if (v==tv+0.01) & (dv<0)
y(ii)=t;
else
y(ii)=0;
end
ii = ii+1;
end
y(y==0)=[];
end
我在没有 MATLAB 函数的情况下使用 simulink 块 data store memory
及其 read
和 write
块解决了这个问题。从右下方输入的信号是瞬时速度。 if
语句是
(u1 >= 22.2) & (u1<=22.3) & (u2<0)
由于 simulink 使用时间步并且瞬时速度永远不会正好 22.2
,因此您不能使用 u1==22.2
在 SimuLink 中,为您的函数添加第二个输出和第五个输入。然后使用该新输出作为对函数的反馈。
function [y, output] = fcn(t,v,dv,tv,input)
y = 0;
output = input;
if (v == tv + 0.01) && (dv < 0)
y = t;
if (input == -1)
output = t;
end
end
将 output
附加到 IC
块,在其中将 input
初始值设置为 -1
或您要使用的任何值。然后将 IC
块附加到函数的 input
上。 output
会通过函数不断反馈。一旦设置好,它将永远保持其价值。