0

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}$$

Robin
  • 6,201
Phedg1
  • 147
  • Calculus? ${}{}$ – Vishu Apr 03 '21 at 10:47
  • Do you need to find minimum analytically or numerical methods are accepted? – Yalikesi Apr 03 '21 at 10:47
  • Have you tried $x=5\sin t$? – J.G. Apr 03 '21 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 03 '21 at 10:53
  • @J.G. What is t? – Phedg1 Apr 03 '21 at 10:58
  • @Phedg1 I was suggesting a substitution that defines a new variable, $t=\arcsin(x/5)$. – J.G. Apr 03 '21 at 10:59
  • @Tavish What procedure in calculus? – Phedg1 Apr 03 '21 at 10:59
  • When you say you need the game to output the minimum value of $y$, will $y$ always have exactly this formula? If so, you can just cache the minimum as a constant. Or would the minimisation problem vary in its parameters or coefficients? If so, please edit your question to reflect your needs. Also, we may be able to suggest efficient function-minimizing library calls if you tell us your progamming language. – J.G. Apr 03 '21 at 11:00
  • @J.G. The minimisation problem will vary. If someone can teach or show me how to solve for the minimum using this example I will be able to reverse engineer my formula. I didn't include the full thing because I didn't want to make people have to do extra work that I could do. But I'll edit the question now to include the full thing. – Phedg1 Apr 03 '21 at 11:06
  • The function has a minima https://www.wolframalpha.com/input/?i=local+minimum+calculator&assumption=%7B%22F%22%2C+%22LocalMinimizeCalculator%22%2C+%22curvefunction%22%7D+-%3E%22-3x%5E2-10x-225-40sqrt%2825-x%5E2%29%22. – Ishraaq Parvez Apr 03 '21 at 11:14
  • The symmetry of the formula makes me wonder, do you want to find a point on a conic section with the minimum distance to a fixed point? – Ishraaq Parvez Apr 03 '21 at 11:15
  • @IshraaqParvez I am trying to find the point on an ellipse with the minimum distance to a fixed point. The formulas for this were given here: https://math.stackexchange.com/questions/185600/finding-a-point-on-a-ellipse-so-that-it-has-the-shortest-distance-between-this But of all the answers I cannot figure out how to actually calculate that point from any of them. – Phedg1 Apr 03 '21 at 11:19
  • Use parametric coordinates. But I might be able to help a bit more - the point you are after is always located on the normal of the ellipse passing through the fixed point. Find such a normal, and determine all the points common to it. – Ishraaq Parvez Apr 03 '21 at 11:21
  • The project I am working on is written in python. – Phedg1 Apr 03 '21 at 11:33
  • @Phedg1 Differentiate and set equal to zero. – Vishu Apr 03 '21 at 11:34
  • How long can you afford the algorithm to spend on this problem? I've posted a 1 ms solution, but I don't know whether that's fast enough for you. – J.G. Apr 03 '21 at 11:36
  • @J.G. This function will potentially need to be done many times per frame (~60fps). Which is why I'm hoping to avoid iterating or successive approximation to find the answer. – Phedg1 Apr 03 '21 at 11:38
  • Apologies everyone, I realized I forgot to include a sqrt. – Phedg1 Apr 03 '21 at 12:21
  • Your newest square root won't change the location of the minimum. – J.G. Apr 03 '21 at 12:24
  • Thanks for accepting my answer. If it isn't fast enough for you, you can parallelize by passing in array-valued parameters, experimenting with optimal arguments in minimize_scalar, or handing the task to another language. – J.G. Apr 03 '21 at 12:45

2 Answers2

2

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(w2-x2)-b2)**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.

J.G.
  • 118,053
0

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

Phedg1
  • 147