# Thread: Perl to count current value based on next value

1. Gee! These Aren't Roasted!
Join Date
Dec 2012
Beans
171

## Re: Perl to count current value based on next value

Originally Posted by Vaphell
assuming linear interpolation
I think that the point be exactly this one: is the interpolation linear or not?
I guess not, because otherwise the algorithm would be very simple; but if the interpolation isn't linear, the unknown values would be unpredictable without more informations about their sequence.

2. Tall Cafè Ubuntu
Join Date
Feb 2009
Beans
1,458

## Re: Perl to count current value based on next value

The formula OP gave is just linear interpolation when you know y[n-1] and y[n+1] and have to find y[n]. (Actually in that particular case it simplifies greatly, but you need the general formula to interpolate with a gap of 2 or more.)

3. 5 Cups of Ubuntu
Join Date
Feb 2011
Beans
35

## Re: Perl to count current value based on next value

Originally Posted by Tony Flury
This is almost exactly the same problem as this thread :

and this one :

The first one is marked as solved.

One of the arts of programming is re-use - so adapt the solution previously provided.
They are not the same...

4. 5 Cups of Ubuntu
Join Date
Feb 2011
Beans
35

## Re: Perl to count current value based on next value

Originally Posted by iMac71
I think that the point be exactly this one: is the interpolation linear or not?
I guess not, because otherwise the algorithm would be very simple; but if the interpolation isn't linear, the unknown values would be unpredictable without more informations about their sequence.
It is linear which based on two points.

5. 5 Cups of Ubuntu
Join Date
Feb 2011
Beans
35

## Re: Perl to count current value based on next value

Originally Posted by trent.josephsen
The formula OP gave is just linear interpolation when you know y[n-1] and y[n+1] and have to find y[n]. (Actually in that particular case it simplifies greatly, but you need the general formula to interpolate with a gap of 2 or more.)
Correct

6. Gee! These Aren't Roasted!
Join Date
Dec 2012
Beans
171

## Re: Perl to count current value based on next value

the following script might be what you're looking for:
PHP Code:
``` my \$loop = 1;while (<>){    my \$delta_x, \$x_i, \$y_i, \$x_k, \$y_k;    if (\$loop == 1) {        (\$x_i, \$y_i, \$x_k, \$y_k) = split    }    else {        (\$x_i, \$y_i) = (\$x_k, \$y_k);        (\$x_k, \$y_k) = split    }    \$delta_x = (\$x_k - \$x_i) / 2;    my \$x_j = \$x_i + \$delta_x;    my \$y_j = ((\$x_j - \$x_k) / (\$x_i - \$x_k)) * \$y_i + ((\$x_j - \$x_i) / (\$x_k - \$x_i)) * \$y_k;    print \$x_i,"\t",\$y_i,"\n(",\$x_j,")\t(",\$y_j,")\n";    \$loop++;}  ```
when you run the script, the data should be entered as follows:
- first input line: two couples of coordinates (i.e. two points);
- next input lines: a couple of coordinates (i.e. one point).
Last edited by iMac71; January 18th, 2013 at 10:10 PM. Reason: Insertion of \$delta_x as the difference between two non consecutive values of x