Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <cmath>
- using namespace std;
- const double pi = 3.1415;
- const double e = 2.7182;
- typedef double(*pointFunc)(double);
- double f1(double x1) {
- return (1/(1+pow(2*x1,0.5)));
- }
- double f2(double x2) {
- return (pow(e,x2)*sin(x2));
- }
- double f3(double x3) {
- return (1/(3+2*cos(x3)));
- }
- double simpson_integral(pointFunc f, double a, double b, int n) {
- const double h = (b-a)/n;
- double k1 = 0, k2 = 0;
- for(int i = 1; i < n; i += 2) {
- k1 += f(a + i*h);
- k2 += f(a + (i+1.)*h);
- }
- return h/3*(f(a) + 4*k1 + 2*k2);
- }
- int main() {
- setlocale(LC_ALL,"ru");
- double a1=0,a2=0,a3=0, b1=1,b2=pi/4,b3=pi/6, eps;
- double s1,s2,s3,s;
- int n = 1;
- cout << "\nВведите требуемую точность eps = ";
- cin >> eps;
- s1 = simpson_integral(f1, a1, b1, n);
- do {
- s = s1;
- n = 2 * n;
- s1 = simpson_integral(f1, a1, b1, n);
- }
- while (fabs(s1 - s) > eps);
- s2 = simpson_integral(f2, a2, b2, n);
- do {
- s = s2;
- n = 2 * n;
- s2 = simpson_integral(f2, a2, b2, n);
- } while (fabs(s2 - s) > eps);
- s3 = simpson_integral(f3, a3, b3, n);
- do {
- s = s3;
- n = 2 * n;
- s3 = simpson_integral(f3, a3, b3, n);
- } while (fabs(s3 - s) > eps);
- cout << "\n (1/(1+pow(2*x,0.5) Интеграл = " << s1 << " количество разбиений = " << n << endl;
- cout << "\n (pow(e,x)*sin(x)) Интеграл = " << s2 << " количество разбиений =" << n << endl;
- cout << "\n (1/(3+2*cos(x)) Интеграл = " << s3 << " количество разбиений =" << n << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement