Error: Operator '-' is not supported for operands of type 'function_handle'.

Hello, i have the following Code:
f1 = [-1,-1];
f2=@(x)-10*x(1)+x(1)^2-4*x(2)+x(2)^2;
A = [3 1;
2 1 ;
1 2];
b = [12; 9; 12];
z = [0,0];
d = [-1,0];
Method(f1,f2,A,b,z,d);
function [x,alpha] = Method(f1,f2,A,b,z,d)
f =@(x)-x(size(A,2)+1);
x0 = (zeros(1,size(A,2) + 1));
lb = [(zeros(1,size(A,2))),-Inf];
if (~isempty(f1))
Anew = [f1,-d(1)];
Anew(2:size(A,1)+1,1:size(A,2)) = A;
bnew = [z(1);b];
c1 =@(x) f2 - d(2)*x(size(A,2)+1) - z(2);
(else ... %in this case Anew,bnew and c1 would look different)
end
nonlincon = @constr;
function [c,ceq] = constr(x)
c = c1;
ceq = [];
end
[x,alpha]=fmincon(f,x0,Anew,bnew,[],[],lb,[],nonlincon);
end
This gives me the following error: Operator '-' is not supported for operands of type 'function_handle'.
I know that it is because of the line "c1 =@(x) f2 - d(2)*x(size(A,2)+1) - z(2);". Does someone knows how i could solve this problem? Thanks.

2 Comments

A general description of what you are trying to do with inputs and expected outputs would be helpful. I cannot follow your code (lots of problems).
It is a part of a specific Algorithm. I want to use the fmincon function to solve a nonlinear Problem. At the beginning f1, f2, A,b,z and d are given. But inside the function "Method" we want to change this problem still a bit before giving it into fmincon.

Sign in to comment.

 Accepted Answer

To be used in calculations, functions must be evaluated, and that means providing them with an argument.
See if this provides the desired result:
c1 =@(x) f2(x) - d(2)*x(size(A,2)+1) - z(2);
I am confindent that will work and not throw the previous error. I have not tested the code with this correction.
.

6 Comments

Hello, thank you for your answer. This gives me a new error: input arguments to function include colon operator. To input the colon character, use ':' instead.
But i dont know what it means.
These two lines also smell funny.
if (~isempty(f1))
Anew = [f1,-d(1)];
A function handle cannot be empty. It must be (as of I want to say release R14 back in 2004 but I'm not 100% sure on that; certainly for at least a decade) a scalar. So that first line of code is dead code.
Since you can't have a non-scalar function handle array, the second line I quoted above must error. In both places, like @Star Strider stated, you probably want to evaluate f1 with some inputs and check the output of that evaluation for emptyness before concatenating it with something else.
I only posted a part of my code because otherwise its too hard to understand. It could be that f1 = [] and this i want to exclude in this case. ( the second line is not giving an error). If i replace the line c = c1 with c = f2 - d(2)*x(size(A,2)+1) - z(2) my Code is working. But in the else cases c is different thats why i cannot do this.
My pleasure!
It means that I missed another error, and that is tha the ‘c1’ call does not provide it with an argument, so nothing appropriate results.
I assume that the call here should be:
c = c1(x);
so the complete function would be:
function [c,ceq] = constr(x)
c = c1(x);
ceq = [];
end
Also, in the ‘Method’ call, it will curently return only ‘x’ to the workspace, and it will assign it to the generic variable ‘ans’. To get the desired outputs, call it as:
[x,alpha] = Method(f1,f2,A,b,z,d)
That will return both outputs to the workspace, assigned appropriately to their variables.
MATLAB is superb, however it lacks the ability to infer intentions. It is necessary to tell it explicitly what you want it to do.
I have not run the code to test it as posted or or test it with these corrections to it.
.
As always my (our with the contribution by @Steven Lord) pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!