Advertisement
Czopski

Całkowanie numeryczne

Jun 5th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2.         #include <cstdlib>
  3.  
  4.         using namespace std;
  5.  
  6.         double f(double x) {
  7.                 return 3*(x*x)+x;
  8.         }
  9.  
  10.         double prostokat(double min, double max, int n) {
  11.                 double h = ((max - min) / n);
  12.                 double suma = 0;
  13.  
  14.                 for (int i = 0; i < n; i++) {
  15.                         double p = h*f(min + i*h);
  16.                         suma += p;
  17.                 }
  18.                 return suma;
  19.         }
  20.  
  21.         double trapez(double min, double max, int n) {
  22.                 double h = ((max - min) / n);
  23.                 double suma = 0;
  24.  
  25.                 for (int i = 0; i < n; i++) {
  26.                         double p = (f(min + i*h)+f(min+i*h+h))*h/2;
  27.                         suma += p;
  28.                 }
  29.                 return suma;
  30.         }
  31.  
  32.         double simpson(double min, double max, int n) {
  33.                 double suma = 0;
  34.                 double h = ((max - min) / n);
  35.  
  36.                 for (int i = 0; i < n; i++) {
  37.                         double f0 = f(min + i*h);
  38.                         double f1 = f(min + i*h+h/2);
  39.                         double f2 = f(min + i*h+h);
  40.  
  41.                         double p = h / (2 * 3) * (f0 + 4 * f1 + f2);
  42.                         suma += p;
  43.                 }
  44.                 return suma;
  45.         }
  46.  
  47.         int main(){
  48.  
  49.             cout.precision(4);
  50.             cout.setf(ios::fixed);
  51.  
  52.                 double wynik = prostokat(4, 12, 2 );
  53.                 cout << wynik <<endl;
  54.  
  55.                 double wynik3 = trapez(4, 12, 2 );
  56.                 cout << wynik3 << endl;
  57.  
  58.                 double wyunik2 = simpson(4, 12, 2 );
  59.                 cout<<wyunik2<<endl;
  60.  
  61.  
  62.  
  63.                 system("pause");
  64.             return 0;
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement