How to efficiently find the max of this function?

96 Views Asked by At

I am a software developer and my software uses a function that I believe is very inefficient.

I need to find the value of x that results in the maximum value out of the function. Currently, I will iterate over different x values thousands of times and compare the results of the function until I find the highest value, which is quite inefficient.

I also have a constraint: I can only use integers. If an operation results in a decimal number (like a division), all the decimals will be truncated. I do not need an exact result for this calculation, but it should be reasonably precise.

In the function below, c1 and b1 to b6 are given constants. I need to be able to change them in the formula between cycles, but I already know their value before I need to find the max. For instance, I would know that for a given cycle $c_{1}=999$, $b_{1}=123401014$, $b_{2}=231248124$, etc.

All the constants and x are always positive integers greater than 0.

Here is the function f(x) for which I need to find the value of x corresponding to max(f(x)):

$$f(x)=x\times f_1(x)f_2(x)f_3(x)-x$$

where: \begin{align} f_1(x)&=\frac{b_1c_1}{10000b_2+c_1x}\\ f_2(x)&=\frac{b_3c_1}{10000b_4+c_1xf_1(x)}\\ f_3(x)&=\frac{b_5c_1}{10000b_6+c_1xf_1(x)f_2(x)} \end{align}

I posted an almost identical question here and got an excellent answer, but I found out after many tests that my original function had mistakes in it.

Thanks in advance!

Here is a plot of the function with typical constants values.

Edit: Bonus question

My software also uses this slightly modified function (note the addition of $c_2$ constant):

$$f(x)=x\times f_1(x)f_2(x)-x$$

where: \begin{align} f_1(x)&=\frac{b_1c_1}{10000b_2+c_1x}\\ f_2(x)&=\frac{b_3c_2}{10000b_4+c_2xf_1(x)} \end{align}

What would be this function simplified? And what would be the formula to find the stationary points?

Here is a plot.

1

There are 1 best solutions below

3
On BEST ANSWER

Hint.

After a lot of algebra we get

$$ f(x) = \left(\frac{\alpha_1 +\alpha_2 x}{\alpha_3+\alpha_4 x}\right)x $$

now by calculating the stationary points with $f'(x)$ we are done.

NOTE

With $a = 10^4$ $$ \cases{ \alpha_1 = b_1 b_3 b_5 c_1^3-a^3b_2 b_4 b_6\\ \alpha_2 = -(a^2b_4b_6c_1+a b_1 b_6 c_1^2+b_1b_3c_1^3)\\ \alpha_3 = a^3b_2b_4b_6\\ \alpha_4 = c_1(a^2b_4b_6+ab_1b_6c_1+b_1b_3c_1^2) } $$

now from $f'(x)=0$ the stationary points are located at

$$ x^* = \frac{-\alpha_2\alpha_3\pm\sqrt{\alpha_2\alpha_3(\alpha_2\alpha_3-\alpha_1\alpha_4)}}{\alpha_2\alpha_4} $$

for the second function we have after simplifications

$$ f(x) = \left(\frac{\alpha_1}{\alpha_2 +\alpha_3 x}-1\right)x $$

with

$$ \cases{ \alpha_1 = b_1b_3c_1c_2\\ \alpha_2 = a^2b_2b_4\\ \alpha_3 = c_1(a b_4+b_1c_2) } $$

and the stationary points are defined at $f'(x^*) = 0$ as

$$ x^* = -\frac{\alpha_2\pm\sqrt{\alpha_1\alpha_2}}{\alpha_3} $$