Share your knowledge at the LQ Wiki.
 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, 12: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 12:59 AM.
01-21-2013, 03:48 AM   #2
millgates
Member

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

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, 04: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, 07:51 AM #4 grail LQ Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 9,516 Rep: You did not answer the question about HOW you arrived at those figures?
01-21-2013, 08: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 08:19 PM.

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

 Thread Tools Search this Thread Search this Thread: Advanced Search

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post eminempark Programming 11 01-21-2013 12:42 AM lamletoi Linux - Newbie 3 05-01-2012 07:56 AM cjreyn Linux - Desktop 0 01-27-2012 03:48 PM leonidg Programming 5 03-06-2005 03:47 AM jtp51 Slackware - Installation 2 10-06-2004 11:10 AM

LinuxQuestions.org

All times are GMT -5. The time now is 03:21 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -
 Advertisement
 My LQ
 Write for LQ LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
 Syndicate Latest Threads   LQ News Twitter: @linuxquestions Facebook: linuxquestions Google+: linuxquestions