Solutio in silico

Data Interpolation Source Code

The data interpolation code provides a quick way to estimate values from tabular data using linear and logarithmic interpolation.

Source Code

C++

Dependencies

There are no dependencies ouside of the C++ standard library.

Sample

The following excerpts of code demonstrates some functions and usage of the data interpolation functions:

// Interpolate for 1D vector data, with variable data spacing
std::vector<double> x_data = {1, 1.5, 3, 6};
std::vector<double> y_data = {2, 3, 6, 12};
double y = LinearInterpolation(x_data, y_data, 4.0);
// y = 8.0

// Interpolate for 1D vector data, with constant data spacing
std::vector<double> x_data = {1, 2, 3, 4};
std::vector<double> y_data = {2, 4, 6, 8};
double y = LinearInterpolationFast(x_data, y_data, 1.5, 1.0);
// y = 3.0

Interpolation Mathematics

Given a set of data containing two one-dimensional vectors \mathbf{x} and \mathbf{y}, where the desired values y_i are known at discrete intervals as a function of the parameter x_i, the goal of interpolation is to determine any value of y from a chosen value for x, including value not occuring at the known intervals. For linear interpolation:

y = fy_{m+1} + (1 - f)y_m

f = \frac{x - x_m}{x_{m+1} - x_m}

Where the variables a and b denote the values above and below the desired point, respectively. Similarly, for logarithmic interpolation:

y = y_{m+1}^f + y_m^{1-f}

f = \frac{log(x) - log(x_m)}{log(x_{m+1}) - log(x_m)}