Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- y=kwadratura('cos')\
- #include<iostream>
- #include<cmath>
- #include<iomanip>
- using namespace std;
- double funkcja(double x){
- return cos(x);
- }
- int main(){
- double x[5]={-0.906179845938664,-0.538469310105683,0,0.538469310105683,0.906179845938664};
- double A[5]={0.236926885056189,0.478628670499366,0.568888888888889,0.478628670499366,0.236926885056189};
- //y=A*feval(f,x)';
- double wynik = 0;
- for(int i = 0; i < 5; i++)
- wynik += A[i] * funkcja(x[i]);
- 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;
- cout <<"********************************************" << endl<<endl<< "WYNIK: " <<wynik<<endl;
- }
- #include<iostream>
- #include<cmath>
- #include<iomanip>
- using namespace std;
- int main(){
- double x[5]={-0.906179845938664,-0.538469310105683,0,0.538469310105683,0.906179845938664};
- double A[5]={0.236926885056189,0.478628670499366,0.568888888888889,0.478628670499366,0.236926885056189};
- //y=A*feval(f,x)';
- double wynik = 0;
- for(int i = 0; i < 5; i++)
- wynik += A[i] * cos(x[i]);
- cout << wynik;
- }
- https://deepanshubhatti.blogspot.com/2013/10/to-evaluate-definite-integral-by-gauss.html
- #include<iostream>
- #include<cmath>
- #include<iomanip>
- using namespace std;
- long double f(long double x)
- {
- long double d;
- d=pow(x,3);
- return d;
- }
- //For Legendre's Polynomial Pn(x)
- long double pn(long double a[],int n,int m,long double x)
- {
- int i;
- long double p=0;
- if(m==0)
- {
- for(i=0;i<=n;i=i+2)
- {
- if(x==0)
- break;
- p+=a[i]*pow(x,i);
- }
- }
- else
- {
- for(i=1;i<=n;i=i+2)
- {
- p+=a[i]*pow(x,i);
- }
- }
- return p;
- }
- //Derivative of Pn(x)
- long double dn(long double a[],int n,int m,long double x)
- {
- int i;
- long double p=0;
- if(m==0)
- {
- for(i=0;i<=n;i=i+2)
- {
- if(x==0)
- break;
- p+=i*a[i]*pow(x,i-1);
- }
- }
- else
- {
- for(i=1;i<=n;i=i+2)
- {
- p+=i*a[i]*pow(x,i-1);
- }
- }
- return p;
- }
- //Factorial Function
- long double fact(int n)
- {
- int i;
- long double f=1;
- for(i=2;i<=n;i++)
- {
- f*=i;
- }
- return f;
- }
- //Main Function
- int main()
- {
- int n,m,i,N;
- double c,d;
- cout<<"wpisz wartosc n dla Pn(x) : \n";
- cin>>n;
- cout<<"wpisz dolna granice a dla calki : \n";
- cin>>c;
- cout<<"wpisz gorna granice b dla calki : \n";
- cin>>d;
- if(n<=0)
- return 0;A C++ Program To evaluate a Definite Integral by Gauss Quadrature Formula.
- long double a[n],b,x,y[n],z[n],w[n],l,v,s,g=0,u[n];
- m=n%2;
- if(m==0)
- {
- N=n/2;
- }
- else
- {
- N=(n-1)/2;
- }
- for(i=0;i<=N;i++)
- {
- a[n-2*i]=(pow(-1,i)*fact(2*n-2*i))/(pow(2,n)*fact(i)*fact(n-i)*fact(n-2*i));
- }
- if(m==0)
- {
- cout<<"\nThe Legendre's Polynomial is : ";
- cout<<a[0];
- for(i=2;i<=n;i=i+2)
- cout<<" + ("<<setprecision(10)<< a[i]<<") X^"<<i;
- }
- else
- {
- cout<<"\nThe Legendre's Polynomial is : ";
- cout<<"("<<a[1]<<") X";
- for(i=3;i<=n;i=i+2)
- cout<<" + ("<<a[i]<<") X^"<<i;
- }A C++ Program To evaluate a Definite Integral by Gauss Quadrature Formula.
- cout<<endl;
- //Roots of Pn(x)
- for(i=0;i<n;i++)
- {
- z[i]=cos(3.14*(i+0.75)/(n+0.5));
- l=z[i];
- do
- {
- s=l-(pn(a,n,m,l)/dn(a,n,m,l));
- v=l;
- l=s;
- }
- while(fabs(l-v)>0.0000000000000001);
- y[i]=l;
- w[i]=2/((1-pow(l,2))*(dn(a,n,m,l)*dn(a,n,m,l)));
- }
- for(i=0;i<n;i++)
- {
- u[i]=((d-c)*y[i]/2)+(c+d)/2;
- }
- cout<<"Roots\t\t\t\t"<<"Weights\n";
- for(i=0;i<n;i++)
- {
- cout<<setprecision(15)<<y[i]<<"\t\t"<<setprecision(15)<<w[i]<<endl;
- }
- for(i=0;i<n;i++)
- g+=w[i]*f(u[i]);
- g=g*(d-c)/2;
- cout<<"The Value of Integration is = "<<setprecision(10)<<g<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement