Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <locale>
  4. #include <cmath>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. double function1(double x) {
  9.     return (x - 2)*(x - 2) - 3;
  10. }
  11.  
  12. double function2(double x, double s) {
  13.     return cos(x)*cos(x) - pow(x, 1 / s);
  14. }
  15.  
  16. void dichotomy(double a, double b, double (*function1)(double)) {
  17.     int iterations = 0;
  18.     cout << setw(10) << "x" << setw(15) << "F(x)" << setw(13) << "iterations " << endl;
  19.     cout << "______________________________________________________" << endl;
  20.     double x = (a + b) / 2;
  21.     while (abs(function1(x))>0.000001) {
  22.  
  23.         iterations++;
  24.         if (function1(x) < 0) {
  25.             b = x;
  26.         }
  27.         else  a = x;
  28.         x = (a + b) / 2;
  29.     }
  30.     cout << setw(10) << x << setw(15) << function1(x) << setw(13) << iterations << endl;
  31.     cout << "______________________________________________________ \n" << endl;
  32.  
  33. }
  34.  
  35. void dichotomy2(double a0, double b0, double(*function2)(double,double)) {
  36.     double s0 = 1.95, sn = 2, ds = 0.01;
  37.     for (double s = s0; s <= sn; s += ds) {
  38.         double a = a0, b = b0;
  39.         int iterations = 0;
  40.         double x = (a + b) / 2;
  41.         while (abs(function2(x, s))>0.000001) {
  42.             iterations++;
  43.             if (function2(x, s) < 0) {
  44.                 b = x;
  45.             }
  46.             else  a = x;
  47.             x = (a + b) / 2;
  48.         }
  49.         cout << setw(10) << "s" << setw(15) << "x" << setw(15) << "F(x)" << setw(13) << "iterations " << endl;
  50.         cout << setw(10) << s << setw(15) << x << setw(15) << function2(x, s) << setw(13) << iterations << endl;
  51.     }
  52.     cout << "______________________________________________________" << endl;
  53. }
  54.  
  55.  
  56.  
  57. int main()
  58. {
  59.     dichotomy(-2.0, 1.0, function1);
  60.     dichotomy2(0.0, 1.0, function2);
  61.     system("pause");
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement