# how to define sqrt in Optimization?

2 views (last 30 days)
kanuri venkata mohana on 31 Aug 2020
Answered: Walter Roberson on 2 Sep 2020
HI,
I would like to implemet sqrt and round functions in optimization. Can anyone help me how to do that?
objoptim=clOptimization;
w=opt(objoptim);
classdef clOptimization
properties
P=optimvar('P',2)
end
methods
function w=opt(obj)
y=obj.P(1);
z=sqrt(y*obj.P(2));
w=round(y*z);
end
end
end
This is just an example to know the syntax. Can anyone help me.

Alan Weiss on 1 Sep 2020
I'm not sure what you are talking about. If you are trying to extend the problem-based method, then the short answer is you are on your own, there is no documented way to extend the problem-based functions. If you are talking about something else, then please describe what you are trying to do in more detail.
Alan Weiss
MATLAB mathematical toolbox documentation

Walter Roberson on 2 Sep 2020
sqrt() is already defined for optimization expressions, at least in R2020a.
>> A = optimvar('A')
A =
OptimizationVariable with properties:
Name: 'A'
Type: 'continuous'
IndexNames: {{} {}}
LowerBound: -Inf
UpperBound: Inf
See variables with show.
See bounds with showbounds.
>> sqrt(A)
ans =
Nonlinear OptimizationExpression
sqrt(A)
Be careful though, as sqrt() is not differentiable at 0.
I do not currently see any way to achieve round() using the supported functions.
round() is not differentiable at integers (and has a derivative of 0 at real values that are not integers).
Remember that the underlying optimization algorithms assume differentiability for any variable not marked as integer.
The supported integer constraints involve using intlinprog() solver, which does not support non-linear objectives -- the objective must be resolvable to a matrix multiplication, not to a generalized function.
If you need a generalized function as the objective, then you cannot turn it into an effectively integer-constraint problem by using round(): the optimization functions will fail badly on it, thinking that the objective function is flat near the integer locations.
If you need non-linear functions plus integer constraints, then you should be using ga() or gamultiobj() or paretosearch()