// program for lagrange interpolation #include <iostream> using namespace std; class lagrange { public: lagrange() { p = 0; } void askF(); void findL(); void askX(); void findP(); void askN() { cout << "Enter number of values: "; cin >> n; } void askXX() { cout << "Enter the value of X to find y: "; cin >> xx; } private: double Li[10],x[10],xx,Fi[10],p; int n; }; void lagrange::findL() { for(int i = 0; i < n; i ++ ) { Li[i] = 1; for(int j = 0; j<n;j++) { if(j!=i) { Li[i] *= (xx-x[j])/(x[i]-x[j]); } } } cout << endl << endl; for(int i = 0;i<n;i++) { cout <<"Li[" <<i <<"] : "<<Li[i] << endl; } } void lagrange::askX() { cout << "ENter given Xs :" << endl; for (int i = 0; i<n ;i++) { cout << "ENter x[" << i << "]: " ; cin >> x[i]; } } void lagrange::askF() { cout << "ENter F :" << endl; for (int i = 0; i<n ;i++) { cout << "ENter F[" << i << "]: " ; cin >> Fi[i]; } } void lagrange::findP() { for(int i = 0;i<n;i++) { p += Fi[i]*Li[i]; } cout <<endl<<"The Value of P"<< n-1 << "(" << xx << "): "<< p ; } int main() { lagrange l1; l1.askN(); l1.askXX(); l1.askX(); l1.askF(); l1.findL(); l1.findP(); return 0; }
C and C++ programs, games, softwares. Easy programming guide. Learn to code and enjoy coding in CoderNepal
Home » C++ Program code for Lagrange Interpolation
C++ Program code for Lagrange Interpolation
Subscribe to:
Posts (Atom)
No comments:
Post a Comment