Error using fminsearch and optimset

4 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 6 May 2019
Edited: Walter Roberson on 6 May 2019
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 Comments
Walter Roberson
Walter Roberson on 7 May 2019
Could you post your current code, and the xlsx file?
Nara Salles
Nara Salles on 8 May 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Environment Customization in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!