diff --git a/MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.cpp b/MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.cpp index cf984006af5aea892cd6df1f75795d4277955d9d..33ab8f3ddc88b900e65babba64a291ddccf0d2b0 100644 --- a/MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.cpp +++ b/MathLib/InterpolationAlgorithms/PiecewiseLinearInterpolation.cpp @@ -52,13 +52,18 @@ double PiecewiseLinearInterpolation::getValue(double pnt_to_interpolate) const pnt_to_interpolate)); std::size_t const interval_idx = std::distance(_supp_pnts.begin(), it) - 1; + // support points. + double const x = _supp_pnts[interval_idx]; + double const x_r = _supp_pnts[interval_idx + 1]; + + // values. + double const f = _values_at_supp_pnts[interval_idx]; + double const f_r = _values_at_supp_pnts[interval_idx + 1]; + // compute linear interpolation polynom: y = m * (x - support[i]) + value[i] - const double m((_values_at_supp_pnts[interval_idx + 1] - - _values_at_supp_pnts[interval_idx]) / - (_supp_pnts[interval_idx + 1] - _supp_pnts[interval_idx])); + const double m = (f_r - f) / (x_r - x); - return m * (pnt_to_interpolate - _supp_pnts[interval_idx]) + - _values_at_supp_pnts[interval_idx]; + return m * (pnt_to_interpolate - x) + f; } double PiecewiseLinearInterpolation::getDerivative( @@ -81,17 +86,21 @@ double PiecewiseLinearInterpolation::getDerivative( if (interval_idx > 2 && interval_idx < _supp_pnts.size() - 1) { - double const tangent_right = - (_values_at_supp_pnts[interval_idx - 1] - - _values_at_supp_pnts[interval_idx + 1]) / - (_supp_pnts[interval_idx - 1] - _supp_pnts[interval_idx + 1]); - double const tangent_left = - (_values_at_supp_pnts[interval_idx - 2] - - _values_at_supp_pnts[interval_idx]) / - (_supp_pnts[interval_idx - 2] - _supp_pnts[interval_idx]); - double const w = - (pnt_to_interpolate - _supp_pnts[interval_idx]) / - (_supp_pnts[interval_idx - 1] - _supp_pnts[interval_idx]); + // left and right support points. + double const x_ll = _supp_pnts[interval_idx - 2]; + double const x_l = _supp_pnts[interval_idx - 1]; + double const x = _supp_pnts[interval_idx]; + double const x_r = _supp_pnts[interval_idx + 1]; + + // left and right values. + double const f_ll = _values_at_supp_pnts[interval_idx - 2]; + double const f_l = _values_at_supp_pnts[interval_idx - 1]; + double const f = _values_at_supp_pnts[interval_idx]; + double const f_r = _values_at_supp_pnts[interval_idx + 1]; + + double const tangent_right = (f_l - f_r) / (x_l - x_r); + double const tangent_left = (f_ll - f) / (x_ll - x); + double const w = (pnt_to_interpolate - x) / (x_l - x); return (1. - w) * tangent_right + w * tangent_left; } else