Facebook
From Chocolate Goose, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 254
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. double funkcja(double x)
  5. {
  6.         return (x - 2)*(x + 2);
  7. }
  8. double zerowe(double a, double b, double e)
  9. {
  10.         double c;
  11.         while (b - a > e)
  12.         {
  13.                 c = (a + b) / 2.0;
  14.  
  15.                 if (funkcja(a)*funkcja(c) < 0)
  16.                 {
  17.                         b = c;
  18.  
  19.                 }
  20.                 else
  21.                 {
  22.                         a = c;
  23.                 }
  24.  
  25.         }
  26.         return c;
  27.  
  28. }
  29.         double falsi(double a, double b, double e)
  30.         {
  31.                 double c;
  32.                 while (b - a > e)
  33.                 {
  34.                         c = (a*funkcja(b) - b*funkcja(a)) / (funkcja(b) - funkcja(a));
  35.                         if (funkcja(a)*funkcja(c) < 0)
  36.                         {
  37.                                 b = c;
  38.                         }
  39.                         else
  40.                         {
  41.                                 a = c;
  42.                         }
  43.                 }
  44.                 return c;
  45.         }
  46.         int main()
  47.         {
  48.                 double a, b, e;
  49.                 cout << "Podaj poczatek przedzialu\n"; cin >> a;
  50.                 cout << "Podaj koniec przedzialu\n"; cin >> b;
  51.                 cout << "Podaj dokladnosc\n"; cin >> e;
  52.                 cout << "Miejsce zerowe w punkcie ";
  53.                 cout << zerowe(a, b, e) << endl;
  54.                 cout << "Miejsce zerowe w punkcie ";
  55.                 cout << falsi(a, b, e) << endl;
  56.        
  57.         }