Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. double liniowa (double x)
  8. {
  9.     return 21*x-37;
  10. }
  11.  
  12. double kwadrat (double x)
  13. {
  14.  
  15.     return 2*x*x+3*x-5;
  16. }
  17.  
  18. double tryg (double x)
  19. {
  20.     return cos(x)*4;
  21. }
  22.  
  23. double zlozenie (double x)
  24. {
  25.     return fabs(x*x*sin(x));
  26. }
  27.  
  28. double wartosci(double x, int wybor)
  29. {
  30.     double wynik;
  31.     if(wybor==1)
  32.         wynik=liniowa(x);
  33.     else if (wybor==2)
  34.         wynik=kwadrat(x);
  35.     else if (wybor==3)
  36.         wynik=tryg(x);
  37.     else
  38.         wynik=zlozenie(x);
  39.         return wynik;
  40. }
  41.  
  42. double wartosci1(double x, int wybor)
  43. {
  44.     if(wybor==1)
  45.         return liniowa(x)*exp(-x);
  46.     else if (wybor==2)
  47.         return kwadrat(x)*exp(-x);
  48.     else if (wybor==3)
  49.         return tryg(x)*exp(-x);
  50.     else
  51.         return zlozenie(x)*exp(-x);
  52. }
  53.  
  54. void simpson (double a, double b, int wybor, int wybor2, double &wynik, double &wynik1, int &n)
  55. {
  56.     double dx, suma2, suma, x;
  57.     int i;
  58.     n++;
  59.     wynik+=wynik1;
  60.     dx=(b-a)/(n);
  61.     suma2=0;
  62.     suma=0;
  63.     for (i=1; i<n; i++)
  64.     {
  65.         x=a+i*dx;
  66.         suma+=wartosci1(x-dx/2, wybor);
  67.         suma2+=wartosci1(x, wybor);
  68.     }
  69.     suma+=wartosci1(b-dx/2, wybor);
  70.     if (wybor2==1)
  71.         wynik1=((b-a)/(6*n))*(wartosci1(a, wybor)+wartosci1(b, wybor)+2*suma2+4*suma);
  72.     else
  73.         wynik1=((b-a)/(6*n))*(wartosci1(a, wybor)+wartosci1(b, wybor)+2*suma2+4*suma);
  74. }
  75.  
  76. void laguerre (int wybor, double wezly[], double siema[], int n, double &wynik, double &wynik1)
  77. {
  78.     wynik=wynik1;
  79.     wynik1=0;
  80.     for (int i=0; i<=n; i++)
  81.         wynik1+=siema[i]*wartosci(wezly[i], wybor);
  82. }
  83.  
  84. int main()
  85. {
  86.     double a, b, eps, wynik=0, wynik1=0;
  87.     int wybor, wybor2, wyborwaga, n=1;
  88.     double waga=1;
  89.     double ile=0;
  90.     double x1[2]= {0.585789, 3.414214};
  91.     double x2[3]= {0.415775, 2.294280, 6.289945};
  92.     double x3[4]= {0.322548, 1.745761, 4.536620, 2.395071};
  93.     double x4[5]= {0.263560, 1.413403, 3.596426, 7.085810, 12.640801};
  94.  
  95.     double a1[2]= {0.853553, 0.146447};
  96.     double a2[3]= {0.711093, 0.278518, 0.010389};
  97.     double a3[4]= {0.603154, 0.357419, 0.038888, 0.000539};
  98.     double a4[5]= {0.521756, 0.398667, 0.075942, 0.003612, 0.000023};
  99.  
  100.     double *X[4]= {x1, x2, x3, x4};
  101.     double *A[4]= {a1, a2, a3, a4};
  102.     cout<<"Wybierz funkcje:"<<endl<<"1. funkcja liniowa"<<endl<<"2. wielomian"<<endl<<"3. funkcja trygonometryczna"<<endl<<"4. zlozenie"<<endl;
  103.     cin>>wybor;
  104.     if(wybor>=1 && wybor<=4)
  105.     {
  106.         cout << "1. Okreslony przedzial calkowania. \n2. Przedzial calkowania [0, inf]." << endl;
  107.         cin >> wybor2;
  108.         if (wybor2==1)
  109.         {
  110.             cout << "Okresl przedzial: " << endl;
  111.             cout << "Poczatek przedzialu: ";
  112.             cin >> a;
  113.             cout << "Koniec przedzialu: ";
  114.             cin >> b;
  115.             cout << "Podaj dokladnosc: ";
  116.             cin >> eps;
  117.             cout << endl;
  118.             a=0;
  119.             wynik1=0;
  120.            do
  121.                 {
  122.                     simpson(a,b,wybor,wybor2,wynik,wynik1,n);
  123.                     ile++;
  124.                 }
  125.                 while (fabs(wynik-wynik1)>eps);
  126.         }
  127.         else if (wybor2==2)
  128.         {
  129.             ile=0;
  130.             cout << "Podaj dokladnosc: ";
  131.             cin >> eps;
  132.             cout << endl;
  133.             a=0;
  134.             b=1;
  135.             wynik1=0;
  136.             do
  137.             {
  138.  
  139.                 simpson(a,b,wybor,wybor2,wynik,wynik1,n);
  140.                 a++;
  141.                 b++;
  142.                 ile++;
  143.             }
  144.             while (fabs(wynik1)>eps);
  145.        }
  146.        cout<<"Wynik metoda Newtona-Cotesa: "<<wynik1<<endl<<"Dla liczby przedzialow "<<ile<<endl;
  147.         int m=1;
  148.         wynik1=0;
  149.         do
  150.         {
  151.             laguerre(wybor,X[m-1],A[m-1],m,wynik,wynik1);
  152.             m++;
  153.         }
  154.         while (fabs(wynik1-wynik)>eps and m<=4);
  155.         cout<<"Wynik metoda Laguerre'a: "<<wynik1<<endl<<"Dla liczby wezlow n = "<<m<<endl;
  156.     }
  157.     else
  158.         cout<<"Nie ma takiej funkcji :<"<<endl;
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement