Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. double e = 2.71828182846;
  9.  
  10. double f(double x) {
  11.     return pow(e, x);
  12. }
  13.  
  14. double F(int n) {
  15.     return (pow(((1 + sqrt(5)) / 2.0), n) - pow(((1 - sqrt(5)) / 2.0), n)) / sqrt(5);
  16. }
  17.  
  18. void DichotomyMethod(double EPSILON, double a, double b) {
  19.     ofstream fout("dichotomy.txt");
  20.     double x1, x2;
  21.     double d = EPSILON / 2.0;
  22.     double q;
  23.  
  24.     for (int i = 1; b - a > EPSILON; i++) {
  25.         x1 = (a + b - d) / 2;
  26.         x2 = (a + b + d) / 2;
  27.  
  28.         q = b - a;
  29.  
  30.         fout << i << " " << a << " " << b << " " << b - a << " ";
  31.  
  32.         if (f(x1) < f(x2))
  33.             b = x2;
  34.         else a = x1;
  35.  
  36.         fout << b - a << " " << q / (b - a) << endl;
  37.     }
  38.  }
  39.  
  40. void GoldenSectionMethod(double EPSILON, double a, double b) {
  41.     ofstream fout("goldenSection.txt");
  42.     double x1, x2;
  43.     double q;
  44.     int flag = 0;
  45.  
  46.     x1 = a + (3 - sqrt(5)) / 2.0 * (b - a);
  47.     x2 = a + (sqrt(5) - 1) / 2.0 * (b - a);
  48.  
  49.     double f1 = f(x1);
  50.     double f2 = f(x2);
  51.     q = b - a;
  52.  
  53.     for (int i = 1; b - a > EPSILON; i++) {
  54.         fout << i << " " << a << " " << b << " " << b - a << " ";
  55.         if (f1 < f2) {
  56.             b = x2;
  57.             x2 = x1;
  58.             f2 = f1;
  59.             flag = 1;
  60.         }
  61.         else {
  62.             a = x1;
  63.             x1 = x2;
  64.             f1 = f2;
  65.             flag = 0;
  66.         }
  67.         fout << b - a << " " << q / (b - a) << endl;
  68.         q = b - a;
  69.  
  70.         if (flag) {
  71.             x1 = a + (3 - sqrt(5)) / 2.0 * (b - a);
  72.             f1 = f(x1);
  73.         }
  74.         else {
  75.             x2 = a + (sqrt(5) - 1) / 2.0 * (b - a);
  76.             f2 = f(x2);
  77.         }
  78.     }
  79. }
  80.  
  81. void FibonacciMethod(double EPSILON, double a, double b) {
  82.     ofstream fout("Fibonacci.txt");
  83.     double x1, x2;
  84.     double q = b - a;
  85.     int n;
  86.  
  87.     for (n = 7; F(n) < (b - a) / EPSILON; n++);
  88.  
  89.     x1 = a + (F(n - 2) / F(n)) * (b - a);
  90.     x2 = a + (F(n - 1) / F(n)) * (b - a);
  91.  
  92.     double f1 = f(x1);
  93.     double f2 = f(x2);
  94.  
  95.     for (int i = 0; i < n - 3; i++) {
  96.         if (f1 < f2) {
  97.             b = x2;
  98.             x2 = x1;
  99.             f2 = f1;
  100.             x1 = a + (F(n - i - 3) / F(n - i - 1)) * (b - a);
  101.             f1 = f(x1);
  102.         }
  103.         else {
  104.             a = x1;
  105.             x1 = x2;
  106.             f1 = f2;
  107.             x2 = a + (F(n - i - 2) / F(n - i - 1)) * (b - a);
  108.             f2 = f(x2);
  109.         }
  110.         fout << i + 1 << " " << a << " " << b << " " << b - a << " ";
  111.         fout << b - a << " " << q / (b - a) << endl;
  112.         q = b - a;
  113.     }
  114.     x2 = x1 + EPSILON;
  115.     f2 = f(x2);
  116.     if (f1 < f2)
  117.         b = x1;
  118.     else a = x1;
  119.  
  120.     fout << n - 2 << " " << a << " " << b << " " << b - a << " ";
  121.     fout << b - a << " " << q / (b - a) << endl;
  122. }
  123.  
  124. void findInterval(double h) {
  125.     double x = 50;
  126.     double start = x;
  127.     int numberIteration = 0;
  128.     ofstream out("interval.txt");
  129.  
  130.     if (f(x) > f(x + h))
  131.         x = x + h;
  132.     else
  133.     {
  134.         x = x - h;
  135.         h = -h;
  136.     }
  137.  
  138.     out << "Number of iteration " << setw(20) << left << "Current interval " << endl;
  139.  
  140.     while (f(x) > f(x + h)) {
  141.         if (x < 0)
  142.             break;
  143.         x = x + h;
  144.         numberIteration++;
  145.         out << numberIteration << "\t" << "[" << x << ";" << start << "]" << "\t" << endl;
  146.     }
  147.  
  148. }
  149.  
  150. void main() {
  151.     double EPSILON;
  152.     double a = 0, b = 100;
  153.     cout << "EPSILON?" << endl;
  154.     cin >> EPSILON;
  155.     cout << "Dichotomy Method" << endl;
  156.     DichotomyMethod(EPSILON, a, b);
  157.     cout << "Golden Section Method" << endl;
  158.     GoldenSectionMethod(EPSILON, a, b);
  159.     cout << "Fibonacci Method" << endl;
  160.     FibonacciMethod(EPSILON, a, b);
  161.     cout << "Find Interval" << endl;
  162.     findInterval(0.1);
  163.     _getch();
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement