Clear Filters
Clear Filters

Passing symprod into fmincon error

1 view (last 30 days)
I'm trying to pass a symprod() as the function variable for fmincon() and getting quite a few errors.
For example, if my code is like so:
func = @(t, x, z) symprod(abs(u - t), u, x, z);
fmincon(func(1, 2, 4), 0, [], [], [], [], -1, 1)
I get the errors
Error using optimfcnchk (line 93)
If FUN is a MATLAB object, it must have an feval method.
Error in fmincon (line 399)
funfcn = optimfcnchk(FUN,'fmincon',length(varargin),funValCheck,flags.grad,flags.hess,false,Algorithm);
Error in A6test (line 4)
fmincon((func(1, 2, 4)), 0, [], [], [], [], -10, 10)
So, looking at the first error, I discovered I could us matlabFunction() to bypass it. Naturally, I tried that:
syms u
func = @(t, x, z) symprod(abs(u - t), u, x, z);
fmincon(matlabFunction(func(1, 2, 4)), 0, [], [], [], [], -1, 1)
However, now I get a set of different errors
Error using symengine>@()6.0
Too many input arguments.
Error in fmincon (line 536)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in A6test (line 4)
fmincon(matlabFunction(func(1, 2, 4)), 0, [], [], [], [], -10, 10)
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
The first of these is pretty self-explanatory but I am pretty sure that the amount of variable inputs is correct. Thus, it leaves me thinking that matlabFunction() does not work here and I need a different solution to my problem, or a whole different method of tackling how to pass symprod() to fmincon(). Any and all help would be greatly appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Nov 2016
After your symprod over u is done, the variable u will no longer be present in the result.
You are passing in specific numeric t, x, z. The u will be iterated over the specific values from the specific x to z range, and the subtraction of that specific u from the numeric t is going to give a definite value. Multiply them all together and the func(1,2,4) gives a symbolic numeric result, a constant expression.
By default, matlabFunction applied to symbolic constant expressions notices that there are no free variables and so builds an anonymous function with no inputs.
You could use the 'vars' parameter of fmincon to force it to have a parameter, but that parameter would not take part in the value of the constant. minimizing a constant is not useful.
Perhaps,
fmincon( @(t) double(func(t, 2, 4)), 0, [], [], [], [], -1, 1)
  2 Comments
Victor Yuan
Victor Yuan on 11 Nov 2016
In the example you posted, to my untrained eyes, it looks like the value being minimized is t, when I wanted u to be.
To clarify my original question, I want u to be the one to be minimized. t, x and z are just variable constants that affect the function in symprod().
If your example actually does still minimize u, could you either explain why or link me to an article explaining its syntax and inner workings? Thanks a lot.
Walter Roberson
Walter Roberson on 11 Nov 2016
You are correct that my suggestion minimizes over t, minimizing the symbolic product. You cannot minimize on u.
The syntax
result = symprod(abs(u - t), u, x, z);
is like
u_vals = x : z;
for uidx = 1 : u_vals
u = u_vals(uidx);
temp(uidx) = abs(u-t);
end
result = prod(temp);
to the extent that you could meaningfully minimize on u, that would be like minimizing x, in which case there is no point in carrying out the product, just
fmincon(@(x) x, ....)
which would of course lead you to -inf if there are no constraints and the lower bound of the constraint if there are constraints.

Sign in to comment.

More Answers (1)

Alan Weiss
Alan Weiss on 11 Nov 2016
There are several possible problems here, but the main one is that fmincon, like all other optimization solvers, takes a single variable of optimization, usually called x. If you have several variables, have them be components of the vector x. See the documentation on writing objective functions or the getting started example. And, since you seem to want to have symbolic variables, you might want to look at an example of using Symbolic Math Toolbox™ with Optimization Toolbox™ Solvers (there are others in the documentation as well, just take a look).
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

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

Start Hunting!