Skip to content

Instantly share code, notes, and snippets.

@markusbuchholz
Created June 20, 2021 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusbuchholz/b10dd5905b6ae1805ba0faa784f771c8 to your computer and use it in GitHub Desktop.
Save markusbuchholz/b10dd5905b6ae1805ba0faa784f771c8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <tuple>
#include <vector>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
//global
std::vector<float> interpolated;
//------------------------------------------------------------------
void runInterpolation(std::tuple<std::vector<float>, std::vector<float>, float> input)
{
std::vector<float> xx = std::get<0>(input);
std::vector<float> yy = std::get<1>(input);
float interX = std::get<2>(input);
int ijk{0};
int ii{0};
float w_interX{0};
int size = xx.size();
for (auto &fi : yy)
{
float factor{1};
int jj{0};
float xi = xx[ii];
for (auto &xj : xx)
{
float factor_i{1};
if (xj != xi)
{
factor_i = (interX - xj) / (xi - xj);
}
factor = factor * factor_i;
}
w_interX = w_interX + fi * factor;
ii++;
}
std::cout << " :: " << w_interX << std::endl;
interpolated.push_back(w_interX);
}
//------------------------------------------------------------------
void plotNumericalApproach(std::vector<float> xx1, std::vector<float> yy1, std::vector<float> xx2, std::vector<float> yy2)
{
plt::figure_size(1200, 780);
plt::title("Interpolation");
plt::plot(xx1, yy1);
plt::plot(xx2, yy2, "r-");
plt::show();
}
//------------------------------------------------------------------
int main()
{
//line----------------------------------------
std::vector<float> xi{1, 2, 4, 8};
std::vector<float> f_xi{1, 3, 7, 11};
std::vector<float> interX{1.2, 3.0, 6.0, 7.0};
//sin----------------------------------------
// std::vector<float> xi{0.0, 0.52, 1.052, 1.578, 2.105, 2.631, 3.157, 3.684, 4.210, 4.736, 5.26, 5.78, 6.31, 6.84, 7.36, 7.89, 8.42, 8.94, 9.473, 10.0};
// std::vector<float> f_xi{0.0, 0.50, 0.86, 0.9999, 0.86, 0.488, -0.016, -0.5163, -0.8766, -0.999, -0.852, -0.473, 0.032, 0.530, 0.884, 0.999, 0.843, 0.459, -0.048, -0.544};
// std::vector<float> interX{0.001, 0.75, 1.0, 1.5, 2.0, 2.5, 2.7, 3.4, 3.8, 4.1, 4.8, 5.3, 5.9, 6.4, 6.9, 7.5, 8.0, 8.5, 8.9, 9.1};
//quadratic---------------------------------
// std::vector<float> xi{-5.0, -3.88, -2.778, -1.667, -0.556, 0.556, 1.667, 2.778, 3.88, 5.0};
// std::vector<float> f_xi{25.0, 15.129, 7.738, 2.778, 0.308, 0.308, 2.778, 7.738, 15.129, 25.0};
// std::vector<float> interX{-4.6, -3.8, -2.7, -1.5, -0.4, 0.8, 2.1, 3.3, 3.9, 4.7};
std::tuple<std::vector<float>, std::vector<float>, float> input;
std::tuple<std::vector<float>, std::vector<float>> output;
for (auto &interXi : interX)
{
input = std::make_tuple(xi, f_xi, interXi);
runInterpolation(input);
}
plotNumericalApproach(xi, f_xi, interX, interpolated);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment