Thread: For a piece of code

1. For a piece of code

Hey there everyone. I hope i got the right forum here. I really, quite desperately need this graph -

turned into an equation i can use in my lua programming script. If anyone could do this for me, i'd be eternally greatful.. thank you.

2. Originally Posted by RogueFighter
Hey there everyone. I hope i got the right forum here. I really, quite desperately need this graph -

turned into an equation i can use in my lua programming script. If anyone could do this for me, i'd be eternally greatful.. thank you.
It would help if you gave some background on what this curve represents.

CB

3. Originally Posted by CaptainBlack
It would help if you gave some background on what this curve represents.

CB
It doesnt represent anything really, its just out of my mind. Its a weight to speed ratio graph. The less lines that are in my piece of code, the more efficiently it will run. I just really need something thats one line and tells the code to replicate that, that isnt

if(weight) =>50
then(speed) ( 1 )
elseif(weight) ==49
then(speed) ( 2 )

Something with so many lines of code will run sluggishly.

4. First, there are an infinite number of functions that would give graphs that look like what you have given. However, you can look for the easiest. It clearly is not a straight line so lets try quadratic. Any quadratic will be of the form $\displaystyle y= ax^2+ bx+ c$ so we need three data points to determine those three coefficients. Just looking at your graph it looks like weight is 50 when speed is 0, weight 25 when speed is 25, and weight is 0 when speed is 35.

But apparently you want speed as a function of weight (your graph has "dependent" and "independent" axes swapped from the usual convention) so you want (50, 0), (25, 25), and (0, 35).

Putting those values into the equation,
$\displaystyle a(0)^2+ b(0)+ c= c= 35$
$\displaystyle a(25)^2+ b(25)+ c= 625a+ 25b+ 35= 25$
and
$\displaystyle a(50)^2+ b(50)+ c= 2500a+ 50b+ 35= 0$

Multiplying the second equation by 2 gives 1250a+ 50b= -20 while the third equation is 2500a+ 50b= -35. Subtracting eliminates b: 1250a= -15 so that $\displaystyle a= -\frac{3}{250}$.

Putting that back into 1250a+ 50b= -20 (or just 125a+ 5b= -2) we have $\displaystyle -\frac{3}{2}+ 5b= -2$ so $\displaystyle 5b= -\frac{4}{2}+ \frac{3}{2}= -\frac{1}{2}$ and $\displaystyle b= -\frac{1}{10}$.

So one possible function is $\displaystyle y= -\frac{3}{250}x^2- \frac{1}{10}x+ 35$. That will exactly fit the points I chose:
$\displaystyle -\frac{3}{250}(0)^2- \frac{1}{10}(0)+ 35= 35$
$\displaystyle -\frac{3}{250}(25)^2- \frac{1}{10}(25)+ 35= 25$
$\displaystyle -\frac{3}{250}(50)^2- \frac{1}{10}(50)+ 35= 0$
but probably will not go exactly through other points.

For example, from your graph it appears that a weight of 15 corresponds to a speed of 30. Setting x= 15, $\displaystyle -\frac{3}{250}(15)^2- \frac{1}{10}(15)+ 35= 30.8$ instead- close but not exact. If you wanted that point to be exact also, you could go up to third power giving you another coefficient and another point to use. But then there would be other points at which the function would not be exact.
I suspect that $\displaystyle y= -\frac{3}{250}x^2- \frac{1}{10}x+ 35$, where "x" is the weight and "y" is the speed, is the best simple function you will find to fit this graph.