Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parenthe

fun = @(x) (0.00 0.00 0.01 -0.01 -0.01 -0.05 0.12 0.16 0.20 0.05 -0.02 0.11 0.06 0.04 -0.08 0.43 -0.26 -0.18 -0.12 -0.12);
x0 = [21.70 2.40 7.60 22.80 30.00 5.80 11.20 6.20 8.20 3.50 9.00 3.20 9.50 2.20 17.50 3.20 8.70 3.50 2.40 10.60];
A = [];
b = [];
Li = [21.70 2.40 7.60 22.80 30.00 5.80 11.20 6.20 8.20 3.50 9.00 3.20 9.50 2.20 17.50 3.20 8.70 3.50 2.40 10.60];
Aeq = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
beq = 0;
lb = -0.1*Li;
ub = 0.1*Li;
[x fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)

Answers (2)

fun = @(x) [0.00 0.00 0.01 -0.01 -0.01 -0.05 0.12 0.16 0.20 0.05 -0.02 0.11 0.06 0.04 -0.08 0.43 -0.26 -0.18 -0.12 -0.12];
%^^ use []
use ' [ ' insead of ' ( '

3 Comments

HI VBBV when i use '[]' instead of '()' i am getting following error.
Error using fmincon
Supplied objective function must return a scalar value.
Error in test_case30 (line 13)
[x fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Hi akash,
K = [0.00 0.00 0.01 -0.01 -0.01 -0.05 0.12 0.16 0.20 0.05 -0.02 0.11 0.06 0.04 -0.08 0.43 -0.26 -0.18 -0.12 -0.12];
[x fval] = fmincon(@fun,x0,A,b,Aeq,beq,lb,ub)
function x = fun(K)
% do something here
% return x as scalar
end
The function defined has no variables except the vector. Instead define a function and call it with function handle as argument shown above, passing the vector

Sign in to comment.

This is not valid MATLAB syntax. I've commented it out so that later lines of code in this answer can run.
% fun = @(x) (0.00 0.00 0.01 -0.01 -0.01 -0.05 0.12 0.16 0.20 0.05 -0.02 0.11 0.06 0.04 -0.08 0.43 -0.26 -0.18 -0.12 -0.12);
If you wanted fun to be a function handle that returns that specific vector use [] to create the vector.
fun = @(x) [0.00 0.00 0.01 -0.01 -0.01 -0.05 0.12 0.16 0.20 0.05 -0.02 0.11 0.06 0.04 -0.08 0.43 -0.26 -0.18 -0.12 -0.12];
fun(42)
ans = 1×20
0 0 0.0100 -0.0100 -0.0100 -0.0500 0.1200 0.1600 0.2000 0.0500 -0.0200 0.1100 0.0600 0.0400 -0.0800 0.4300 -0.2600 -0.1800 -0.1200 -0.1200
But that function is not going to satisfy the requirements fmincon imposes on the function you pass into it. Without knowing what problem you're trying to solve it's going to be impossible to say how to modify your fun function to solve it.

2 Comments

Hi steven,
I am trying to minimize the objective function. The objective function is
max ( x1 * 0.00 + x2* 0.00 + x3*0.01 ------------------x20*(-0.12) )
the values of x are [21.70 2.40 7.60 22.80 30.00 5.80 11.20 6.20 8.20 3.50 9.00 3.20 9.50 2.20 17.50 3.20 8.70 3.50 2.40 10.60]
the limit for x is -0.1*Li to +0.1Li
I'm not sure that I understand you, but your objective function might be best coded as
fun = dot(x,Li)
But I am not sure exactly what you are doing. Is it linear programming? If so, see the linprog documentation.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Asked:

on 8 Dec 2022

Edited:

on 10 Dec 2022

Community Treasure Hunt

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

Start Hunting!