Beruflich Dokumente
Kultur Dokumente
Search: Go
Not logged in
C++
TÊNIS GORGE SKULL
Information
Tutorials
Reference R$ 349,90 SAIBA MAIS
Articles
Forum
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
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.
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}}
Thanks.
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
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.
http://www.cplusplus.com/forum/general/216928/ 2/2