problem running fmincon, 'Input arguments to function include colon operator.'

I am trying to run fmincon and i get this error:
'Input arguments to function include colon operator.
To input the colon character, use ':' instead.'
I cannot understand why I get this error.
My code is below.
Anyone any ideas? Thank you.
% code
low_b = zeros(size(param));
options = optimoptions('fmincon', ...
'Algorithm','interior-point', ...
'DerivativeCheck', 'on',...
'GradObj', 'on',...
'HessFcn', @hessfh, ...
'Hessian', 'user-supplied',...
'Display','final');
[xfinal,fval,exitflag,output] = fmincon(@fh,[0.1;0.1],[],[],[],[],low_b,[],[],options);
% code
function [f,gradfh]=fh(param)
syms param1 param2 real
param=[param1;param2];
f3 =((191352748222723/(7128637342969177/1125899906842624)^((param1 - 1)/param1)/(7128637342969177/1125899906842624)^(1/param1))/9511602413006487552 + ((2733703199318633/70368744177664 - param2)^(1 - param1)*(2733703199318633/70368744177664 - param2)^param1)/(561*(param2 - 2733703199318633/70368744177664)))^2 + (log(2733703199318633/70368744177664 - param2)/561 + 2074186557694359/(1125899906842624*param1) - log(-(param2 - 2733703199318633/70368744177664)*(2733703199318633/70368744177664 - param2)^param1)/(2733703199318633/70368744177664 - param2)^param1)^2
f = matlabFunction(f3,'vars',{param});
gradf3 = jacobian(f3,param).';
gradfh = matlabFunction(gradf3,'vars',{param});
end
% code
function hessf=hessfh(param)
syms param1 param2 real
param=[param1;param2]
f3 =((191352748222723/(7128637342969177/1125899906842624)^((param1 - 1)/param1)/(7128637342969177/1125899906842624)^(1/param1))/9511602413006487552 + ((2733703199318633/70368744177664 - param2)^(1 - param1)*(2733703199318633/70368744177664 - param2)^param1)/(561*(param2 - 2733703199318633/70368744177664)))^2 + (log(2733703199318633/70368744177664 - param2)/561 + 2074186557694359/(1125899906842624*param1) - log(-(param2 - 2733703199318633/70368744177664)*(2733703199318633/70368744177664 - param2)^param1)/(2733703199318633/70368744177664 - param2)^param1)^2
gradf3 = jacobian(f3,param).';
hessf3 = jacobian(gradf3,param);
hessf = matlabFunction(hessf3,'vars',{param});
end

 採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 1 日

0 投票

The second output of the objective function should be the gradient evaluated at the input point, not a function handle. Likewise the hessian should be the value of the hessian, not a function handle.

11 件のコメント

If I do not use the gradient and hessian as function handle,
then i get the error from 'deal'.
'Error using deal
The number of outputs should match the number of inputs.'
Any suggestions in this situation?
% code
syms param1 param2
param=[param1;param2];
f3 =((191352748222723/(7128637342969177/1125899906842624)^((param1 - 1)/param1)/(7128637342969177/1125899906842624)^(1/param1))/9511602413006487552 + ((2733703199318633/70368744177664 - param2)^(1 - param1)*(2733703199318633/70368744177664 - param2)^param1)/(561*(param2 - 2733703199318633/70368744177664)))^2 + (log(2733703199318633/70368744177664 - param2)/561 + 2074186557694359/(1125899906842624*param1) - log(-(param2 - 2733703199318633/70368744177664)*(2733703199318633/70368744177664 - param2)^param1)/(2733703199318633/70368744177664 - param2)^param1)^2;
gradf3 = jacobian(f3,param).';
fh = matlabFunction(f3,gradf3,'vars',{param});
hessf3 = jacobian(gradf3,param);
hessfh = matlabFunction(hessf3,'vars',{param});
low_b = zeros(size(param));
options = optimoptions('fmincon', ...
'Algorithm','interior-point', ...
'DerivativeCheck', 'on',...
'GradObj', 'on',...
'HessFcn', hessfh, ...
'Hessian', 'user-supplied',...
'Display','final');
[xfinal,fval,exitflag,output] = fmincon(fh,[0.1;0.1],[],[],[],[],low_b,[],[],options);
Walter Roberson
Walter Roberson 2017 年 1 月 1 日
Your objective should only return the second output when it is requested. You will need to use a utility function to make the decision.
Ekin Ceyda Cetin
Ekin Ceyda Cetin 2017 年 1 月 1 日
In the example, it uses function handle. That is so confusing. Is it possible to make the objective function return the second output when it is requested without a function handle?
Thank you for all the answers btw.
You have to pass a function handle to fmincon. However the function being called needs to check nargout and not return a second value when it is not asked for. When you use deal() it always wants as many left hand outputs as you have on the right hand.
You can write a small auxiliary function to do the work.
function [a, b] = oneOrTwo(t, y, f, g)
a = f(t, y);
if nargout > 1
b = g(t, y) ;
end
Then
fmincon(@(t, y) oneOrTwo(t, y, f, gradfh),......)
I tried to do as you say, I am getting colon operator error as I get in the beginning.
'Input arguments to function include colon operator. To input the colon character, use ':' instead.'
What am I doing wrong? Obviously I am missing sth but don't know what.
% code
syms param1 param2
param=[param1;param2];
f3 =((191352748222723/(7128637342969177/1125899906842624)^((param1 - 1)/param1)/(7128637342969177/1125899906842624)^(1/param1))/9511602413006487552 + ((2733703199318633/70368744177664 - param2)^(1 - param1)*(2733703199318633/70368744177664 - param2)^param1)/(561*(param2 - 2733703199318633/70368744177664)))^2 + (log(2733703199318633/70368744177664 - param2)/561 + 2074186557694359/(1125899906842624*param1) - log(-(param2 - 2733703199318633/70368744177664)*(2733703199318633/70368744177664 - param2)^param1)/(2733703199318633/70368744177664 - param2)^param1)^2;
gradf3 = jacobian(f3,param).';
hessf3 = jacobian(gradf3,param);
hessfh = matlabFunction(hessf3,'vars',{param});
low_b = zeros(size(param));
options = optimoptions('fmincon', ...
'Algorithm','interior-point', ...
'DerivativeCheck', 'on',...
'GradObj', 'on',...
'HessFcn', hessfh, ...
'Hessian', 'user-supplied',...
'Display','final');
[xfinal,fval,exitflag,output] = fmincon(@(param) fh(param, f, gradfh),[0.1;0.1],[],[],[],[],low_b,[],[],options);
% code
function [f,gradfh]=fh(param, f, gradfh)
syms param1 param2
param=[param1;param2];
f3 =((191352748222723/(7128637342969177/1125899906842624)^((param1 - 1)/param1)/(7128637342969177/1125899906842624)^(1/param1))/9511602413006487552 + ((2733703199318633/70368744177664 - param2)^(1 - param1)*(2733703199318633/70368744177664 - param2)^param1)/(561*(param2 - 2733703199318633/70368744177664)))^2 + (log(2733703199318633/70368744177664 - param2)/561 + 2074186557694359/(1125899906842624*param1) - log(-(param2 - 2733703199318633/70368744177664)*(2733703199318633/70368744177664 - param2)^param1)/(2733703199318633/70368744177664 - param2)^param1)^2
f = matlabFunction(f3,'vars',{param})
if nargout > 1 % gradient required
gradf3 = jacobian(f3,param).';
gradfh = matlabFunction(gradf3,'vars',{param});
end
end
Walter Roberson
Walter Roberson 2017 年 1 月 2 日
Your fh is returning function handles. It needs to return numeric values.
Do you mean, i need to use the function handle just to make the choice of calculating only objective function or calculating gradient as well if necessary? I tried that too, not enough input arguments. Is this what you mean?
So sorry for being such a trouble :'
% code
syms param1 param2
param=[param1;param2];
f3 =((191352748222723/(7128637342969177/1125899906842624)^((param1 - 1)/param1)/(7128637342969177/1125899906842624)^(1/param1))/9511602413006487552 + ((2733703199318633/70368744177664 - param2)^(1 - param1)*(2733703199318633/70368744177664 - param2)^param1)/(561*(param2 - 2733703199318633/70368744177664)))^2 + (log(2733703199318633/70368744177664 - param2)/561 + 2074186557694359/(1125899906842624*param1) - log(-(param2 - 2733703199318633/70368744177664)*(2733703199318633/70368744177664 - param2)^param1)/(2733703199318633/70368744177664 - param2)^param1)^2;
f = matlabFunction(f3,'vars',{param});
gradf3 = jacobian(f3,param).';
gradfh = matlabFunction(gradf3,'vars',{param});
hessf3 = jacobian(gradf3,param);
hessfh = matlabFunction(hessf3,'vars',{param});
low_b = zeros(size(param));
param0=[3.6;2.6];
options = optimoptions('fmincon', ...
'Algorithm','interior-point', ...
'DerivativeCheck', 'on',...
'GradObj', 'on',...
'HessFcn', hessfh, ...
[xfinal,fval,exitflag,output] = fmincon(@(param1, param2) oneOrTwo(param1, param2,f,gradfh),param0,[],[],[],[],low_b,[],[],options);
% code
function [a, b] = oneOrTwo(c, d, h, g)
syms c d
a = h(c,d);
if nargout > 1
b = g(c,d) ;
end
end
% code
syms param1 param2
param=[param1;param2];
f3 =((191352748222723/(7128637342969177/1125899906842624)^((param1 - 1)/param1)/(7128637342969177/1125899906842624)^(1/param1))/9511602413006487552 + ((2733703199318633/70368744177664 - param2)^(1 - param1)*(2733703199318633/70368744177664 - param2)^param1)/(561*(param2 - 2733703199318633/70368744177664)))^2 + (log(2733703199318633/70368744177664 - param2)/561 + 2074186557694359/(1125899906842624*param1) - log(-(param2 - 2733703199318633/70368744177664)*(2733703199318633/70368744177664 - param2)^param1)/(2733703199318633/70368744177664 - param2)^param1)^2;
f = matlabFunction(f3,'vars',{param});
gradf3 = jacobian(f3,param).';
gradfh = matlabFunction(gradf3,'vars',{param});
hessf3 = jacobian(gradf3,param);
hessfh = matlabFunction(hessf3,'vars',{param});
low_b = zeros(size(param));
param0=[3.6;2.6];
options = optimoptions('fmincon', ...
'Algorithm','interior-point', ...
'DerivativeCheck', 'on',...
'GradObj', 'on',...
'HessFcn', hessfh, ...
[xfinal,fval,exitflag,output] = fmincon(@(params) oneOrTwo(params, f,gradfh), param0, [], [], [], [], low_b, [], [], options);
% code
function [fun_val, grad_val] = oneOrTwo(params, fun_h, grad_h)
fun_val = fun_h(params);
if nargout > 1
grad_val = grad_h(params) ;
end
Ekin Ceyda Cetin
Ekin Ceyda Cetin 2017 年 1 月 3 日
I should have done it with three inputs in the function handle, I see now. Thank you for the answers.
Rustem Devletov
Rustem Devletov 2019 年 4 月 26 日
Hello, friend. I'm really really feeling despondent. I don't know how to implement the following idea. My supervisor told me to supple the gradient to a function from another file. How can I implement this? Can you suggest anything?

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by