# What Equation Draws This Graph? (and can you do better?)

This topic is 4882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Having not done much of this since highschool, i'm appealing to you math gurus for this one. Google can only teach me so much. I need a real person. For a math model in my game, i need to produce this graph (or just the upper right quadrant of it anyway): I need an equation for this with a few gotchas. Context: It's meant to represent diminishing returns. This is a general enough statement to get the problem communicated without bothering you with all the details. But let's call the Y axis "Percentage Effectiveness" and the X axis "Distance". That is: the further away you get, the worse the effectiveness is. Assume that the blue dot is the "start" and that it is always y=100 (to represent "100% effective"). The red dot is the "end" at which you have 0% effect. I want the graph to ride along pretty straight for a long while then drop off sharply at the end. This is what i came up with to produce the above graph, just goofing off on a graphing calc online: y = 100 - ( (0.05*x)^4 ) Here is my gotcha: The endpoint on the slope (rightside x-intercept) needs to be a specific number (call it "Z") and the graph needs to adjust accordingly. That is to say, "You achieve 0% Effect at a Distance of Z." I hope i explained that right. What i want to know is: - Is there a simpler formula for this or am i making it too complicated? - How can i keep the same basic graph but "stretch" it horizontally to hit a specific x-intercept? Thanks for your help. I really appreciate it.

##### Share on other sites
I thought that looked like a quartic. Looks like I still remember that stuff!

Anyway, here's my opinion on your problem:
Quote:
 Original post by leiavoia- Is there a simpler formula for this or am i making it too complicated?

Your formula is quite simple as it is, at least to me. You could go with a quadratic or something inversely exponential or logarithmic if you want a different curve, but if the quartic model fits your needs best, then go with it.

Quote:
 - How can i keep the same basic graph but "stretch" it horizontally to hit a specific x-intercept?

It's the coefficient in front of the x term in your equation that controls the "stretchiness" in the direction of the x-axis. So in your example, it's the 0.05. Halve that, and the graph will dilate out by a factor of 2.

##### Share on other sites
y = 100 - ( (0.05*x)^4 )

You want to adjust the 0.05 so that your graph is 0 at a certain X.

Let's call it a and solve.

0 = 100 - ( (a*x) ^ 4)
(a*x) ^ 4 = 100
a*x = 100^(1/4)
a = 3.1622/x

That's all you need, unless I've made some stupid mistake somewhere.

##### Share on other sites
If your function has the value you want at input X, and you want it to have that same value at input Y, simply multiply the X by Y/X:

  y = 100 - ( (0.05*(x*(X/Y))^4 )

More generally, if you want value V at point Q, you can analytically solve this through finding derivative = 0 of the function (F - (V*x/Q)).

When it comes to diminishing returns, my favorite is the atan function. You can just keep going and going and it never QUITE gets to 1.0 :-)

##### Share on other sites
You guys are quick. Thanks!

I guess my original equation was fairly decent then. I've got a good model in mind now with some numbers to go with it. Thanks again!

##### Share on other sites
Quote:
 Original post by leiavoiaContext: It's meant to represent diminishing returns. This is a general enough statement to get the problem communicated without bothering you with all the details. But let's call the Y axis "Percentage Effectiveness" and the X axis "Distance". That is: the further away you get, the worse the effectiveness is.

That's not really a "diminishing returns" graph. Such a graph would be tending to zero, not decreasing at an increasing rate. This one is more like an application of an inverse-squared law (e.g. radiance y at a distance x) or something similar.

##### Share on other sites
You are right, but it fits my model better. I'm actually modeling "planetary habitability" for space-faring races in a strategy game i'm designing. I want races to be able to settle planets in their general "comfort zone," with diminishing returns the further away from their zone that they get. However, there needs to be a point at which a planet is totally inhospitable even to technologically advanced life (that is, y=0).

##### Share on other sites
Quote:
 Original post by leiavoiaYou are right, but it fits my model better. I'm actually modeling "planetary habitability" for space-faring races in a strategy game i'm designing. I want races to be able to settle planets in their general "comfort zone," with diminishing returns the further away from their zone that they get. However, there needs to be a point at which a planet is totally inhospitable even to technologically advanced life (that is, y=0).

The answers you've received so far look great to me, but I thought I'd just throw this one in the pot for flavour [smile] You could try one-sized Gaussian, which is more in keeping with what kSqaured suggested. f(x) = exp(-a*x^2), where a > 0. The shortcoming of this is that there is no value of x where f(x) = 0, if that is a problem. However, if you scaled f(x) properly, you will have a nice probabilistic interpretation for 'normal' planetary conditions.

-Josh

##### Share on other sites
From "Texturing & Modeling" second edition, by Ebert et al. there is an equation that *may* be interesting to you - I use it all the time.

It seems you want the slope at x=0 to be 0, good. For diminishing returns, we would like the slope at x=max to also be 0. The equation that works for this can be found in chapter 2 page 26-27, called a "smoothstep."

y=3x^2 - 2x^3 is zero at x=0 and 1 at x=1.

Invert:
y=1 - (3x^2 - 2x^3), now 1 at x=0 and 0 at x=1.

normalize your range first before putting it into the equation:
a = minx, b = maxx
x = (x - a) / (b - a)

Increase the range by r:
y = r * (1 - (3((x-a)/(b-a))^2 - 2((x-a)/(b-a))^3))

So, there is an alternative. Again, not an accurate model of diminishing returns, but maybe better than a vertical cutoff of the x-axis at x=max.

##### Share on other sites
Then, there is also the cosine function (slopes 0 at minx and maxx).
y=cosx gives 1 at x=0 and -1 at x=pi

using same adjustment calculations as my previous post, we have:

y = cos(pi * x), yields 1 at x=0 and -1 at x=1

y = (cos(pi * x) + 1) / 2, yields 1 at x=0, 0 at x=1

y = r * (cos(pi * x) + 1) / 2

y = r * (cos(pi * ((x - a) / (b - a))) + 1) / 2
This last one is r at x=a and 0 at x=b

These techniques for adjusting domain and range can be applied to most any equation. Good luck.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 33
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015633
×