Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <cmath>
  5. using namespace std;
  6. void bisekcja(float a, float b, float eps);
  7. float funkcja(float x);
  8. float funkcja2(float x);
  9. float funkcja3(float x);
  10.  
  11. int main()
  12. {
  13.     int liczba = 0;
  14.     do {
  15.         cout << "MENU" << endl;
  16.         cout << "1. Pierwiastek funkcji f1(x) = sin(x)cos(x)+5x+1" << endl;
  17.         cout << "2. Pierwiastek funkcji f2(x) = (π-x*x+sin(x))/sqrt(25-x)" << endl;
  18.         cout << "3. Pierwiastek funkcji f3(x) = exp(-1.5x)-0.3x*x" << endl;
  19.         cout << "4. Koniec" << endl;
  20.         cin >> liczba;
  21.         switch (liczba)
  22.         {
  23.         case 1:
  24.            
  25.             break;
  26.         }
  27.     }
  28.     while (liczba != 4);
  29.  
  30.         /**
  31.         float a, b, eps, c, pierw;
  32.         cout << "Podaj a: " << endl;
  33.         cin >> a;
  34.         cout << "Podaj b: " << endl;
  35.         cin >> b;
  36.         cout << "Podaj eps: " << endl;
  37.         cin >> eps;
  38.  
  39.         bisekcja(a, b, eps);
  40.         */
  41.  
  42.         _getch();
  43. }
  44. float funkcja(float x)
  45. {
  46.     float f;
  47.     f = sin(x)*cos(x)+5*x+1;
  48.     return f;
  49. }
  50. float funkcja2(float x)
  51. {
  52.     float f;
  53.     f = (3.14 - x*x + sin(x)) / sqrt(25 - x);
  54.     return f;
  55. }
  56. float funkcja3(float x)
  57. {
  58.     float f;
  59.     f = exp(-1.5*x) - 0.3*x*x;
  60.     return f;
  61. }
  62.  
  63.  
  64.  
  65. void bisekcja(float a, float b, float eps)
  66. {
  67.     float pierw, c, i;
  68.     i = 0;
  69.     if (funkcja(a) == 0)
  70.     {
  71.         pierw = a;
  72.         cout << "Pierwiastek: " << a << endl;
  73.     }
  74.     else
  75.     {
  76.         if (funkcja(b) == 0)
  77.             cout << "Pierwiastek: " << b << endl;
  78.         pierw = b;
  79.     }
  80.     do
  81.     {
  82.  
  83.         c = (a + b) / 2;
  84.         if (funkcja(c) == 0)
  85.         {
  86.             pierw = c;
  87.         }
  88.         if (funkcja(a)*funkcja(c)<0)
  89.         {
  90.             b = c;
  91.         }
  92.         else
  93.             a = c;
  94.         i++;
  95.     } while (abs(a - b) >= eps);
  96.     pierw = c;
  97.     cout << "Pierwiastek" << c << endl;
  98.     cout << "Liczba iteracji" << i << endl;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement