Please Help Me With This Math!

BlitzMax Forums/BlitzMax Programming/Please Help Me With This Math!

Rico(Posted 2013) [#1]
I have a relationship between x and y that goes something like this

x=0                                       y=11
x=1                                       y=16
x=2                                       y=22
x=3                                       y=29
x=4                                       y=37
x=5                                       y=46

etc


You can see the difference between each y value is 5,6,7,8,9 respectively (one more each time)

How can i represent this in a formula/equation so given a value of x it will find the correct value of y. Thank you :)


Jesse(Posted 2013) [#2]
n = 0
y = 6
for i = 0 until maxvalue
x :+ n
y :+ 5+n
n :+ 1
next


TomToad(Posted 2013) [#3]
Edit: nm doesn't work


TomToad(Posted 2013) [#4]
y=((x-1)*x)/2+x*5+11

That works.


Rico(Posted 2013) [#5]
Awesome, thanks TomToad. I spent a while trying to come up with a formula myself but couldn't figure it out!

Thanks too Jesse but I was looking more for a direct relationship between x & y - without the loop so it would be fast.


Floyd(Posted 2013) [#6]
One of the standard techniques for this is to check if there is a polynomial which represents the series of numbers. This is easily done by repeatedly taking the difference of consecutive elements: second-first, third-second etc.

For this example we get:

11 16 22 29 37 46 ...
  5  6  7  8  9
   1  1  1  1 

If we ever reach a row with all elements the same then there is a polynomial relationship. The number of steps required to get a row with all elements the same is the degree of the polynomial.
Here we needed two steps to get 1 1 1 ... That means the polynomial has degree 2, i.e. highest order term is x^2:

y = A*x^2 + B*x + C

where A,B,C are some constants which we must determine. Sometimes this can be done easily by guessing, or using various tricks.
But in any case we can grind out the answer by filling in known values for x and y. We know y=11 when x=0, y=16 when x=1 etc.

Thus we must solve

11 = C
16 = A + B + C
22 = A*4 + B*2 + C

This is solved the same way we generated those rows of numbers, subtract each equation from the one after it. Do this repeatedly to get simpler equations. In the end we have

11 = C
9/2 = B
1/2 = A

And the result is the quadratic equation

y = x^2 * 1/2 + x * 9/2 + 11

You might prefer to rewrite this as

y = x*(x+9)/2 + 11


Kryzon(Posted 2013) [#7]
That is very interesting Floyd, thanks for sharing.


Rico(Posted 2013) [#8]
Thanks too Floyd, was wondering how you go about doing that.