I have a quadratic function f where, f = function (x) {2+.1*x+.23*(x*x)}
. Let's say I have another quadratic fn g where g = function (x) {3+.4*x-.60*(x*x)}
Now, I want to maximize f given the constraints 1. g>0
and 2. 600<x<650
I have tried the packages optim
, constrOptim
and optimize
. optimize
does one dim. optimization, but without constraints and constrOptim
I couldn't understand. I need to this using R. Please help.
PS In this example, the values may be erratic as I have given two random quadratic functions, but basically I want maximization of a quadratic fn given a quadratic constraint.
If you solve g(x)=0
for x
by the usual quadratic formula then that just gives you another set of bounds on x
. If your x^2
coefficent is negative then g(x) > 0
between the solutions, otherwise g(x)>0
outside the solutions, so within (-Inf, x1) and (x2, Inf).
In this case, g(x)>0
for -1.927 < x < 2.59
. So in this case both your constraints cannot be simultaneously achieved ( g(x)
is LESS THAN 0 for 600<x<650
).
But supposing your second condition was 1 < x < 5
, then you'd just combine the solution from g(x)>0
with that interval to get 1 < x < 2.59
, and then maximise f
in that interval using standard univariate optimisation.
And you don't even need to run an optimisation algorithm. Your target f
is quadratic. If the coefficient of x^2
is positive the maximum is going to be at one of your limits of x, so you only have a small number of values to try. If the coefficient of x^2
is -ve then the maximum is either at a limit or at the point where f(x)
peaks (solve f'(x)=0
) if that is within your limits.
So you can do this precisely, there's just a few conditions to test and then some intervals to compute and then some values of f
at those interval limits to calculate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.