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

조회 수: 4 (최근 30일)
Jijo Varghese
Jijo Varghese 2018년 9월 17일
댓글: Walter Roberson 2018년 9월 17일
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