Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <locale>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. int iterations;
  9. int iterations2;
  10.  
  11. double function(double x) {
  12.     return (x-1)*(x-1) - 3;
  13. }
  14.  
  15. int derivative(double x) {
  16.     return 2 * x - 2;
  17. }
  18.  
  19.  
  20. double function2(double x) {
  21.     return cos(x)*cos(x)-pow(x, 1/5);
  22. }
  23.  
  24. void dichotomy(double a, double b) {
  25.     double x = (a + b) / 2;
  26.     while (abs(function(x))>0.000001) {
  27.        
  28.         iterations ++;
  29.         if (function(x) < 0) {
  30.             b = x;
  31.         }
  32.         else  a = x;
  33.         x = (a + b) / 2;
  34.     }
  35.     cout << "x0: "<< x << endl;
  36. }
  37.  
  38.  
  39.  
  40. int СhordMethod(double a, double b) {
  41.     double t;
  42.     while (fabs(b - a) >= 0.000001) {
  43.         iterations2++;
  44.         t = a + (function(b)*(b - a)) / (function(b) - function(a));  
  45.         if (function(a)*function(t)<0) {
  46.             b = t;
  47.         }
  48.         else if (function(t)*function(b)<0) {
  49.             a = t;
  50.         }
  51.         else return t;
  52.     }
  53.     cout << "x0: " << t<<endl;
  54.     return t;
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60.     dichotomy(-2.0, 1.0);
  61.     cout << "Number of iterations: " << iterations << endl;
  62.     СhordMethod(-2.0, 1.0);
  63.     cout << "Number of iterations: " << iterations2 << endl;
  64.     system("pause");
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement