Fmincon solution is same as initial guess

Hello,
My Fmincon function does not minimize the cost function, instead, it reports the same initial guess provided to it. Please help with recommendations to the piece of code below:
function J=cost_function(y)
for i=1:size(a,1)
AA = y0*b(i,:)';
BB = y0*c(i,:)';
CC = (y0(1).*c(i,1)/BB)*c(i,1) + (y0(2).*c(i,2)/BB)*c(i,2);
DD = y0*d(i,:)';
EE = y0*e(i,:)';
all(i,:)=[AA, BB, CC, DD, EE];
end
J=sum(abs(all(1,:) - x(1:5)') ./ x(1:5)')^2;
The script calculates the a, b, c, d, e,....... and passes them to the function along with "y0".
Please advise, and thanks in advance

 採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 7 月 14 日
編集済み: Sulaymon Eshkabilov 2021 年 7 月 14 日

0 投票

THere are a couple of errs oin your fcn, i.e. y0, a,b,c,d not defined. Moreover, x is not defined.
function J=cost_function(y0, a, b, c, d, e, x)
for i=1:size(a,1)
AA = y0*b(i,:)';
BB = y0*c(i,:)';
CC = (y0(1).*c(i,1)/BB)*c(i,1) + (y0(2).*c(i,2)/BB)*c(i,2); % Check: multiplied and divided by c(i,1) and c(i,2)
DD = y0*d(i,:)';
EE = y0*e(i,:)';
all(i,:)=[AA, BB, CC, DD, EE];
end
J=sum(abs(all(1,:) - x(1:5)') ./ x(1:5)')^2;

4 件のコメント

Samy Alkhayat
Samy Alkhayat 2021 年 7 月 14 日
All of these are defined in the script that calls the function. Should they be defined inside the function?
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 7 月 14 日
yes.
Samy Alkhayat
Samy Alkhayat 2021 年 7 月 14 日
Thank you very much.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 7 月 15 日
Most welcome!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeNonlinear Optimization についてさらに検索

製品

リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by