Page 1 of 1

Function for line chart: Linear Interpolation Missing values

Posted: Sat Aug 14, 2010 10:39 am
by HappyJupiter
I am building a line graph from an array that is built from a db of user entered data.

What I need help with is adding any missing values. (Linear Interpolation)
Figuring out the value if there is only 1 value missing is simple (start value+end value/2), but if there is more than 1 value missing in a row it becomes more difficult.

1 missing value example:
$sample=array(1.3,2.1,,4.7);
how to figure it out: the missing value is this case is (2.1+4.7)/2 = 3.4

2 missing value example:
$sample=array(2.4,3.1,,,6.9);
how to figure it out: ??

I need a php function that accepts 3 vars and returns the calculated values for each of the missing values. (I guess it makes sense to return the calculated values in an array)

function buildMissingValues($startValue,$endValue,$numberOfMissingValues){
//brilliant code here
}

In the above "2 missing value" example I would like to:

$newValues=buildMissingValues(3.1,6.9,2);

and have an array returned with 2 values that complete the line chart points.

I'm not sure if I have explained this well enough.

I REALLY appreciate your help!!

Re: Function for line chart: Linear Interpolation Missing va

Posted: Sat Aug 14, 2010 12:37 pm
by shawngoldw
This sounds like more of a math problem. I didn't write up the function but this logic should work:

stepSize = (end - start) / (numMissing + 1)

Then you can loop:
previous = start
for 0 to numMissing
ret[] = previous + stepSize

Lemme know how this works out.


Shawn

Re: Function for line chart: Linear Interpolation Missing va

Posted: Sun Aug 15, 2010 10:20 pm
by HappyJupiter
Thanks Shawn - you gave me a few great tidbits that help me get over the hump.

Got it all figured out and implemented and it works fabulously :D

Thanks again!

Re: Function for line chart: Linear Interpolation Missing va

Posted: Mon Aug 16, 2010 10:56 am
by shawngoldw
No problem! :D
glad to help