What's the inverse function of $ f(x) = 0.02(x-1)^3 + 130(x-1)^{1.5} + 130(x - 1) $?

175 Views Asked by At

The equation is graphed here: https://www.desmos.com/calculator/kgvnud77dg

I've come up with this equation as part of designing a game. This equation is used to map the user level to their cumulative score. In the game, I only store the user cumulative score. So, I need the inverse function to calculate the level on the fly by simply passing the score.

Example of corresponding Levels and Scores:

Level 1: $ f(1) = 0 $
Level 2: $ f(2) = 260 $
Level 3: $ f(3) = 627 $
Level 4: $ f(4) = 1066 $
Level 5: $ f(5) = 1561 $
Level 10: $ f(10) = 4694 $
Level 50: $ f(50) = 53312 $
Level 100: $f(100) = 160330 $
Level 200: $f(200) = 548423 $

I need to be able to calculate the level using score, like this: $ f^{-1}(1561) = 5 $

2

There are 2 best solutions below

2
On BEST ANSWER

In this particular case, your best bet is not to use the inverse function (whose closed form, if it exists, is probably a horrible mess), but to sample this function at every possible level value that you want (let's hope that you don't want infinite levels) and then when you're given a score, check the index of the score that's immediately lower than it. This works because your function is strictly increasing. In pseudo-code it would look like this :

array = [ f(i) for i = 1 to MAX_LEVEL ]

function getLevelFromScore(score)
    for k = 1 to MAX_LEVEL - 1
        if array[k] > score then return k - 1
    return MAX_LEVEL
1
On

There are $9$ data points $(x_k,y_k)$ $(1\leq k\leq 9)$. If you define $(\xi_k,\eta_k):=(\log x_k,\log y_k)$, i.e., plot the data on double logarithmic paper, then you can see that, apart from $(\xi_1,\eta_1)$ the $(\xi_k,\eta_k)$ are lying close to a line approximatively given by $\eta=4.4+1.65 \xi$. (So called regression analysis can find the optimal values here). Given that, there is a reasonable approximation of the connection between level $x$ and score of $y$ the form $y= c\,x^\alpha$ with $c\approx e^{4.4}$ and $\alpha\approx 1.65$. A relation of this kind can be inverted easily. But note that computationally the solution proposed by @Matrefeytontias might be cheaper and even realize the given data exactly.