# How do I find the minimum of a function? (Quadratic function with square root component)

I have a function that I am trying to find the minimum of. I know how to solve for the minimum of a quadratic function as well as a square root function. I don't know how to solve for the minimum of a function that has both of those elements. I have simplified the function as much as I can and it has come to this.

$$y = \sqrt{-3x^2-10x+225-40\sqrt{25-x^2}}$$

What is the procedure I must do to find the minimum?

Here is a general formula. I am trying to find the value of $$x$$ that minimises $$y$$.

$$y=\sqrt{(x-a)^2+(\frac{h}{w}\sqrt{w^2-x^2}-b)^2}$$

• Calculus? ${}{}$ – Tavish Apr 3 at 10:47
• Do you need to find minimum analytically or numerical methods are accepted? – Yalikesifulei Apr 3 at 10:47
• Have you tried $x=5\sin t$? – J.G. Apr 3 at 10:51
• I'm not sure what you mean by analytically or numerically. I am contributing to a game and I just need to be able to program in a formula that will spit out the minimum value of this function. I chose arbitrary values to see if I could solve it, then I would create my proper formula. I could iterate through many x values and choose the minimum y as an approximation but that would be bad practice. – Phedg1 Apr 3 at 10:53
• @J.G. What is t? – Phedg1 Apr 3 at 10:58

To minimize $$y^2=(x-a)^2+(\tfrac{h}{w}\sqrt{w^2-x^2}-b)^2$$ algebraically requires finding its turning points by solving $$\frac{dy^2}{dx}=0$$. This reduces to solving a quartic equation. Although quartics admit a solution in radicals, it's rarely worth using. It's probably best to use numerical minimization instead; it's not as expensive as you might think. Here's a Python solution:

from numpy import sqrt#Imported from numpy instead of math in case you need to parallelize
from scipy.optimize import minimize_scalar

def best_x(a, b, h, w):
f = lambda x: (x-a)**2+(h/w)*sqrt(w**2-x**2)-b**2)**2
max_x = abs(w)
return minimize_scalar(f, bounds=(-max_x, max_x)).x


For me, this took 1 millisecond to solve the special case you mentioned originally. You should be able to adapt it to C++ using Boost, which will be even faster.

An alternative answer is available here. It is more work to implement but computationally cheaper. https://math.stackexchange.com/a/4093343/125521