Hello,
I am writing an algorithm for a project of mine, and at some point I need to find the value of alpha that will minimize a function subject to constraints.
This is what I have
x1=xi(1)+gammai(1)*alpha;
x2=xi(2)+gammai(2)*alpha;
x3=xd(1)+deltad(1)*alpha;
x4=xd(2)+deltad(1)*alpha;
falpha =matlabFunction((exp(x1))*(4*(x1).^2 + 2*(x2).^2 + 4*(x1)*(x2) + 2*(x2) + 1) + 0*x3 + 0*x4);
c=matlabFunction([-10-x1; x1+9; -x2+1;x2-1.5;-x3;x3-1;-x4;x4-1]);
ceq=matlabFunction([x1*x2-x1-x2+x3+1.5;x1*x2-x4+10]);
const=[c, ceq];
[alpha]=fmincon(falpha,0,[],[],[],[],[],[],const);
This is the error I get: Error using horzcat Nonscalar arrays of function handles are not allowed; use cell arrays instead.

 採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 25 日

1 投票

const = @(alpha) deal(c(alpha), ceq(alpha));

1 件のコメント

Edgard El Cham
Edgard El Cham 2017 年 10 月 25 日
Thank you, it worked!

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

その他の回答 (1 件)

Matt J
Matt J 2017 年 10 月 25 日
編集済み: Matt J 2017 年 10 月 25 日

0 投票

As the error says, your const argument must be a single function handle returning two arguments [c,ceq]. You instead have passed two separate function handles.

4 件のコメント

Edgard El Cham
Edgard El Cham 2017 年 10 月 25 日
How can I fix const so that the fmincon works? I do not want you to solve it for me, maybe give me an example, if possible. Thanks
Matt J
Matt J 2017 年 10 月 25 日
function [c,ceq]=myConstraints(alpha)
....
Edgard El Cham
Edgard El Cham 2017 年 10 月 25 日
Thing is I need to run it in a loop, with changing constraints.
Matt J
Matt J 2017 年 10 月 25 日
That can be done by passing fixed parameters to the constraints.

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

Community Treasure Hunt

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

Start Hunting!

Translated by