Minimize vector using Fmincon

19 ビュー (過去 30 日間)
Melvin Corvers
Melvin Corvers 2020 年 6 月 8 日
編集済み: Melvin Corvers 2020 年 6 月 9 日
I want to minimize a cost function using Fmincon. My goal is to minimize "obj" such that my model has the best fit to the experimental data.
C = importdata('Experiment6.txt')
S = C(:,1); % Column 84x1; experimental data
L = C(:,2); % Column 84x1; experimental data
F = C(:,3); % Column 84x1; experimental data
MEdp = 0; % Fixed
MEp = 196; % Fixed
E = ones(1,11); % Initial guess (Xo)
t = logspace(-4, 1,11); % Fixed vector
Eoptimized = fmincon(@objective, E, [],[])
function obj = objective(E, MEdp, t, MEp, F, L, S)
% Calculate model, returns 84x1 columns
for i = 1:length(t)
MEp = MEp + ( E(i) * t(i).^2 * F.^2) ./ ( 1 + ( t(i)^2 * F.^2) );
MEdp = MEdp + ( E(i) * t(i)* F) ./ ( 1 + ( t(i)^2 * F.^2) );
end
% Cost function
obj = sum ( ( ( (L - MEdp) -1 ).^2 ) + ( ( (S - MEp) -1 ) .^2 ) );
end
Objection returns a single value. I want to have E returned as an optimized vector. However, when I try to run the the code it states that it does not have enough input arguments.
% Without this code the function works propely:
Eoptimized = fmincon(@objective, E, [],[])
The following error message appears:
Not enough input arguments.
Error in DiscreteSpectrum>objective(line 264)
for i = 1:length(t)
Error in fmincon (line 552)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in DiscreteSpectrum (line 244)
Eoptimized = fmincon(@objective, E, [], []);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.

採用された回答

Matt J
Matt J 2020 年 6 月 8 日
編集済み: Matt J 2020 年 6 月 8 日
You are not passing the fixed parameter variables MEdp, t, MEp, F, L, S to the objective. One way to do so is,
fun=@(E) objective(E, MEdp, t, MEp, F, L, S);
Eoptimized = fmincon(fun, E, [],[])
  1 件のコメント
Melvin Corvers
Melvin Corvers 2020 年 6 月 8 日
Thank you very much! It worked.

サインインしてコメントする。

その他の回答 (1 件)

Alan Weiss
Alan Weiss 2020 年 6 月 8 日
I think that you called the objective function incorrectly. Try
Eoptimized = fmincon(@(E)objective(E, MEdp, t, MEp, F, L, S), E)
That said, I think you would do better to use the correct solver for this problem, namely lsqnonlin. You will find that lsqnonlin is faster and converges better than fmincon for this problem.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 件のコメント
Melvin Corvers
Melvin Corvers 2020 年 6 月 8 日
編集済み: Melvin Corvers 2020 年 6 月 9 日
Thank you very much. Your solution works. The function lsqnonlin is indeed much better for this problem.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNonlinear Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by