Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- double f(double x) {
- return 3*(x*x)+x;
- }
- double prostokat(double min, double max, int n) {
- double h = ((max - min) / n);
- double suma = 0;
- for (int i = 0; i < n; i++) {
- double p = h*f(min + i*h);
- suma += p;
- }
- return suma;
- }
- double trapez(double min, double max, int n) {
- double h = ((max - min) / n);
- double suma = 0;
- for (int i = 0; i < n; i++) {
- double p = (f(min + i*h)+f(min+i*h+h))*h/2;
- suma += p;
- }
- return suma;
- }
- double simpson(double min, double max, int n) {
- double suma = 0;
- double h = ((max - min) / n);
- for (int i = 0; i < n; i++) {
- double f0 = f(min + i*h);
- double f1 = f(min + i*h+h/2);
- double f2 = f(min + i*h+h);
- double p = h / (2 * 3) * (f0 + 4 * f1 + f2);
- suma += p;
- }
- return suma;
- }
- int main(){
- cout.precision(4);
- cout.setf(ios::fixed);
- double wynik = prostokat(4, 12, 2 );
- cout << wynik <<endl;
- double wynik3 = trapez(4, 12, 2 );
- cout << wynik3 << endl;
- double wyunik2 = simpson(4, 12, 2 );
- cout<<wyunik2<<endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement