views:

375

answers:

4

I need an exponential equation with the following parameters:

When x = 0, y = 153.
When x = 500, y = 53.
Y should increase exponentially as X approaches 0 and should decrease exponentially when X approaches 500.

For some reason I can't remember how to do this. I'm sure once I see the equation (or one similar) I can figure out the rest.

Context in Programming: This is for a Javascript function that changes the color of a div when a textarea's max-length is about to be reached. Other alternatives or code snippets are very welcome.

UPDATE: I don't know why but -1500/(x+15)+153 gives me something close to what I am looking for. So It looks like what I was asking for is not what I really wanted.

I guess what I am looking for is:
When x = 0, y = 53.
When x = 500, y = 153.

A: 

I don't know what the curve would be exactly but it would probably be in the family of 1/(x^d) where d is your exponent. Look here for an idea of what the curve looks like. Is that what you want?

Poindexter
+1  A: 

You have to be more specific, but y = 154-x^0.742625 should do the trick. Here, the decimal number is log(101)/log(500).

http://www.wolframalpha.com/input/?i=154-x^%28log%28101%29%2Flog%28599%29%29

Paxinum
+1  A: 

edit (after your update):

With your changes, you're asking for an ascending function and something resembling y = 1/x.

The scale of your function can be changed to fit your exact coordinates, although the curve slopes much more steeply at the start.

y = 154 - 10100 / (20 * x + 100) @ Wolfram Alpha
Plot of 154 - 10100 / (20 * x + 100) from x=0 to x=500 @ Wolfram Alpha

Noting the integer solutions, we make use of the solution x=96, y=149 to alter the formula, scaling these values into your coordinate range. This will give us something closer to your updated curve that slopes a little bit more gently.

y = 158 - 2625 / (x + 25) @ Wolfram Alpha
Plot of 158 - 2625 / (x + 25) from x=0 to x=500 @ Wolfram Alpha

Here's a plot of your version, for comparison.

y = -1500 / (x + 15) + 153 @ Wolfram Alpha


Original Answer (before your update)

I think you will see some strange convergences towards your destination color if you use a non-linear scale, but nonetheless, you can use a general formula and decide what polynomial or exponent gives you the best results.

First, the algebraic / polynomial function.

A * X ^ N + B = Y

This general formula can be solved in system to give you a polynomial of order N that fits a curve between two known points. In this case, we solve for <X = 0, Y = 153> to <X = 500, Y = 53>.

Substituting the first coordinate pair, we easily obtain B.

A * (0) ^ N + B = (153)
0 + B = (153)
B = 153

Now, substituting the second pair, we can find A.

A * (500) ^ N + 153 = (53)
A * (500) ^ N = -100
A = -100 / (500 ^ N)

If you want a linear scale, you substitute N = 1, and that gives us A = -0.20 .

-0.20 * X + 153 = Y

If you want a quadratic scale, you substitute N = 2, and that gives us A = -0.0004 .

-0.0004 * X ^ 2 + 153 = Y

You could also use some non-integer value for N, between 1 and 2 (try 1.5 or 1.6), which I think will probably give you better results. Also note that as this function increases, it will eventually drop below zero, but only after the curve has passed through the second point.

Here is the exponential function. I use e as the base here, although you could alter it to anything else greater than 1. To fit a curve between two points, we will get the best results if both points have Y values over zero. Otherwise, we would have to add an offset and determine where we want the baseline to be. For purposes here, we'll assume the baseline is Y = 0. This means that as X increases, Y will eventually creep toward, but not actually reach, 0, after it has passed through the second point.

A * e ^ (B * X) = Y

Again, solve for the first coordinate.

A * e ^ (B * 0) = 153
A * e ^ (0) = 153
A * 1 = 153
A = 153

Substitute in to get B, with the second coordinate.

153 * e ^ (B * 500) = 53
e ^ (B * 500) = 53 / 153
B * 500 = ln(53 / 153)
B = ln(53 / 153) / 500

ln(val) is the natural log that is inverse to e ^ val. My calculator says that B is about equal to -0.0021202920156806272577911119053782, or perhaps -0.0021 would work best in short. If you want to solve this for other exponent bases, use exponent/logarithm identities in the same fashion to solve for any other base, and to change the base of the logarithm to ln() [log() in js] or log() [log() / Math.log10e in js].

meklarian
perfect! the updated function you gave me works great! Thanks!
David Murdoch
A: 

Basically you probably want to solve the logistic equation dy/dx = K y (ymax - y). The solution is in the provided link.

erikkallen
Actually, after you provided your reason, this is probably overkill. A linear gradient is probably better for your application.
erikkallen