MATLAB: Passing symprod into fmincon error

fminconfunction functionssymprod

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.

Best Answer

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)
Related Question