What's wrong with my objective function?

1 回表示 (過去 30 日間)
Richárd Tóth
Richárd Tóth 2019 年 8 月 3 日
コメント済み: Star Strider 2019 年 8 月 3 日
Hello
When I run this line of code,
[d,fval] = fseminf(@objfun,x0,numOfInequations,@semInfConstraints);
I get the following error:
Input arguments to function include colon operator. To input the colon character, use ':' instead.
Error in fseminf (line 424)
initVals.f = initVals.f(:);
Error in optimize (line 29)
[d,fval] = fseminf(@objfun,x0,numOfIneqs,@semInfConstraints);
Here is my objective function:
function f = objfun(D,s)
global numOfVars
% Objective function, -1*D(1)*D(2)*...*D(n) /n dimension/
str = 'f=@(D) -1*';
dimension = numOfVars;
for i=1:dimension
if i<dimension
str = join([str 'D(' string(i) ')*'],'');
else
str = join([str 'D(' string(i) ');'],'');
end
end
eval(str);
  3 件のコメント
Richárd Tóth
Richárd Tóth 2019 年 8 月 3 日
Okay, I think the problem is
str = 'f=@(D) -1*';
By removing the @(D), I no longer get the error message about the colon.
Star Strider
Star Strider 2019 年 8 月 3 日
If you want to create a functon from a string, use the str2func function. It likely does everything you’re doing, however it’s more straightforward. Also,using sprintf to create your function strings is likely more efficient than concatenating strings.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Programming and Mixed-Integer Linear Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by