Advertisement
csetanzil

Polynomial FItting

Jun 18th, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class PolynomialFit
  5. {
  6.     int i,j,k,n,N;
  7.     double *x,*y;
  8. public:
  9.     void GaussEM(double B[][4],double a[]);
  10.     void GetData();
  11.     void Method();
  12.  
  13.  
  14.  
  15. };
  16. void PolynomialFit::GetData()
  17. {
  18.         freopen("data.txt","r",stdin);
  19.         cout<<"Enter the number of data of each variable"<<endl;
  20.         cin>>N;
  21.         x = new double[N];
  22.         y = new double[N];
  23.         cout<<"Enter all value of X : "<<endl;
  24.         for (i=0;i<N;i++)
  25.         cin>>x[i];
  26.         cout<<"Enter the all value of Y : "<<endl;
  27.          for (i=0;i<N;i++)
  28.          cin>>y[i];
  29.          cout<<"Enter the degree of the equation :"<<endl;
  30.          cin>>n;
  31. }
  32.  
  33. void PolynomialFit::Method()
  34.     {
  35.         double X[2*n+1];
  36.         for (i=0;i<2*n+1;i++)
  37.         {
  38.             X[i]=0;
  39.             for (j=0;j<N;j++)
  40.                 X[i]=X[i]+pow(x[j],i);
  41.         }
  42.         double B [3][4];
  43.         double a[3];
  44.         for (i=0;i<=n;i++)
  45.             for (j=0;j<=n;j++)
  46.                 B[i][j]=X[i+j];
  47.          double Y[n+1];
  48.          for (i=0;i<n+1;i++)
  49.          {
  50.                 Y[i]=0;
  51.                 for (j=0;j<N;j++)
  52.                 Y[i]=Y[i]+pow(x[j],i)*y[j];
  53.         }
  54.         for (i=0;i<=n;i++)
  55.                 B[i][n+1]=Y[i];
  56.         n=n+1;
  57.  
  58.         GaussEM(B,a);
  59.  
  60.          cout<<"All Coefficient are below:\n";
  61.         for (i=0;i<n;i++)
  62.           cout<<"x^"<<i<<"="<<a[i]<<endl;
  63.        cout<<"Final Equation is"<<endl;
  64.       for (i=0;i<n;i++)
  65.          cout<<"+"<<a[i]<<""<<"x^"<<i;
  66.       cout<<endl;
  67.     }
  68.  
  69. void PolynomialFit::GaussEM(double B[][4],double a[])
  70. {
  71.     for (i=0;i<n;i++)
  72.         for (k=i+1;k<n;k++)
  73.             if (B[i][i]<B[k][i])
  74.                 for (j=0;j<=n;j++)
  75.                 {
  76.                     double temp=B[i][j];
  77.                     B[i][j]=B[k][j];
  78.                     B[k][j]=temp;
  79.                 }
  80.  
  81.     for (i=0;i<n-1;i++)
  82.         for (k=i+1;k<n;k++)
  83.             {
  84.                 double t=B[k][i]/B[i][i];
  85.                 for (j=0;j<=n;j++)
  86.                     B[k][j]=B[k][j]-t*B[i][j];
  87.             }
  88.     for (i=n-1;i>=0;i--)
  89.     {
  90.         a[i]=B[i][n];
  91.         for (j=0;j<n;j++)
  92.             if (j!=i)
  93.                 a[i]=a[i]-B[i][j]*a[j];
  94.         a[i]=a[i]/B[i][i];
  95.     }
  96. }
  97. int main()
  98. {
  99.     PolynomialFit ob;
  100.     ob.GetData();
  101.     ob.Method();
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement