Matlab 未定义函数';青年';对于类型为';双倍';
我正在Matlab中为液压阀建立一个模型(test1) 当我尝试优化工具时,我得到以下错误:运行优化时出错。类型为“double”的输入参数的未定义函数“yout” 这是我使用的目标函数的代码:Matlab 未定义函数';青年';对于类型为';双倍';,matlab,simulink,Matlab,Simulink,我正在Matlab中为液压阀建立一个模型(test1) 当我尝试优化工具时,我得到以下错误:运行优化时出错。类型为“double”的输入参数的未定义函数“yout” 这是我使用的目标函数的代码: function F = obj_find_valve_param_a_max(x,Q_r) % Objective function to find the maximum valve area parameter value % Copyright 2010 MathWorks, Inc. as
function F = obj_find_valve_param_a_max(x,Q_r)
% Objective function to find the maximum valve area parameter value
% Copyright 2010 MathWorks, Inc.
assignin('base','a_max', x);
% If necessary, reset parameterizaton to second option (table)
model = 'test1';
load_system(model);
blkpth = find_system(bdroot,'ClassName','valve_dir_4_way');
set_param(char(blkpth),'mdl_type','1');
sim(model);
k = [1 1 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2]; % Weight multipliers
% Computing objective function
F = 0;
for j = 1:11
F = F + k(j) * (yout(j) - Q_r(j))^2;
end
end
% EOF
% Script to find directional valve parameter a_max
% Copyright 2010 MathWorks, Inc.
% This script file invokes optimization process to find the orifice
% area at maximum opening for 4-way directional valve when the valve is
% modeled using the first parameterization option (linear relationship
% between control signal and orifice area).
% init_opening - vaalve initial openings [mm]
% A_leak - leakage area [m^2]
% Q_r - vector of required flow rate at 11 valve displacements
% x0 - initial value for the only variable parameter: orifice maximum area
init_opening = -1; % mm
A_leak = 1e-9; % m^2
% Vector of required flow rates. Read out from plot on page 8 in
% Eaton/Vickers Porportional Directional Valves catalog for KBFDG5V-10 valve
% Actual flow rates are determined at fixed instances of time by exporting
% flow rate measured at the external loop of the valve to the MATLAB
% workspace
Q_r = [0 0 52 150 248 346 450 540 625 670 700];
% Set initial value of the orifice maximum area
x0 = 4.8; % [cm^2]
% Optimization
[x,fval,exitflag,output] = ...
fminsearch(@obj_find_valve_param_a_max,x0, ...
optimset('Tolx',1e-6,'Display','iter'),Q_r);
%bdclose all
如果我从第一个代码yout(j)-Q\r(j)
中删除,迭代将成功完成。
请问有人知道如何处理这个错误吗?
谢谢 此错误意味着MATLAB不知道如何处理对yout的引用。如果yout是一个变量(我猜这是你的意图),那么它必须在被索引之前被初始化。线路
试图索引数组yout的第j个元素。因此,yout必须是长度至少为j的数组。但是,您的代码没有初始化变量,因此出现错误。您没有向我们展示您的模型
test1
,更重要的是,它是如何配置的。您需要在模型中具有根级别的输出端口块,并将模型配置为将输出保存到工作区变量yout
(有关如何执行此操作的详细信息,请参阅)。另外,您的脚本(我怀疑它是从一些MathWorks材料中复制的…)假定yout
的长度为11。确保正确设置采样时间和/或抽取/输出选项,以确保正确设置。您会怎么做?我在这里没有看到它的定义。@Compassyout
是Simulink模型test1
的输出,但我怀疑OP没有正确配置模型以将变量输出到MATLAB工作区。它实际上是不正确的,可以在不初始化的情况下对变量进行索引。如果您循环yout(j)=2,它将在第一次迭代中创建。正如您正确指出的,这里的问题是它试图访问不存在的变量的元素。yout
是脚本运行的Simulink模型test1
的输出。我怀疑OP没有将其模型配置为将yout
作为变量输出(如果有)。
yout(j) - Q_r(j)