Sie sind auf Seite 1von 2

12/03/2018 Linear interpolation in C++ - C++ Forum

Search: Go
Not logged in

Forum General C++ Programming Linear interpolation in C++ register log in

C++
TÊNIS GORGE SKULL
Information
Tutorials
Reference R$ 349,90 SAIBA MAIS
Articles
Forum

Forum Linear interpolation in C++


Beginners
RezaAb (21) May 31, 2017 at 10:33am
Windows Programming
UNIX/Linux Programming Hi Guys,
General C++ Programming
Lounge I am trying to linearly interpolate a 2D vector. I tried different methods offered by the people, but I could not find
Jobs an appropriate answer.
Here is an example:
BOTA
BRADSTREE I have the 2D vector:
R$ 439,99 T HALF CAB A[2][5]={{1,5,10,15,20}}{{0.3,0.5,0.8,0.10,0.14}}
and also I have the following 1D vector:
B[20]={{1,2,3,4,5,6,...,17,18,19,20}}

So since I am not very familiar with C++ programming, my question is how can I write an inline function for solving
this issue?

Thanks,
Last edited on May 31, 2017 at 10:59am

jonnin (2143) May 31, 2017 at 11:26am

not sure what your data is meaning..

if the user types in (???) you want to get (???) back out, from which (or both) of the vectors?

LI is not too hard -- you treat an array as a function/lookup for data points, you get the point above and below the
desired point, and compute the answer. If its outside your bounds, you can either reject it or continue the last line
from the last 2 points.

RezaAb (21) May 31, 2017 at 11:31am

Thanks for the reply, the final answer which I am trying to evaluate is a vector similar to B including interpolated
values based on the second row of the A. It might be a 2D vector:

B'[2][20]={{1,2,3,4,5,6,...,17,18,19,20}}{{0.3,0.35,0.4,0.5,....0.8,0.10,0.13,0.14}}

I was wondering if we have a library for this. I am trying to be efficient in time!

Thanks.

lastchance (1729) May 31, 2017 at 12:55pm

1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 using namespace std;
5
6 //======================================================================
7
8 // Returns interpolated value at x from parallel arrays ( xData, yData )
9 // Assumes that xData has at least two elements, is sorted and is strictly monotonic increasing
10 // boolean argument extrapolate determines behaviour beyond ends of array (if needed)
11
12 double interpolate( vector<double> &xData, vector<double> &yData, double x, bool extrapolate )
13 {
14 int size = xData.size();
15
16 int i = 0; // find left end of interval for interpo
17 if ( x >= xData[size - 2] ) // special case: beyond right end
18 {
19 i = size - 2;
20 }
21 else
22 {
23 while ( x > xData[i+1] ) i++;
24 }
25 double xL = xData[i], yL = yData[i], xR = xData[i+1], yR = yData[i+1]; // points on either side (unless beyond
26 if ( !extrapolate ) // if beyond ends of array and not extra
27 {
28 if ( x < xL ) yR = yL;
29 if ( x > xR ) yL = yR;
30 }
31
32 double dydx = ( yR - yL ) / ( xR - xL ); // gradient
33
34 return yL + dydx * ( x - xL ); // linear interpolation
35 }
36
37 //======================================================================
38
39 int main()
40 {
41 // Original data
42 vector<double> xData = { 1, 5, 10, 15, 20 };
43 vector<double> yData = { 0.3, 0.5, 0.8, 0.1, 0.14 };
44
45 // Set up some points for interpolation in xVals
46 const int NPTS = 20;

http://www.cplusplus.com/forum/general/216928/ 1/2
12/03/2018 Linear interpolation in C++ - C++ Forum
47 vector<double> xVals, yVals;
48 for ( int i = 1; i <= NPTS; i++ ) xVals.push_back( (double)i );
49
50 // Interpolate
51 for ( double x : xVals )
52 {
53 double y = interpolate( xData, yData, x, true );
54 yVals.push_back( y );
55 }
56
57 // Output
58 #define SP << fixed << setw( 15 ) << setprecision( 6 ) <<
59 #define NL << '\n'
60 cout << "Original data:\n";
61 for ( int i = 0; i < xData.size(); i++ ) cout SP xData[i] SP yData[i] NL;
62 cout << "\nInterpolated data:\n";
63 for ( int i = 0; i < xVals.size(); i++ ) cout SP xVals[i] SP yVals[i] NL;
64 }

Original data:
1.000000 0.300000
5.000000 0.500000
10.000000 0.800000
15.000000 0.100000
20.000000 0.140000

Interpolated data:
1.000000 0.300000
2.000000 0.350000
3.000000 0.400000
4.000000 0.450000
5.000000 0.500000
6.000000 0.560000
7.000000 0.620000
8.000000 0.680000
9.000000 0.740000
10.000000 0.800000
11.000000 0.660000
12.000000 0.520000
13.000000 0.380000
14.000000 0.240000
15.000000 0.100000
16.000000 0.108000
17.000000 0.116000
18.000000 0.124000
19.000000 0.132000
20.000000 0.140000

jonnin (2143) May 31, 2017 at 1:04pm

if I understood this

you have
y = f(x)
where X is currently 1,5,10,15 (which is annoying, because 1-5 is not the same as 5-10 .. its +4, +5, +5, +5, ...?!)
and you want the Y data for
X = 1,2,3.... N

Is that correct?

I don't know of anything built into the language to do it, but what I don't know can fill a lot of books. Im sure there
is a library out there that does this, its a common task. I couldn't say which one to pick. If you want to DIY, I can
probably help there.

RezaAb (21) May 31, 2017 at 1:31pm

Thanks, Jonnin. Lastchance's code solved my problem.

Topic archived. No new replies allowed.

SANDALIA TBL NEKKOL LS M

R$ 159,99 SAIBA MAIS

Home page | Privacy policy


© cplusplus.com, 2000-2017 - All rights reserved - v3.1
Spotted an error? contact us

http://www.cplusplus.com/forum/general/216928/ 2/2

Das könnte Ihnen auch gefallen