LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
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

Reply
 
Search this Thread
Old 01-21-2013, 12:55 AM   #1
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Rep: Reputation: Disabled
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.
 
Old 01-21-2013, 03:48 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by eminempark View Post
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 View Post
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?
 
Old 01-21-2013, 04:02 AM   #3
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
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
 
Old 01-21-2013, 07:51 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,469

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
You did not answer the question about HOW you arrived at those figures?
 
Old 01-21-2013, 08:16 PM   #5
eminempark
Member
 
Registered: Oct 2012
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
Old 01-22-2013, 02:46 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,469

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
And are you saying you cannot implement the formula in awk? Seems fairly straight forward.
 
Old 01-22-2013, 03:16 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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?
 
  


Reply

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl script for linear interpolation eminempark Programming 11 01-21-2013 12:42 AM
Help to solve perl run shell script lamletoi Linux - Newbie 3 05-01-2012 07:56 AM
frame interpolation cjreyn Linux - Desktop 0 01-27-2012 03:48 PM
Cubic interpolation leonidg Programming 5 03-06-2005 03:47 AM
Should I recompile kernel 2.4.7 to remove rivafb in order to solve Nvidia issues? jtp51 Slackware - Installation 2 10-06-2004 11:10 AM


All times are GMT -5. The time now is 04:12 AM.

Main Menu
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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration