How can I express sum of an optimvar raised to a power

13 views (last 30 days)
I am getting an error saying [Undefined operator '^' for input arguments of type 'optim.problemdef.OptimizationVariable'.] when I write the following code:
x = optimvar('x');
syms d;
z = symsum(x^d,d,1,5);
can anybody tell me how to code it correctly

Accepted Answer

Alan Weiss
Alan Weiss on 17 Sep 2018
Edited: Alan Weiss on 17 Sep 2018
There are several problems with what you are trying to do. As the documentation explicitly states, the only supported operations on optimization variables are linear or quadratic ( x.^2 or x.*y sort of things). And quadratic was added in R2018b, which just came out, so unless you have the very latest software, you can use only linear expressions with optimization variables. In addition, you are attempting to mix symbolic math with optimization variables, and those two things are objects of different classes, and do not interoperate.
I'm glad that you like optimization variables, but currently their use is restricted to linear or quadratic problems.
Alan Weiss
MATLAB mathematical toolbox documentation
  3 Comments
Walter Roberson
Walter Roberson on 17 Sep 2018
The Symbolic Toolbox can be useful in creating formulae to be optimized.
In some cases it can be effective to do optimization symbolically, by differentiating and solving the result for 0.
In other cases, after generating the formula, you would use matlabFunction() to create an anonymous function that can evaluate a given position numerically.
Equation fitting can, in general, be handled by constructing the function f based upon symbolic vector x and symbolic parameters to be searched for, and then doing
OBJ = sum( (subs(f, x, KNOWN_X) - KNOWN_Y).^2 );
and then using matlabFunction() on OBJ, specifying the parameters with the 'vars' option using the {[Variables]} form. For example,
obj = matlabFunction(OBJ, 'vars', {[a, b, p]})
for an equation with parameters a, b, and p.
The resulting obj function will calculate the sum-of-squares residue for a row vector of coefficients, which you would then attempt to minimize. You would probably not use just plain fmincon for this purpose, as fmincon easily gets stuck in local minima, but you might use a global optimization technique, including potentially just multistart of fmincon (that is, evaluate at a number of different starting points.)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!