Passing symprod into fmincon error
1 view (last 30 days)
Show older comments
Victor Yuan
on 11 Nov 2016
Commented: Walter Roberson
on 11 Nov 2016
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.
0 Comments
Accepted Answer
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
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.
More Answers (1)
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
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!