LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Script to solve second order (polynomial) interpolation (http://www.linuxquestions.org/questions/programming-9/script-to-solve-second-order-polynomial-interpolation-4175446534/)

eminempark 01-21-2013 12:55 AM

Script to solve second order (polynomial) interpolation
 
Currently I have awk command to do linear interpolation
Code:

awk '
{
  P[$1]=$2
  I[i++]=$1
}
END{
  j=0; s=I[j]; t=I[j+1]
  for(i=m;i<=n;i++){
    if(I[j+2] && i>t){
      j++; s=I[j]; t=I[j+1]
    }                       
    print i,P[s]+(i-s)*(P[t]-P[s])/(t-s)
  }                                   
}
' m=1 n=8 infile

INPUT
Code:

#X Y
1 22.3125
4 22.5
8 22.1875

OUTPUT
Code:

1 22.3125
2 22.2917
3 22.2708
4 22.5
5 22.2344
6 22.2188
7 22.2031
8 22.1875

Any idea how to change the code to polynomial interpolation?
EXPECTED OUTPUT
Code:

1 22.3125
2 33.4583
3 33.4375
4 22.25
5 22.2321
6 22.2158
7 22.2009
8 22.1875

The Y of X0=0. Please help me to solve this... Thank you very much...

millgates 01-21-2013 03:48 AM

Quote:

Originally Posted by eminempark (Post 4874467)
Currently I have awk command to do linear interpolation
INPUT
Code:

#X Y
1 22.3125
4 22.5
8 22.1875

OUTPUT
Code:

1 22.3125
2 22.2917
3 22.2708
4 22.5
5 22.2344
6 22.2188
7 22.2031
8 22.1875


That doesn't seem like the correct result. It does not even look like the output of the program you posted.


Quote:

Originally Posted by eminempark (Post 4874467)
EXPECTED OUTPUT
Code:

1 22.3125
2 33.4583
3 33.4375
4 22.25
5 22.2321
6 22.2158
7 22.2009
8 22.1875


How did you calculate the desired output?

eminempark 01-21-2013 04:02 AM

Quote:

Originally Posted by millgates (Post 4874540)
That doesn't seem like the correct result. It does not even look like the output of the program you posted.




How did you calculate the desired output?

Thank you for the respond. It should be
Code:

1 22.3125
2 22.2917
3 22.2708
4 22.25
5 22.2344
6 22.2188
7 22.2031
8 22.1875


grail 01-21-2013 07:51 AM

You did not answer the question about HOW you arrived at those figures?

eminempark 01-21-2013 08:16 PM

Quote:

Originally Posted by grail (Post 4874674)
You did not answer the question about HOW you arrived at those figures?

DO you mean how I get the values? For the values 1,4,and 8, they are pre-defined. The desired output are based on this formula
(((x-x2) * (x-x3)) / ((x1-x2) * (x1-x3))) * y1 + (((x-x1) * (x-x3)) / ((x2-x1) * (x2-x3))) * y2 + (((x-x1) * (x-x2)) / ((x3-x1) * (x3-x1))) * y3

x = current ID;
x1 = the first known ID (second previous known ID); ---> 1
x2 = the second known ID (first previous known ID); ---> 4
x3 = the third known ID (next known ID); ---> 8
y1 = the first known value (the value of ID x1)
y2 = the second known value (the value of ID x2)
y3 = the third known value (the value of ID x3)

grail 01-22-2013 02:46 AM

And are you saying you cannot implement the formula in awk? Seems fairly straight forward.

millgates 01-22-2013 03:16 AM

Again, it gives a different values than what you showed in the first post. Perhaps the last parenthesis should have been (x3-x2) instead of (x3-x1)?
But even then it does not explain the values 33.something.
Anyway, you just described the algorithm, so you obviously know how to do it. So what exactly is preventing you from putting it into a script?


All times are GMT -5. The time now is 07:26 PM.