Error using fminsearch and optimset

조회 수: 4 (최근 30일)
Nara Salles
Nara Salles 2019년 5월 5일
댓글: Nara Salles 2019년 5월 8일
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