4
$\begingroup$

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 $

$\endgroup$
4
  • 1
    $\begingroup$ This is a quadratic equation in disguise. $\endgroup$
    – Szeto
    Aug 20, 2018 at 12:37
  • 1
    $\begingroup$ Actually the $(x-1)$ in the last term spoils the possibility of solving this as a simple quadratic. If there's a closed form for this (which I am not sure there is), I bet it's very messy. Why do you need an inverse? Perhaps some information on the bigger problem you were trying to solve might help. $\endgroup$
    – David K
    Aug 20, 2018 at 13:05
  • $\begingroup$ I have added more details to the question. Basically, I'm using this to design a game. I don't mind changing the equation to something simpler and easier to solve, my only concern is to get similar difficulty progression. $\endgroup$ Aug 20, 2018 at 14:08
  • 5
    $\begingroup$ This is not a textbook problem, but a real problem of a kind of "outsider" who hopes that people with more analytical expertise than he can muster will point him into the right direction. I don't know why MSE should not be a place for such questions. $\endgroup$ Aug 20, 2018 at 15:59

2 Answers 2

3
$\begingroup$

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
$\endgroup$
2
  • $\begingroup$ Your approach would definitely do the trick, I was hoping to avoid that though. If only there was a straightforward inverse function! ASIDE: Another approach is to store the level, which I was hoping to avoid in the first place. Then, I can do operations on the level directly. For example, if I wanted to know the score remaining to level up, then $f(level + 1) − score$" $\endgroup$ Aug 20, 2018 at 19:41
  • 1
    $\begingroup$ When you think about it, computing the inverse function for a given argument would probably involve cubic roots, logarithms and whatnot, so in fact doing this approach (or yours) is probably computationally faster anyway. $\endgroup$ Aug 21, 2018 at 0:17
2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thanks for the help! It's much appreciated. I decided to use a programmatic solution instead, as mentioned by @Matrefeytontias. $\endgroup$ Aug 20, 2018 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .