Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. y=kwadratura('cos')\
  2.  
  3. #include<iostream>
  4. #include<cmath>
  5. #include<iomanip>
  6.  using namespace std;
  7.  
  8. double funkcja(double x){
  9.     return cos(x);
  10. }
  11.  
  12. int main(){
  13. double x[5]={-0.906179845938664,-0.538469310105683,0,0.538469310105683,0.906179845938664};
  14. double A[5]={0.236926885056189,0.478628670499366,0.568888888888889,0.478628670499366,0.236926885056189};
  15. //y=A*feval(f,x)';
  16. double wynik = 0;
  17. for(int i = 0; i < 5; i++)
  18.     wynik += A[i] * funkcja(x[i]);
  19. cout <<" dla podanych danych \n x  =[-0.906179845938664,-0.538469310105683,0,0.538469310105683,0.906179845938664];\n A  =[0.236926885056189,0.478628670499366,0.568888888888889,0.478628670499366,0.236926885056189];\n"<<endl;
  20. cout <<"********************************************" << endl<<endl<< "WYNIK: " <<wynik<<endl;
  21. }
  22.  
  23.  
  24.  
  25.  
  26. #include<iostream>
  27. #include<cmath>
  28. #include<iomanip>
  29.  using namespace std;
  30.  
  31.  
  32. int main(){
  33. double x[5]={-0.906179845938664,-0.538469310105683,0,0.538469310105683,0.906179845938664};
  34. double A[5]={0.236926885056189,0.478628670499366,0.568888888888889,0.478628670499366,0.236926885056189};
  35. //y=A*feval(f,x)';
  36. double wynik = 0;
  37. for(int i = 0; i < 5; i++)
  38.     wynik += A[i] * cos(x[i]);
  39.  
  40. cout << wynik;
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. https://deepanshubhatti.blogspot.com/2013/10/to-evaluate-definite-integral-by-gauss.html
  49.  
  50. #include<iostream>
  51. #include<cmath>
  52. #include<iomanip>
  53.  
  54. using namespace std;
  55.  
  56.  
  57. long double f(long double x)
  58. {
  59.     long double d;
  60.     d=pow(x,3);
  61.     return d;
  62. }
  63.  
  64. //For Legendre's Polynomial Pn(x)
  65. long double pn(long double a[],int n,int m,long double x)
  66. {
  67.     int i;
  68.     long double p=0;
  69.     if(m==0)
  70.     {
  71.         for(i=0;i<=n;i=i+2)
  72.         {
  73.             if(x==0)
  74.                 break;
  75.             p+=a[i]*pow(x,i);
  76.         }
  77.     }
  78.     else
  79.     {
  80.         for(i=1;i<=n;i=i+2)
  81.         {
  82.             p+=a[i]*pow(x,i);
  83.         }
  84.     }
  85.     return p;
  86. }
  87.  
  88. //Derivative of Pn(x)
  89. long double dn(long double a[],int n,int m,long double x)
  90. {
  91.     int i;
  92.     long double p=0;
  93.     if(m==0)
  94.     {
  95.         for(i=0;i<=n;i=i+2)
  96.         {
  97.             if(x==0)
  98.                 break;
  99.             p+=i*a[i]*pow(x,i-1);
  100.         }
  101.     }
  102.     else
  103.     {
  104.         for(i=1;i<=n;i=i+2)
  105.         {
  106.             p+=i*a[i]*pow(x,i-1);
  107.         }
  108.     }
  109.     return p;
  110. }
  111.  
  112. //Factorial Function
  113. long double fact(int n)
  114. {
  115.     int i;
  116.     long double f=1;
  117.     for(i=2;i<=n;i++)
  118.     {
  119.         f*=i;
  120.     }
  121.     return f;
  122. }
  123.  
  124. //Main Function
  125. int main()
  126. {
  127.     int n,m,i,N;
  128.     double c,d;
  129.     cout<<"wpisz wartosc n dla Pn(x) : \n";
  130.     cin>>n;
  131.     cout<<"wpisz dolna granice a dla calki : \n";
  132.     cin>>c;
  133.     cout<<"wpisz gorna granice b dla calki : \n";
  134.     cin>>d;
  135.  
  136.     if(n<=0)
  137.         return 0;A C++ Program To evaluate a Definite Integral by Gauss Quadrature Formula.
  138.  
  139.     long double a[n],b,x,y[n],z[n],w[n],l,v,s,g=0,u[n];
  140.     m=n%2;
  141.     if(m==0)
  142.     {
  143.         N=n/2;
  144.     }
  145.     else
  146.     {
  147.         N=(n-1)/2;
  148.     }
  149.  
  150.     for(i=0;i<=N;i++)
  151.     {
  152.         a[n-2*i]=(pow(-1,i)*fact(2*n-2*i))/(pow(2,n)*fact(i)*fact(n-i)*fact(n-2*i));
  153.     }
  154.  
  155.     if(m==0)
  156.     {
  157.         cout<<"\nThe Legendre's Polynomial is : ";
  158.         cout<<a[0];
  159.         for(i=2;i<=n;i=i+2)
  160.             cout<<" + ("<<setprecision(10)<< a[i]<<") X^"<<i;
  161.     }
  162.     else
  163.     {
  164.         cout<<"\nThe Legendre's Polynomial is : ";
  165.         cout<<"("<<a[1]<<") X";
  166.         for(i=3;i<=n;i=i+2)
  167.             cout<<" + ("<<a[i]<<") X^"<<i;
  168.     }A C++ Program To evaluate a Definite Integral by Gauss Quadrature Formula.
  169.     cout<<endl;
  170.  
  171.     //Roots of Pn(x)
  172.     for(i=0;i<n;i++)
  173.     {
  174.         z[i]=cos(3.14*(i+0.75)/(n+0.5));
  175.         l=z[i];
  176.         do
  177.         {
  178.             s=l-(pn(a,n,m,l)/dn(a,n,m,l));
  179.             v=l;
  180.             l=s;
  181.         }
  182.         while(fabs(l-v)>0.0000000000000001);
  183.  
  184.         y[i]=l;
  185.         w[i]=2/((1-pow(l,2))*(dn(a,n,m,l)*dn(a,n,m,l)));
  186.     }
  187.  
  188.     for(i=0;i<n;i++)
  189.     {
  190.         u[i]=((d-c)*y[i]/2)+(c+d)/2;
  191.     }
  192.     cout<<"Roots\t\t\t\t"<<"Weights\n";
  193.     for(i=0;i<n;i++)
  194.     {
  195.         cout<<setprecision(15)<<y[i]<<"\t\t"<<setprecision(15)<<w[i]<<endl;
  196.     }
  197.  
  198.     for(i=0;i<n;i++)
  199.         g+=w[i]*f(u[i]);
  200.  
  201.     g=g*(d-c)/2;
  202.     cout<<"The Value of Integration is = "<<setprecision(10)<<g<<endl;
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement