Advertisement
Wojtekd

Sinxdx

Dec 6th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. #define M_PI 3.141592
  6.  
  7. /* calkowanie numeryczne funkcji sin(x) metoda Simpsona */
  8.  
  9. int main( void )
  10. {
  11.     double zakres = 3.0;
  12.     double h = 0.1;
  13.  
  14.     int n = 30;
  15.  
  16.     double* x = new double[n];
  17.     double* f = new double[n];
  18.  
  19.     std::cout << n << std::endl;
  20.  
  21.     for (int i = 0; i <= n; i++)
  22.     {
  23.         x[i] = i*h;    
  24.     }
  25.    
  26.     for (int i = 0; i <= n; i++)
  27.     {
  28.         f[i] = sin(x[i]);
  29.     }
  30.  
  31.     for (int i = 0; i <= n; i++)
  32.     {
  33.         std::cout << "x=" << x[i] << "  f= " << f[i] << std::endl;
  34.     }
  35.    
  36.     double calka = 0;
  37.  
  38.     double c1 = f[0] + f[n];
  39.     double c2 = 0;
  40.  
  41.     for (int i = 0; i <= n - 2; i += 2)
  42.     {
  43.         c2 += f[i];
  44.     }
  45.     c2 *= 2;
  46.  
  47.     double c3 = 0;
  48.     for (int i = 1; i <= n - 1; i += 2)
  49.     {
  50.         c3 += f[i];
  51.     }
  52.     c3 *= 4;
  53.  
  54.     calka = (c1 + c2 + c3) * 0.0333333333333;
  55.  
  56.     std::cout << "calka sinxdx od 0 do 3 = " << calka << std::endl;
  57.    
  58.     system("pause");
  59.    
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement