Main Content

Maximizing an Objective

All solvers attempt to minimize an objective function. If you have a maximization problem, that is, a problem of the form

max x f ( x ) ,

then define g ( x ) = f ( x ) and minimize g .

For example, to find the maximum of tan ( cos ( x ) ) near x = 5 , evaluate

[x, fval] = fminunc (@ (x)谭(cos (x)), 5)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 6.2832
fval = -1.5574

The maximum is1.5574(the negative of the reportedfval), and occurs atx = 6.2832. This answer is correct because, to five digits, the maximum is tan ( 1 ) = 1 . 5 5 7 4 , which occurs at x = 2 π = 6 . 2 8 3 2 .