Function for line chart: Linear Interpolation Missing values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HappyJupiter
Forum Newbie
Posts: 6
Joined: Sat Aug 14, 2010 10:36 am

Function for line chart: Linear Interpolation Missing values

Post 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!!
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Function for line chart: Linear Interpolation Missing va

Post 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
HappyJupiter
Forum Newbie
Posts: 6
Joined: Sat Aug 14, 2010 10:36 am

Re: Function for line chart: Linear Interpolation Missing va

Post 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!
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Function for line chart: Linear Interpolation Missing va

Post by shawngoldw »

No problem! :D
glad to help
Post Reply