Advertisement
Guest User

Untitled

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