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

7 views (last 30 days)
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)

VBBV
VBBV on 8 Dec 2022
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
VBBV
VBBV on 10 Dec 2022
Edited: VBBV on 10 Dec 2022
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.


Steven Lord
Steven Lord on 8 Dec 2022
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
Alan Weiss
Alan Weiss on 9 Dec 2022
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.

Community Treasure Hunt

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

Start Hunting!