Advertisement
bs9o

Gaussian Quad.

Mar 9th, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. double f(double x) {
  8.     return sqrt(2 * x + 1);
  9. }
  10.  
  11. double gaussianQuad(double *nodes, double *weights, double a, double b, int n) {
  12.     double res = 0;
  13.     for (int i = 0; i < n; i++) res += f((b + a) / 2 + (b - a) / 2 * nodes[i]) * weights[i];
  14.     res *= (b - a) / 2;
  15.     return res;
  16. }
  17.  
  18. int main() {
  19.     ofstream fout;
  20.     ifstream fin;
  21.     fin.open("input.txt");
  22.     fout.open("output.txt");
  23.     int n;
  24.     double a, b;
  25.     fin >> a >> b;
  26.     fin >> n;
  27.     double *nodes = new double[n], *weights = new double[n];
  28.     for (int i = 0; i < n; i++) fin >> weights[i] >> nodes[i];
  29.     fout << setprecision(10) << gaussianQuad(nodes, weights, a, b, n);
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement