vadimk772336

Untitled

Dec 16th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 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.         while (abs((*f)(X_i) / derivative_first(X_i)) >= error);
  54.     fout.close();
  55.  
  56.     if (X_0 > 0)
  57.         iterations_massiv[1] = i; //Для + корня индекс 1, для отр - 0
  58.     else
  59.         iterations_massiv[0] = i;
  60.  
  61.     return X_i;
  62. }
  63.  
  64. double Chebyshev_method(double X_0, functiontype *f, double error, string file_name, double* iterations_massiv) {
  65.     int i = 1;
  66.     double X_i, prev = X_0;
  67.     ofstream fout(file_name);
  68.     fout << 0 << " " << X_0 << endl;
  69.  
  70.     do {
  71.         X_i = prev - (*f)(prev) / derivative_first(prev) - derivative_second(prev)*pow((*f)(prev), 2) / (2 * pow(derivative_first(prev), 3));
  72.         prev = X_i;
  73.         fout << i << " " << X_i << endl;
  74.         i++;
  75.     } //while (abs((*f)(X_i)) >= error);
  76.         while (abs((*f)(X_i)/derivative_first(X_i)) >= error);
  77.  
  78.     fout.close();
  79.  
  80.     if (X_0 > 0)
  81.         iterations_massiv[1] = i; //Для + корня индекс 1, для отр 0
  82.     else
  83.         iterations_massiv[0] = i;
  84.  
  85.     return X_i;
  86. }
  87.  
  88. int main()
  89. {
  90.     setlocale(LC_ALL, "RUS");
  91.     functiontype Func = &Myfunc;
  92.     Interval Interval; Interval.InitialNode = -10; Interval.EndNode = 10;
  93.     double error = 1 / (pow(10, 10)); //Задаем погрешность
  94.     double* iter_massiv_secant = new double[2];
  95.     double* iter_massiv_Cheb = new double[2];
  96.  
  97.     cout << endl;
  98.     cout << "Поиск корней с погрешностью до " << error << endl << endl;
  99.     cout << "Метод Секущих: " << secant_method(4, 6, &Func, error, "D:/secant_iterations+.txt", iter_massiv_secant) << endl << endl;
  100.     cout << "Метод Секущих: " << secant_method(-4, -3, &Func, error, "D:/secant_iterations-.txt", iter_massiv_secant) << endl << endl;
  101.     cout << "Метод Чебышёва: " << Chebyshev_method(5, &Func, error, "D:/Chebyshev_iterations+.txt", iter_massiv_Cheb) << endl << endl;
  102.     cout << "Метод Чебышёва: " << Chebyshev_method(-3.5, &Func, error, "D:/Chebyshev_iterations-.txt", iter_massiv_Cheb) << endl << endl;
  103.  
  104.     //Итерации
  105.     cout << "Кол-во итераций для нахождения полож. корня методом Секущих  : " << iter_massiv_secant[1] << endl;
  106.     cout << "Кол-во итераций для нахождения полож. корня методом Чебышёва : " << iter_massiv_Cheb[1] << endl << endl;
  107.     cout << "Кол-во итераций для нахождения отриц. корня методом Секущих  : " << iter_massiv_secant[0] << endl;
  108.     cout << "Кол-во итераций для нахождения отриц. корня методом Чебышёва : " << iter_massiv_Cheb[0] << endl << endl;
  109.  
  110.     cout << endl;
  111.     system("pause");
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment