ModestGenius

Task №3

Oct 7th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "cmath"
  4. #include <iomanip>
  5. using namespace std;
  6. #define PI 3.141592653589793238463
  7. double Lagrange (double t, int n, double *x, double *y)
  8. {
  9.     double L=0;
  10.     double *l = new double [n+1];
  11.     for( int i=0;i<=n;i++)
  12.     {
  13.         l[i]=1;
  14.         for(int j=0;j<=n;j++)
  15.         {
  16.             if(i==j)
  17.                 continue;
  18.             else
  19.                 l[i]*=(t-x[j])/(x[i]-x[j]);
  20.         }
  21.     }
  22.     for (int k=0;k<=n;k++)
  23.         L+=(l[k]*y[k]);
  24.     return L;
  25. }
  26.  
  27. int _tmain(int argc, _TCHAR* argv[])
  28. {
  29.     int n;
  30.     double a,b;
  31.     cout<<"ENTER a: ";
  32.     cin>>a;
  33.     cout<<"ENTER b: ";
  34.     cin>>b;
  35.     cout<<"ENTER n: ";
  36.     cin>>n;
  37.     double *t = new double [n+1];
  38.     double *x = new double [n+1];
  39.     double *y = new double [n+1];
  40.     for(int i=0;i<=n;i++)
  41.         t[i]=cos(PI*(2*i+1)/(2*n+2));
  42.    
  43.     for(int i=0;i<=n;i++)
  44.     {
  45.         x[i]=(a+b)/2+t[i]*(b-a)/2;
  46.         y[i]=fabs(x[i]);
  47.         cout<<x[i]<<"\t"<<y[i]<<endl;
  48.     }
  49.     cout<<endl;
  50.  
  51.     for(int i=0;i<n;i++)
  52.     {
  53.         cout<<Lagrange(x[i], n, x, y)<<endl;
  54.         cout<<"\t"<<Lagrange((x[i+1]+x[i])/2, n, x, y)<<endl;
  55.     }
  56.  
  57.     system ("pause");
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment