-2

I'm trying to print out the Lagrange Interpolation Form.

enter image description here

    double x0 = x[0];
    double x1 = x[1];
    double x2 = x[2];
    double x3 = x[3];
    double z0 = z[0];
    double z1 = z[1];
    double z2 = z[2];
    double z3 = z[3];
if (size == 2)
    {
        cout << "\nLagrange Polynomial Form: " << endl;
        cout << (1 / (x0 - x1)) * (z0) << "(x" << -x1 << ")" 
             << (1 / (x1 - x0)) * (z1) << "(x" << -x0 << ")" << endl;

    }
    else if (size == 3)
    {
        cout << "\nLagrange Polynomial Form: " << endl;
        cout << (1 / ((x0 - x1) * (x0 - x2)) * (z0)) << "(x" << -x1 << ")" << "(x" << -x2 << ")"
             << (1 / ((x1 - x0) * (x1 - x2)) * (z1)) << "(x" << -x0 << ")" << "(x" << -x2 << ")"
             << (1 / ((x2 - x0) * (x2 - x1)) * (z2)) << "(x" << -x2 << ")" << "(x" << -x0 << ")";


    }
    else if (size == 4)
    {
        cout << "\nLagrange Polynomial Form: " << endl;
        cout << (1 / ((x0 - x1) * (x0 - x2) * (x0 - x3)) * (z0)) << "(x" << -x1 << ")" << "(x" << -x2 << ")" << "(x" << -x3 << ")"
            << (1 / ((x1 - x0) * (x1 - x2) * (x1 - x3)) * (z1)) << "(x" << -x0 << ")" << "(x" << -x2 << ")" << "(x" << -x3 << ")"
            << (1 / ((x2 - x0) * (x2 - x1) * (x2 - x3)) * (z2)) << "(x" << -x0 << ")" << "(x" << -x1 << ")" << "(x" << -x3 << ")"
            << (1 / ((x3 - x0) * (x3 - x1) * (x3 - x2)) * (z3)) << "(x" << -x0 << ")" << "(x" << -x1 << ")" << "(x" << -x2 << ")";


    }

What I am trying to do is read the size of the given array then print out the formula base on size = 1,2,3... But it took too much line of code if I keep doing until size = 10. How can I simplify my code to for loop or something that easier?

cigien
  • 57,834
  • 11
  • 73
  • 112
John Han
  • 11
  • 1
  • 1
  • 5
  • It would help if you actually showed the equation as well. – cigien Oct 25 '20 at 23:16
  • @cigen what equation do you need? I don't understand? Did you mean the formula of Lagrange Polynomial form? – John Han Oct 25 '20 at 23:20
  • Yes, exactly :) – cigien Oct 25 '20 at 23:21
  • I don't know how to type the formua here. Can you please take a look the formula at this linke? https://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html – John Han Oct 25 '20 at 23:23
  • 1
    @JohnHan The compact form is a sum of products, which would translate to a couple of nested loops. – dxiv Oct 25 '20 at 23:27
  • 1
    You can add an image of equations if you want, like I edited in. – cigien Oct 25 '20 at 23:30
  • 2
    Looking at the formulas, their pattern is quite obvious. Extracting the polymonial coefficients from a convenient array into discrete variables is a step backwards. They should remain their original arrays, and the algorithm should be directly implemented, in the straightforward manner. The Lagrainge Interpolation Form's formulas can be directly translated into C++ code, as is. – Sam Varshavchik Oct 25 '20 at 23:30
  • Why do you rename: double x0 = x[0], etc.? – Elliott Oct 25 '20 at 23:35
  • @Elliott I did it for another part called Simplified Polynomial. – John Han Oct 25 '20 at 23:37

1 Answers1

0

Try this and see if it good for you:

#include <iostream>
#include <vector>

double calc_coeff(double yi, std::vector<double> x, size_t i, size_t n)
{
  double den_prod = 1;
  for(size_t loop = 0; loop < n; loop++)
  {
    if( loop != i)
    {
      den_prod *= (x[i] - x[loop]);
    }
  }
  double final_prod = yi/den_prod;
  return final_prod;
}

double lagrange_interpolation_sum(std::vector<double> x, std::vector<double> y)
{

  std::vector<double> den;
  size_t n = x.size();

  for(size_t i = 0; i < n; i++)
  {
    double coeff = calc_coeff(y[i], x, i, n);
    std::cout<<coeff<<" * ";
    for(size_t j = 0; j < n; j++)
    {
      if(j != i)
      {
        if(x[j] >= 0)
        {
          std::cout<<"(x - "<<x[j] << ") ";
        }
        else
        {
          std::cout<<"(x + "<<-x[j] << ") ";
        }
      }
    }
    std::cout<<std::endl;

  }
  return 0.0;

}

int main()
{
  std::vector<double> x = {4.5, 6.7, 7.8, 8.7, 6.5};
  std::vector<double> y = {1.1, 2.2, 3.3, 4.4, 5.5};
  lagrange_interpolation_sum(x,y);
  return 0;
}
revelationnow
  • 356
  • 1
  • 7