I would like to maximize a function a little bit complex with some constraint:
The function is mathematically similar to this one (I write this one to simplify the explanations):
f(x, y, z, t) = arcsin (x/2t)/(y + x + max (1, z/t))
with z, t between 0 and 1, x between 1 and 2, and y greater than 1/x^2.
Could you give me the code to compute the numerical value for x, y, z and t to maximize this function? From this code, I will derive how the optimisation function should be used.
you can use fmincon
and minimize the additive inverse of your objective function. your constraint ("y greater than 1/x^2") is nonlinear so you should use the nonlcon
argument of fmincon
:
% function definition (minus sign to maximize instead of minimize)
f = @(x) - asin(x(1)/(2*x(4)))/(x(2) + x(1) + max(1, x(3)/x(4)) );
lb = [1 -inf 0 0];% lower bound for [x y z t]
ub = [2 inf 1 1];% upper bound for [x y z t]
x0 = [1.5 0 0.5 0.9]; % initial vector
% minimization
x = fmincon(f,x0,[],[],[],[],lb,ub,@mycon);
where mycon
defines your constraint:
function [c,ceq] = mycon(x)
% y > 1/x^2
% 1/x^2 - y < 0
c = 1/x(1)^2 - x(2);
ceq = 0;
end
you can also pass an options argument to specify optimization options.