Visit the LQ Articles and Editorials section
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] Script to solve second order (polynomial) interpolation
 User Name Remember Me? Password
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 01-21-2013, 01:55 AM #1 eminempark Member   Registered: Oct 2012 Posts: 56 Rep: 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... Last edited by eminempark; 01-21-2013 at 01:59 AM.
01-21-2013, 04:48 AM   #2
millgates
Member

Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep:
Quote:
 Originally Posted by eminempark 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 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?

01-21-2013, 05:02 AM   #3
eminempark
Member

Registered: Oct 2012
Posts: 56

Original Poster
Rep:
Quote:
 Originally Posted by millgates 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```

 01-21-2013, 08:51 AM #4 grail Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 7,698 Rep: You did not answer the question about HOW you arrived at those figures?
01-21-2013, 09:16 PM   #5
eminempark
Member

Registered: Oct 2012
Posts: 56

Original Poster
Rep:
Quote:
 Originally Posted by grail 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)

Last edited by eminempark; 01-21-2013 at 09:19 PM.

 01-22-2013, 03:46 AM #6 grail Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 7,698 Rep: And are you saying you cannot implement the formula in awk? Seems fairly straight forward.
 01-22-2013, 04:16 AM #7 millgates Member Contributing Member   Registered: Feb 2009 Location: 192.168.x.x Distribution: Slackware Posts: 651 Rep: 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?

 Tags awk, numbering, text processing