# Find the maximum value of a function of $x$ for a given range of values of $x$

What is the most efficient and reasonable way to find the maximum value of a function of $x$ within a given range of $x$ values?

For example, given the function $f(x)=3\sin(x)+0.01x^2$, how can I find the maximum between $x=0$ and $x=37$, inclusive. I'm not really looking for the specific answer to this, but the general principle that can be used for any equation. I seem to have forgotten how to do this.

-

You take the derivative $f^\prime$ and find out the points $x$ where it's zero: $f^\prime (x) \stackrel{!}{=} 0$

$f^\prime (x)$ is the slope at $x$. If it's zero it means that you have a found either a minimum, a maximum or a saddle point. You're not interested in saddle points though so you want to check that the points where $f^\prime$ is zero also have the property that $f^{\prime \prime}$ is non-zero. $f^{\prime \prime}$ gives you a measure for the "curvature" of $f$ at that point. Saddle points are horizontal hence have $f^{\prime \prime}$ equal to zero.

Edit As pointed out in the comment by Ronald, $f^{\prime \prime} = 0$ is not a sufficient condition to be at a saddle point. There are functions that satisfy this condition at points where they don't have a saddle point, such as for example $f(x) = x^4$ at $0$. In general you need to look at the first derivative that's non-zero, this is called "higher-order derivative test".

Note that for example if $f$ is a function like $f(x) = x$ then it won't have any points where $f^\prime$ is zero and yet it will attain a maximum at the right end of your interval. To detect this case you simply have to remember to also check $f(a)$ and $f(b)$ if your function is defined on $[a,b]$.

For this you need $f$ to be differentiable. Luckily, your example and probably many of the functions you'll have to find mins and maxs for will be differentiable.

-
strictly speaking $f'=0$ and $f'' = 0$ does not necessarily indicate a saddle point. – Ronald Apr 12 '12 at 23:38
@Ronald Thank you for pointing this out, I added it to my answer. – Rudy the Reindeer Apr 13 '12 at 7:48

I believe the question has not been answered.

E.g. if you take the function x(x-2)^2 and impose the restriction that x is between 0 and 3 then the calculus style maximum by differentiation is at (0.67,1.19) but the true maximum within the range is (3,3) - simply because the function is largest at that point, even though the gradient is not zero.

Unfortunately - I don't know the general answer to this question - I came here looking to find the answer!

Oh. I am so stupid: realised after that the largest value must either be the calculus maximum, or at an endpoint of the range. There are no other possibilities - hence the question is quite simple in the end.

-

For a differentiable function, you can take the derivative, check all points where it is zero (or do a second derivative test) and check the endpoints of the interval. In this particular case, you can't analytically solve for zeros of the derivative. You can search numerically for zeros of the derivative or for maxima of the function.

-