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);
- void simple_iteration();
- void method_chord();
- bool sign(double a);
- int main() {
- int menu = 0;
- cout << "Choice method:\n1.Method Simple Iteration\n2.Method Chord\nEnter:"; cin >> menu;
- if (menu == 1) {
- simple_iteration();
- }
- if (menu == 2) { method_chord(); }
- 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));
- }
- bool sign(double a)
- {
- if (function(a) > 0) return true;
- else if (function(a) < 0) return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement