1#include <iostream>2#include <cmath>3 using namespacestd;4 //calculates the integral of f (x) =1/x on [1,3]5 6 DoubleFDoublex) {7 return 1.0/(x+2);8 }9 Ten DoubleF1 (Doublex) { One return 1.0/(x+5); A } - DoubleF2 (Doublex) { - return 1.0/(x+7); the } - - DoubleF3 (Doublex) { - return 1.0/(x+9); + } - DoubleF4 (Doublex) { + return 1.0/(x+ One); A } at - intMain () { - //using three point Gauss Formula integral value -printf"%2.8f\n", (0.5555556*f (-0.7745967)+0.8888889*f (0)+0.5555556*f (0.7745967) )); - - //using five-point Gaussian formula to calculate the integral value inprintf"%2.8f\n",0.2369269*f (-0.9061799)+0.2369269*f (0.9061799)+0.4786287*f (-0.5384693)+0.4786287*f (0.5384693) + - 0.5688889*f (0) ); toprintf"\ n"); + - //Divide the interval 4 evenly, using the two-point Gaussian formula, and then accumulate the integral value theprintf"%2.8f\n", F1 (-1.0/SQRT (3.0)) +f1 (1.0/SQRT (3.0)) ); *printf"%2.8f\n", F2 (-1.0/SQRT (3.0)) +f2 (1.0/SQRT (3.0)) ); $printf"%2.8f\n", F3 (-1.0/SQRT (3.0)) +f3 (1.0/SQRT (3.0)) );Panax Notoginsengprintf"%2.8f\n", F4 (-1.0/SQRT (3.0)) +f4 (1.0/SQRT (3.0)) ); - theprintf"sum=%2.8f\n", F1 (-1.0/SQRT (3.0)) +f1 (1.0/SQRT (3.0)) +f2 (-1.0/SQRT (3.0)) +f2 (1.0/SQRT (3.0)) +f3 (-1.0/SQRT (3.0)) +f3 (1.0/SQRT (3.0)) +f4 (-1.0/SQRT (3.0)) +f4 (1.0/SQRT (3.0)) ); + A}
Gauss-Legendre formula for quadrature