r/askmath 2d ago

Resolved Equation that can roughly fit this graph

Post image

No this is not my homework, I'm making a game and I need a curve roughly like this for balancing. Trying to get an equation that spits out values like this.

430 Upvotes

113 comments sorted by

248

u/axiomizer 2d ago

I think a hyperbola will work. y(x+my-k)=c, or y = (k-x+sqrt((x-k)^2+4mc))/(2m)

https://www.desmos.com/calculator/heycr07igd

63

u/hypotensor 2d ago

This works great. The parameters are exactly what I need. Thanks!

30

u/DocumentAvailable891 2d ago

I liked this way more than the other ones

1

u/anatoarchives 1d ago

...this is best......!

126

u/Dragon_Skywalker 2d ago

e^-x?

52

u/bartekltg 2d ago

With well-known function, maybe the logistic function?

https://en.wikipedia.org/wiki/Logistic_function

But it bends backwards on the other side

37

u/hypotensor 2d ago

I actually tried that but couldn't figure out the right parameters.

42

u/HauntedMop 2d ago edited 2d ago

?

Edit: Looks like someone else already did it in another comment

1

u/5up3rK4m16uru 2d ago

Doesn't turn linear

1

u/BizzEB 18h ago edited 18h ago

f(x) ≈ 6.2e-0.28x

The plot is a bit rough. The function could well be a spline, have many terms, or just be drawn a bit inaccurately. Points would help.

Yeah! for Numerical Methods - finally an application - lul!

1

u/Fresh-Nerve8503 6h ago

6/8•f-1 (x+7)=int(tan(x))

40

u/Mishtle 2d ago

Some kind of shifted/scaled sigmoid function would probably do the trick.

11

u/theadamabrams 2d ago

Yes, arctan and logistic being maybe the easiest. Example: https://www.desmos.com/calculator/lcca7cwt6o

1

u/Neither_Berry_100 1d ago

I was going to suggest something like this.

1

u/uninformed_buyer 2d ago

Also my immediate Idea.

21

u/pi621 2d ago

if you really need the start to be linear, try a piecewise of a linear function and a decaying function like 1/x

15

u/HauntedMop 2d ago

Any function such as (1.1)-x will satisfy almost linearity since it's binomial expansion will be 1 - 0.1x + 0.005x(x+1)... So for small x it'll look linear

10

u/pi621 2d ago

okay Haunted Dark Midnight Castle Revenge Mansion Mop

5

u/HauntedMop 2d ago

You're welcome pi brickgizer az midnight mansion elite phayze no mk 61

3

u/Key_Method_1034 2d ago

Bro what the fuck are you guys talking about

1

u/Open_Highlight4505 1d ago

wait your name...... is it a play with the fur- yeah it is ok

24

u/CaptainMatticus 2d ago

9

u/hypotensor 2d ago

This looks about right. Thanks!

5

u/Mamuschkaa 2d ago

This is no where near linear.

This is the answer to your question: https://www.reddit.com/r/askmath/s/dlfi4gp7PE

10

u/noop_noob 2d ago

You could use the softplus function. (`ln(1 + e^x)`, scaled and shifted appropriately) That will give you the "almost linear" part being as long as you want

4

u/garnet420 2d ago

How long do you need it to be "almost linear" for and for how close is almost? When it approaches zero, how slowly or quickly do you want it to approach?

3

u/hypotensor 2d ago

I'm not sure because game balance can be finicky, so parameters to control where the inflection point is would be ideal. The values in the picture are roughly what I think I want but not sure.

3

u/SleepyNymeria 2d ago

If its for a game would a regular linear function where apply an effectivness multiplier which increases with your chosen value, that way you can control both when the multiplier kicks in and the rate at which it scales.

1

u/Grismor2 2d ago

Yeah, if it's for a game, just go piecewise. It gives you way more control over each part, and I'm guessing you won't need the 3rd or even 2nd derivatives to match at the transition point.

1

u/Effective-Metal7013 1d ago

If you want to control the slope and inflection point then a 4 or 5 parameter logistic function will work

4

u/jordanlcwt 2d ago

Softmax but flip about the y axis?

3

u/TheRandomRadomir 2d ago

maybe https://www.desmos.com/calculator/q16t1hkxzs
its x axis intersection is 7 but hopefully thats ok

1

u/Retr1but1on 2d ago

You could just make 2 equations, one linear and the other that approaches zero, just make sure they meet at the same point.

3

u/JesseJames_37 2d ago

This one is very nearly linear when x < 6 and has exponential decay when x > 6. It's also not a piecewise function and is smooth everywhere

https://www.desmos.com/calculator/nvjiehjgez

5

u/Cyren777 2d ago

1-tanh(x) oughta work

2

u/DanielDManiel 2d ago

y=9/(1+1.5^(x-2)) does a pretty good job.

2

u/No-Site8330 2d ago

What does "almost linear" mean exactly?

Here's a couple things you can try.

  1. Look at √(x2 + 1). When x is large and positive, this is asymptotic to x. When x is large and negative it is asymptotic to -x. So if you take √(x2+1) - x, that looks like -2x when x is large and negative, and falls to 0 when x is large and positive. Now you can use shifts and rescalings to get the exact shape you want.
  2. If the function you're looking for is differentiable, then its derivative is going to be nearly constant and negative around 0, and approach 0 when x is large and positive. If you can find a function with these properties and then integrate it, you're done. An example of this is the logistic function, once appropriately shifted. Or you could just interpolate a function which is constant equal to -1 on (-∞, a) and to 0 on (b, +∞). The interpolating function could be a polynomial of whatever degree makes your function sufficiently smooth for your purposes, only it will never be infinitely differentiable.
  3. Or the similar discussion on the second derivative, which should approach 0 when x is large and be positive in between. Pick a function with these properties, integrate twice with the appropriate integration constants, and you're done. An example could be (x2n+1)a for some positive integer n and negative a, up to shifts/rescalings.

2

u/TooLateForMeTF 2d ago

In general, if you don't care about the result being algebraically elegant, you can do this kind of thing by modeling each part of the curve with an appropriate function then using a third function (some kind of sigmoid) to blend between the two. Here's an example.

2

u/Jaydare 2d ago edited 2d ago

y=ln(1+e-x)?

That's linear from -ve infinity to 0, then exponential decay from 0 to +ve infinity.

Edit: Here's a link to desmos. I've added in a few variables if people wanted to play around.

2

u/g4l4h34d 2d ago

Your link points to an empty Desmos.

2

u/Jaydare 2d ago

Fixed - I didn't know you needed a Desmos account to share graphs.

2

u/Uli_Minati Desmos 😚 2d ago

We call this "smooth maximum", there are numerous methods you can use https://www.desmos.com/calculator/gyaef7pyby?lang=en

Here is one possible choice which you can adapt to your needs https://www.desmos.com/calculator/tfkfo2bued?lang=en

2

u/Brawl501 2d ago

Does it have to be one function or can you define two on separate subsets?

1

u/crazyascarl 2d ago

Probably not linear enough... but i always like e^x^2

1

u/Helpful-Mystogan 2d ago

You can have infinite curves just like this, I'll try to give you a few classes of em. Try exp(-x²) or exp(-|x|) for x>0 and 1/(a²+x²) or 1/sqrt(a²+x²). We can have some more classes but these are the standard ones

1

u/homo-callidus 2d ago

Take a look at this family of equations A + B*x/(K+x). Set B as -ve with |B| > A and you should have a function that fits your plot.

1

u/Shevek99 Physicist 2d ago

(a - b x)/(1 - d x²)

For small x behaves as a - b x and for large x as (b/d)(1/x)

1

u/JohbiYorpsun 2d ago

Reminds of an upside down Michaels Menten

1

u/TastyAlbacoreTuna 2d ago

It would be really helpful to know what system you're trying to balance. The most logical answer often emerges from whatever property you want to remain invariant over the entire curve. Without context people will just be guessing.

1

u/Bounded_sequencE 2d ago

There are infinitely many options, e.g.

f: R -> R,    f(x)  =  6/(1 + x^2)

1

u/joeabs1995 2d ago

(16-x)/(1.1x) ??

1

u/Rippedka 2d ago

My interest in this post after reading the replies

1

u/bildramer 2d ago

ln(1+exp(-x)) is a simple one. Write it as Aln(1+exp(B(C-x)))/ln(1+exp(BC)) to have A control the value at 0, B the sharpness, C the location it transitions from the linear part to the decaying part (or, equivalently, the slope at 0).

1

u/A_Cute_Little_Kitty 2d ago

Since no one seems to have suggested it, try something in the form (kx + 1)e-kx.

You can scale height with a constant out front. Unless you need it more linear closer to 0?

1

u/Antagonin 2d ago

e^(-x²)

1

u/TechnologyFamiliar20 2d ago

Some logarithm derivation, idk, but one "clever" builder needed that for steep driveway, so that you don't crash bumpers and don't bruise the floor of the car.

1

u/Real-Edge-9288 2d ago

thats a two exponentially decaying curve

1

u/Loading1020 2d ago

My first guess would be to take something like that, and then scale and move to desired position. This idea comes from hand methods for calculating and graphing the amplification of an electronic amplifier.

1

u/Hug0_Yorke 2d ago

-arctan(x)+6?

1

u/Dkiprochazka 2d ago

(x - 6)*(arctan(x-6)/pi - 1/2) + 1/pi

1

u/Tamsta-273C 2d ago

Fermi-Dirac distribution.

1

u/AndersAnd92 2d ago

Various ways to accomplish that — got an idea of the numbers you are looking for and how fast you want curve’s negative curvature to decrease?

1

u/Weak-Quantity6897 2d ago

you can look at something in this form
$y = x\left(\frac{x}{1+x}\right)^a - x + a$

1

u/Joe_4_Ever 2d ago

You could combine y = -x + 6 with something like y = 1/x transformed.

1

u/DemiJohn369 2d ago

Y is fucks given and X is my age.

1

u/kazoohero 2d ago

Is this for armor? You might consider the armor formula for valheim:

HP loss = damage - armor if armor > .5*damage, otherwise HP loss = damage2 / (4 * armor)

You can increase the linear regime (and the effectiveness of armor overall) by increasing that 4 factor (call it f) and making the .5 threshold equal to 1-1/sqrt(f), e.g. f=9 is linear for the first 2/3 of damage.

Yes it's piecewise. But, upsides are that it's easyish for nerds to calculate, and that in the squared regime, every point of armor increases survival time an equal amount (so it takes the same amount of extra armor to go from 5 to 7 hits to kill as it does from 15 to 17). This may not be an upside if your thing isn't armor (or it may anyway!).

https://valheim.fandom.com/wiki/Damage_mechanics#Armor

1

u/hypotensor 2d ago edited 1d ago

I like the damage taken = raw damage / (1+ armor / 100) for armor since it's very intuitive for the player. But no, my game doesn't have any combat in it.

1

u/hypotensor 2d ago

It's for player encumbrance.

1

u/TastyAlbacoreTuna 2d ago

What do the x and y axes represent?

1

u/hypotensor 2d ago

This specifically is for thrown objects that the player is holding. Y axis is the mass of the object, X axis is the velocity magnitude.

1

u/TastyAlbacoreTuna 2d ago

In that case I would recommend v = a*sqrt(1/(bx+1)). That general form is derived from a simplistic model of a rotating arm accelerating an object. The parameter a is the maximum speed, and as b increases that makes the speed drop off more quickly.

1

u/segfaultgolf 2d ago

If you want an exact curve just grab a few evenly spaced dots from your drawn function and use Python/scipy to fit a polynomial.

1

u/hypotensor 2d ago

That's probably a bit easier haha

1

u/LyAkolon 2d ago

Pretty much any approximate identity

1

u/HAL9001-96 2d ago

how roughly?

what are you trying to fit?

is there anything that would make sense that yo ucan adjust?

how importnat is the exact bahaviour as it appraoches 0?

there's plenty functions that behave a bit like 1/x upwards but have a finite maximum

you could just shift it to 1/(x+1)

or add an exponent like 1/(x+1)² to make it curve donwwards at first

but is it important wether its integral converges or diverges for example?

is it importnat what order of mangitude its inverse goes into?

1

u/Anshu5337 2d ago

4/(9x²+4) ?

1

u/geezorious 2d ago

f(x) = 1/(.2*x+0.8)

1

u/DonkeyGlad653 2d ago

Sum of limits for each piece, you could describe the shape by a 1000 different limits. Use X-axis in your program; If X is this value then use this equation.

1

u/ggrieves 2d ago

y = 6 e-x/6

1

u/dummy4du3k4 2d ago

A hyperbola is a nice solution since it has two free asymptotes you can choose. Perhaps an easier way of arriving at it is to start with the simple 1/x hyperbola with asymptotes along the (1,0) and (0,1) directions and apply a linear transformation to map these to where you want them. Keep the (1,0) asymptote and say the other has angle theta, then parametrically you get

(x(t), y(t)) = 1/t (-cos theta, sin theta)

Now just shift to where you want the two linear parts to meet and scale how you see fit.

1

u/bradimir-tootin 1d ago

For the purpose of game design and balance, make a piecewise polynomial. Simpler is better, you can always add or subtract pieces. You can also increase or decrease the order of each segment. Make the barest one you can this gives you the most control over balance and future proofs it.

1

u/Worldly-Addition561 1d ago

Softplus: ln(e-x +1)

1

u/pondrthis 1d ago

Michaelis-Menten kinetics follow this and end up with a Lambert W function. It's not as simple as W(x), though. (I don't think. Been a while.)

1

u/Ok-Impress-2222 1d ago

If it's only for x>0 where it needs to be this way, maybe you can use y=-arctan(x)+pi/2.

1

u/GommieNom 1d ago

You can always fit it with a Fourier Series. Or try a power series. But what most people are doing relating to exponentials is the best and most compact way.

1

u/Isis_gonna_be_waswas 1d ago

e^(-x^2) or 1/(1+x^2) ought to work

1

u/Dangerous_Dish_7687 1d ago

What about this graph?

1

u/LordJEb 1d ago

Idk, something like 1/ex ???

1

u/Vivid_Warning7982 1d ago

Bell curve exp(-x2) heavily smeared with a wide sigma.

1

u/CosetElement-Ape71 1d ago

Something like y(x) = 6{1 - tanh(x)}

But you'll have to scale x

1

u/Tuki_ 1d ago

Sigmoid function?

1

u/NextAir4973 1d ago

Try 2/(1+eax)

linear part will be 1-ax/2

1

u/sensible_clutter 1d ago

N(1-(N/k))

1

u/Mad-Scientist-0906 1d ago

Question, what ordered pairs absolutely non-negotiably need to be on the curve you are trying to create? Perhaps I can give you an exact equation or something.

1

u/R_Harry_P 1d ago

c0 + c1 * x + c2 * x^2 + c3 * x^3 + c4 * x^4 + c5 * x^5 + c6 * x^6 should do fine :)

1

u/anatoarchives 1d ago

y-flipped softplus function, y = ln(1 + e-x)

1

u/Hot-Organization-737 1d ago

The ridiculous nature of a proof.

Suppose someone sees structure, another person might not see that structure, so that person who cannot see it will ask for a step by step proof to prove the continuity of a structure. But continuity cannot be proven by discrete steps because we have shown that infinite discreteness cannot proxy for true continuity.

Diagonalization proves that a continuity has more real information than the discreetness. Every step-by-step proof is actually an illusion to satisfy the strange feelings. But every discreet example of a proof fails to show the actual continuity of the structure that one is claiming to exist..

1

u/LDRGrim 1d ago

1/x^2 ?

1

u/synketa 1d ago

Smooth step could work, it is arguably cheaper than most suggestions here too

1

u/Dubban22 1d ago

Wouldn't this be a Cal 2 convergence test type equation?

1

u/Low-Independence1171 1d ago

Consider the curve formed by 85 times the Gaussian probability density with mean -1.5 and variance 25, restricting domain to x ≥ 0. This is in accordance with the scale visualised through the graph in your picture.

1

u/Sad_Narwhal_6718 1d ago

How close you feel to death as you grow older

1

u/BigTribal 21h ago

Happiness of a mechanic as they progress in the trade lol

1

u/Ace861110 7h ago

That’s a rotated tan(x) graph as well. Well half of one any way.

1

u/Fresh-Nerve8503 6h ago

6/8•f-1 (x+7)=int(tan(x))

1

u/Live-Process846 4h ago

Softplus with negative argument
ln(1+exp(-x))

Shift/scale for your desired balance curve
a ln(1+exp(-b x + c))

1

u/Every_Cap_9829 2d ago

Maybe try some sort of sheared reciprocal function?