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 Center および File ExchangeSimulink Environment Customization についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by