Error using fminsearch and optimset

4 次查看(过去 30 天)
Hi,
I am trying to calibrate parameters using experimental data and simulation results from simulink. But when I try to run it, Matlab gives me some errors. Below the errors:
>> CalibrationASM1_Namoniacal
Index exceeds matrix dimensions.
Error in optASM1 (line 10)
bh=p(3);
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
Error in CalibrationASM1_Namoniacal (line 11)
[p,fval,exitflag,output]=fminsearch(@optASM1,[1,1],options)
Below the way I put my equation and inputs in matlab:
% Load parameters for ASM1 first
%% Variable input
t=xlsread('Resultados experimentais.xlsx','N amoniacal','l7:l16');
Dexp=xlsread('Resultados experimentais.xlsx','N amoniacal','m7:m16');
global yh;
global ya;
global bh;
global ba;
%% Optimization
options=optimset('MaxFunEvals',400,'TolFun',1e-8,'TolX',1e-6);
[p,fval,exitflag,output]=fminsearch(@optASM1,[1,1],options)
%% Outputs
sim('ASM1_batelada');
Dsim=Snhout([2 25 41 49 67 77 83 86 90 92])
yh
ya
bh
ba
cc=corrcoef(Dexp,Dsim);
r2=cc(2,1)
And my functions is written in another file optASM1, as follows:
function f=optASM1(p)
Dexp=xlsread('Resultados experimentais.xlsx','N amoniacal','l7:l16');
texp=xlsread('Resultados experimentais.xlsx','N amoniacal','m7:m16');
global yh;
global ya;
global bh;
global ba;
yh=p(1);
ya=p(2);
bh=p(3);
ba=p(4);
%% Simulink
sim('ASM1_batelada');
Dsim=Snhout([2 25 41 49 67 77 83 86 90 92])
%% Cost-function
f=sum((Dexp-Dsim).^2)
Please, need help to solve this problem.

采纳的回答

Walter Roberson
Walter Roberson 2019-5-6
编辑:Walter Roberson 2019-5-6
You are passing [1,1] as the second argument to fminsearch(). That becomes the input to the first call to optASM1, where it becomes the p argument. You attempt to access p(3) and p(4) but that input only has two values. If you access 4 values in your function then your initial input needs to be length 4 or more.
We recomment that you do not use global; it is often difficult to debug problems where global variables are used.
  3 个评论
Walter Roberson
Walter Roberson 2019-5-7
Could you post your current code, and the xlsx file?
Nara Salles
Nara Salles 2019-5-8
Hi Walter, I have stopped the optimazation and changed the .m file.
Removing global as you recommended, it is working perfectly.
I am very thankful!!. Best regards, Nara.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Environment Customization 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by