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

4 次查看(过去 30 天)
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

采纳的回答

Alan Weiss
Alan Weiss 2018-9-17
编辑:Alan Weiss 2018-9-17
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 个评论
Matt J
Matt J 2018-9-17
编辑:Matt J 2018-9-17
You can do all of that non-symbolically, e.g.,
t=(1:5);
tsum=sum(t.^3),
Moreover, since you have a non-linear optimization problem, you are going to need to implement this way because the non-linear solvers in the Optimization Toolbox are all numeric (not symbolic) solvers.
Walter Roberson
Walter Roberson 2018-9-17
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.)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by