Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <cmath>
- #include <vector>
- #include <Windows.h>
- #include <stdlib.h>
- #include <fstream>
- #include <string.h>
- using namespace std;
- /* Общее */
- typedef double(*functiontype)(double x);
- typedef struct Node
- {
- double x, y;
- } Node;
- typedef struct Interval
- {
- double InitialNode, EndNode;
- } Interval;
- /* Частное */
- double Myfunc(double x)
- {
- return (x*x / 4 - atan(x) - 4);
- }
- double derivative_first(double x) {
- return (x / 2 - (1 / (1 + x * x)));
- }
- double derivative_second(double x) {
- return (0.5 + 2 * x / ((1 + x * x)*(1 + x * x)));
- }
- double secant_method(double X_0, double X_1, functiontype *f, double error, string file_name, double* iterations_massiv) {
- int i = 2;
- double X_i;
- double massiv[2];
- ofstream fout(file_name);
- massiv[0] = X_0;
- massiv[1] = X_1;
- fout << 0 << " " << X_0 << endl;
- fout << 1 << " " << X_1 << endl;
- do {
- X_i = massiv[1] - (massiv[1] - massiv[0]) / ((*f)(massiv[1]) - (*f)(massiv[0])) * (*f)(massiv[1]);
- massiv[0] = massiv[1];
- massiv[1] = X_i;
- fout << i << " " << X_i << endl;
- i++;
- } while (abs((*f)(X_i)) >= error);
- fout.close();
- if (X_0 > 0)
- iterations_massiv[1] = i; //Для + корня индекс 1, для отр - 0
- else
- iterations_massiv[0] = i;
- return X_i;
- }
- double Chebyshev_method(double X_0, functiontype *f, double error, string file_name, double* iterations_massiv) {
- int i = 1;
- double X_i, prev = X_0;
- ofstream fout(file_name);
- fout << 0 << " " << X_0 << endl;
- do {
- X_i = prev - (*f)(prev) / derivative_first(prev) - derivative_second(prev)*pow((*f)(prev), 2) / (2 * pow(derivative_first(prev), 3));
- prev = X_i;
- fout << i << " " << X_i << endl;
- i++;
- } while (abs((*f)(X_i)) >= error);
- fout.close();
- if (X_0 > 0)
- iterations_massiv[1] = i; //Для + корня индекс 1, для отр 0
- else
- iterations_massiv[0] = i;
- return X_i;
- }
- int main()
- {
- setlocale(LC_ALL, "RUS");
- functiontype Func = &Myfunc;
- Interval Interval; Interval.InitialNode = -10; Interval.EndNode = 10;
- int CountIterations = 5;
- double error = 1 / (pow(10, 10));
- double* iter_massiv_secant = new double[2];
- double* iter_massiv_Cheb = new double[2];
- cout << endl;
- cout << "Поиск корней с погрешностью до " << error << endl << endl;
- cout << "Метод Секущих: " << secant_method(4, 6, &Func, error, "D:/secant_iterations+.txt", iter_massiv_secant) << endl << endl;
- cout << "Метод Секущих: " << secant_method(-4, -3, &Func, error, "D:/secant_iterations-.txt", iter_massiv_secant) << endl << endl;
- cout << "Метод Чебышёва: " << Chebyshev_method(5, &Func, error, "D:/Chebyshev_iterations+.txt", iter_massiv_Cheb) << endl << endl;
- cout << "Метод Чебышёва: " << Chebyshev_method(-3.5, &Func, error, "D:/Chebyshev_iterations-.txt", iter_massiv_Cheb) << endl << endl;
- //Итерации
- cout << "Кол-во итераций для нахождения полож. корня методом Секущих : " << iter_massiv_secant[1] << endl;
- cout << "Кол-во итераций для нахождения полож. корня методом Чебышёва : " << iter_massiv_Cheb[1] << endl << endl;
- cout << "Кол-во итераций для нахождения отриц. корня методом Секущих : " << iter_massiv_secant[0] << endl;
- cout << "Кол-во итераций для нахождения отриц. корня методом Чебышёва : " << iter_massiv_Cheb[0] << endl << endl;
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement