Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cmath>
  4. #include <vector>
  5. #include <Windows.h>
  6. #include <stdlib.h>
  7. #include <fstream>
  8. #include <string.h>
  9. using namespace std;
  10. /* Общее */
  11. typedef double(*functiontype)(double x);
  12. typedef struct Node
  13. {
  14.     double x, y;
  15. } Node;
  16. typedef struct Interval
  17. {
  18.     double InitialNode, EndNode;
  19. } Interval;
  20.  
  21. /* Частное */
  22. double Myfunc(double x)
  23. {
  24.     return (x*x / 4 - atan(x) - 4);
  25. }
  26.  
  27. double derivative_first(double x) {
  28.     return (x / 2 - (1 / (1 + x * x)));
  29. }
  30.  
  31. double derivative_second(double x) {
  32.     return (0.5 + 2 * x / ((1 + x * x)*(1 + x * x)));
  33. }
  34.  
  35. double secant_method(double X_0, double X_1, functiontype *f, double error, string file_name, double* iterations_massiv) {
  36.     int i = 2;
  37.     double X_i;
  38.     double massiv[2];
  39.     ofstream fout(file_name);
  40.  
  41.     massiv[0] = X_0;
  42.     massiv[1] = X_1;
  43.     fout << 0 << " " << X_0 << endl;
  44.     fout << 1 << " " << X_1 << endl;
  45.  
  46.     do {
  47.         X_i = massiv[1] - (massiv[1] - massiv[0]) / ((*f)(massiv[1]) - (*f)(massiv[0])) * (*f)(massiv[1]);
  48.         massiv[0] = massiv[1];
  49.         massiv[1] = X_i;
  50.         fout << i << " " << X_i << endl;
  51.         i++;
  52.     } while (abs((*f)(X_i)) >= error);
  53.     fout.close();
  54.  
  55.     if (X_0 > 0)
  56.         iterations_massiv[1] = i; //Для + корня индекс 1, для отр - 0
  57.     else
  58.         iterations_massiv[0] = i;
  59.  
  60.     return X_i;
  61. }
  62.  
  63. double Chebyshev_method(double X_0, functiontype *f, double error, string file_name, double* iterations_massiv) {
  64.     int i = 1;
  65.     double X_i, prev = X_0;
  66.     ofstream fout(file_name);
  67.     fout << 0 << " " << X_0 << endl;
  68.  
  69.     do {
  70.         X_i = prev - (*f)(prev) / derivative_first(prev) - derivative_second(prev)*pow((*f)(prev), 2) / (2 * pow(derivative_first(prev), 3));
  71.         prev = X_i;
  72.         fout << i << " " << X_i << endl;
  73.         i++;
  74.     } while (abs((*f)(X_i)) >= error);
  75.  
  76.     fout.close();
  77.  
  78.     if (X_0 > 0)
  79.         iterations_massiv[1] = i; //Для + корня индекс 1, для отр 0
  80.     else
  81.         iterations_massiv[0] = i;
  82.  
  83.     return X_i;
  84. }
  85.  
  86. int main()
  87. {
  88.     setlocale(LC_ALL, "RUS");
  89.     functiontype Func = &Myfunc;
  90.     Interval Interval; Interval.InitialNode = -10; Interval.EndNode = 10;
  91.     int CountIterations = 5;
  92.     double error = 1 / (pow(10, 10));
  93.     double* iter_massiv_secant = new double[2];
  94.     double* iter_massiv_Cheb = new double[2];
  95.  
  96.     cout << endl;
  97.     cout << "Поиск корней с погрешностью до " << error << endl << endl;
  98.     cout << "Метод Секущих: " << secant_method(4, 6, &Func, error, "D:/secant_iterations+.txt", iter_massiv_secant) << endl << endl;
  99.     cout << "Метод Секущих: " << secant_method(-4, -3, &Func, error, "D:/secant_iterations-.txt", iter_massiv_secant) << endl << endl;
  100.     cout << "Метод Чебышёва: " << Chebyshev_method(5, &Func, error, "D:/Chebyshev_iterations+.txt", iter_massiv_Cheb) << endl << endl;
  101.     cout << "Метод Чебышёва: " << Chebyshev_method(-3.5, &Func, error, "D:/Chebyshev_iterations-.txt", iter_massiv_Cheb) << endl << endl;
  102.  
  103.     //Итерации
  104.     cout << "Кол-во итераций для нахождения полож. корня методом Секущих  : " << iter_massiv_secant[1] << endl;
  105.     cout << "Кол-во итераций для нахождения полож. корня методом Чебышёва : " << iter_massiv_Cheb[1] << endl << endl;
  106.     cout << "Кол-во итераций для нахождения отриц. корня методом Секущих  : " << iter_massiv_secant[0] << endl;
  107.     cout << "Кол-во итераций для нахождения отриц. корня методом Чебышёва : " << iter_massiv_Cheb[0] << endl << endl;
  108.  
  109.     cout << endl;
  110.     system("pause");
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement