Advertisement
Kocyk

calki numercyczne

Nov 22nd, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. double funkcja(double x)
  8. {
  9.     double w;
  10.     w=1.0/(1+pow(x,2));
  11.  
  12.     return w;
  13. }
  14. double mtrapezow(int n,double h,double tabfi[],double tabx[])
  15. {
  16.     double wynik=0;
  17.     wynik=(0.5*tabfi[0])+(0.5*tabfi[n]);
  18.     for(int i=1;i<n;i++) wynik+=tabfi[i];
  19.     wynik=wynik*h*2;
  20.     return wynik;
  21.  
  22. }
  23. double sim(int n,double h,double tabfi[],double tabx[])
  24. {
  25.     double sumaparz=0;
  26.     double sumanieparz=0;
  27.     double wynik=0;
  28.     wynik=tabfi[0]+tabfi[n];
  29.     for(int i=1;i<n;i++)
  30.     {
  31.         if(i%2==0) sumaparz+=tabfi[i];
  32.         else sumanieparz+=tabfi[i];
  33.     }
  34.     wynik+=((4.0*sumanieparz)+(2.0*sumaparz));
  35.     wynik=wynik*(h/3.0)*2;
  36.     return wynik;
  37.  
  38. }
  39. int main()
  40. {
  41.     double b=1;
  42.     double a=-1;
  43.     int n;
  44.     double h;
  45.     cout<<"Podaj n"<<endl;
  46.     cin>>n;
  47.  
  48.     double tabx[n+1];
  49.     double tabfi[n+1];
  50.  
  51.     cout<<endl;
  52.     h=(b-a)/n;
  53.  
  54.     double iksy=a;
  55.     for(int i=0;i<n+1;i++)
  56.     {
  57.         tabx[i]=iksy;
  58.         tabfi[i]=funkcja(tabx[i]);
  59.         iksy+=h;
  60.     }
  61.     for(int i=0;i<n+1;i++)
  62.     {
  63.             cout<<setw(5)<<tabx[i]<<setw(5)<<tabfi[i]<<endl;
  64.     }
  65.  
  66.     cout<<"przyblizona wartosc to : "<<mtrapezow(n,h,tabfi,tabx)<<endl;
  67.     if(n%2==0) cout<<"Przyblizona wartosc druga metoda to: "<<sim(n,h,tabfi,tabx);
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement