Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- double function(double x);
- double fi(double x);
- double derivative(double x);
- double derivative_2(double x);
- int check_for_newton(double a, double b);
- void simple_iteration();
- void method_chord();
- void method_newton();
- void method_chebishev();
- bool sign(double a);
- int main() {
- int menu = 0;
- cout << "Choice method:\n1.Method Simple Iteration\n2.Method Chord\n3.Method Newton\n4.Method Chebishev\nEnter:"; cin >> menu;
- if (menu == 1) {
- simple_iteration();
- }
- if (menu == 2) { method_chord(); }
- if (menu == 3) { method_newton(); }
- if (menu == 4) { method_chebishev(); }
- return 0;
- }
- double function(double x) {
- return pow(x, 3) + 3 * x + 1;
- }
- double fi(double x) {
- return (-1 - pow(x, 3) / 3);
- }
- void simple_iteration() {
- double a, b, eps;
- int n = 0;
- cout << "~~~~~Method Simple Iteration~~~~~" << endl;
- cout << "Enter left border: "; cin >> a;
- cout << "Enter right border: "; cin >> b;
- cout << "Enter epsilon: "; cin >> eps;
- double x_i, x = a;
- do
- {
- x_i = x;
- x = fi(x_i);
- n++;
- } while (fabs(x_i - x) >= eps);
- cout << endl << "Root of the equation: " << x << endl;
- cout << "Count of iterations: " << n << endl;
- printf("Checking the resultating root: %1.10f\n", function(x));
- }
- void method_chord()
- {
- double a, b, eps;
- int n = 0;
- cout << "~~~~~Method Chord~~~~~" << endl;
- cout << "Enter left border: "; cin >> a;
- cout << "Enter right border: "; cin >> b;
- cout << "Enter epsilon: "; cin >> eps;
- double x_i, x = a;
- if (sign(a) == true)
- {
- x = b;
- do
- {
- x_i = x;
- x = x_i - ((function(x_i)) / (function(x_i) - function(a))) * (x_i - a);
- n++;
- } while (fabs(x_i - x) >= eps);
- }
- else
- {
- x = a;
- do
- {
- x_i = x;
- x = x_i - ((function(x_i)) / (function(x_i) - function(b))) * (x_i - b);
- n++;
- } while (fabs(x_i - x) >= eps);
- }
- cout << endl << "Root of the equation: " << x << endl;
- cout << "Count of iterations: " << n << endl;
- printf("Checking the resultating root: %1.10f\n", function(x));
- }
- void method_newton()
- {
- double a, b, eps;
- int n = 0;
- cout << "~~~~~Method Newton~~~~~" << endl;
- cout << "Enter left border: "; cin >> a;
- cout << "Enter right border: "; cin >> b;
- cout << "Enter epsilon: "; cin >> eps;
- double x_i, x = 0;
- if (check_for_newton(a, b) == -1)
- {
- x = a;
- cout << "As x0 we take the left border\n";
- }
- else if (check_for_newton(a, b) == 1)
- {
- x = b;
- cout << "As x0 we take the right border\n";
- }
- cout << "Function(a) * Derivative^2(a) = " << function(a) * derivative_2(a) << endl;
- cout << "Function(b) * Derivative^2(b) = " << function(b) * derivative_2(b) << endl;
- cout << "X0 = " << x << endl;
- do {
- x_i = x;
- x = x_i - (function(x_i)) / derivative(x_i);
- n++;
- } while (fabs(x_i - x) >= eps);
- cout << endl << "Root of the equation: " << x << endl;
- cout << "Count of iterations: " << n << endl;
- printf("Checking the resultating root: %1.15f\n", function(x));
- }
- void method_chebishev()
- {
- double a, b, eps;
- int n = 0;
- cout << "~~~~~Method Chebishev~~~~~" << endl;
- cout << "Enter left border: "; cin >> a;
- cout << "Enter right border: "; cin >> b;
- cout << "Enter epsilon: "; cin >> eps;
- double x_i, x = 0;
- if (check_for_newton(a, b) == -1)
- {
- x = a;
- cout << "As x0 we take the left border\n";
- }
- else if (check_for_newton(a, b) == 1)
- {
- x = b;
- cout << "As x0 we take the right border\n";
- }
- cout << "Function(a) * Derivative^2(a) = " << function(a) * derivative_2(a) << endl;
- cout << "Function(b) * Derivative^2(b) = " << function(b) * derivative_2(b) << endl;
- cout << "X0 = " << x << endl;
- do {
- x_i = x;
- //x = x_i - ((function(x_i)) / derivative(x_i)) - ((derivative_2(x_i) * pow(function(x_i),2)) / (2 * pow(derivative(x_i), 3)));
- x = x_i - ((function(x_i)) / derivative(x_i)) - ((derivative_2(x_i) * function(x_i) * function(x_i)) / (2 * pow(derivative(x_i), 3)));
- n++;
- } while (fabs(x_i - x) >= eps);
- cout << endl << "Root of the equation: " << x << endl;
- cout << "Count of iterations: " << n << endl;
- printf("Checking the resultating root: %1.10f\n", function(x));
- }
- bool sign(double a)
- {
- if (function(a) > 0) return true;
- else if (function(a) < 0) return false;
- }
- double derivative(double x)
- {
- return 3 * pow(x, 2) + 3;
- }
- double derivative_2(double x)
- {
- return 6 * x;
- }
- int check_for_newton(double a, double b)
- {
- if (function(a) * derivative_2(a) > 0)return -1;
- else if (function(b) * derivative_2(b) > 0)return 1;
- else return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement