Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ASD10_Integr_Trap.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <math.h>
- using namespace std;
- double f(double x)
- {
- return 2*log(cos(x)+2)-1;
- }
- double trap(double a, double b, int n)
- {
- double h = (double)(b-a)/n, s = 0;
- for(int i = 1; i <= n-1; i++)
- {
- s += f(a+i*h);
- }
- return h * (f(a) + f(b))/2 + s;
- }
- double simpson(double a, double b, int n) // чётное n => результат менее точный
- {
- double h = (b-a)/n, s = 0;
- for(int i = 1; i <= n-2; i += 2)
- {
- s += (2 * f(a+i*h) + 4*f(a+(i+1)*h))/3;
- }
- return h * ((f(a) + f(b))/2 + s);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int n = 10;
- double a = 0, b = 100;
- double result = trap(a, b, n);
- cout << "Trap (n = " << n << "): " << result << endl;
- n = 11;
- result = simpson(a, b, n);
- cout << "Simpson (n = " << n << "): " << result << endl;
- cout << endl;
- n = 20;
- result = trap(a, b, n);
- cout << "Trap (n = " << n << "): " << result << endl;
- n = 21;
- result = simpson(a, b, n);
- cout << "Simpson (n = " << n << "): " << result << endl;
- cout << endl;
- n = 50;
- result = trap(a, b, n);
- cout << "Trap (n = " << n << "): " << result << endl;
- n = 51;
- result = simpson(a, b, n);
- cout << "Simpson (n = " << n << "): " << result << endl;
- cout << endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement