Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <math.h>
- using namespace std;
- double MethodHalf();
- double SimpleIteration();
- double function(double x);
- int main()
- {
- int menu = 0;
- cout << "Methods:\n1.Half-division Method\n2.Simple Iteration Method\nChoice: "; cin >> menu;
- if (menu == 1) { MethodHalf(); }
- if (menu == 2) { SimpleIteration(); }
- return 0;
- }
- double function(double x)
- {
- return pow(x, 3) + 3 * x + 1;
- }
- double MethodHalf()
- {
- int n = 0;
- double a = 0, b = 0, eps = 0, c = 0;
- cout << "~~~~Half Division Method~~~~" << endl;
- cout << "Enter left border: "; cin >> a;
- cout << "Enter right border: "; cin >> b;
- if (function(a) * function(b) > 0)
- {
- cout << "#Error# Wrong interval\n";
- return 0;
- }
- cout << "Enter eps: "; cin >> eps; cout << endl;
- cout << "Left border: " << a << " || Right border: " << b << " || Eps: " << eps << endl;
- cout << "Equation: x^3 + 3*x + 1 = 0\n";
- do
- {
- c = (a + b) / 2;
- if (function(c) * function(a) <= 0) b = c;
- else a = c;
- n += 1;
- } while (fabs(a - b) >= eps);
- cout << "\n~~~~Result~~~~\n";
- cout << "Root of the equation: " << c << endl;
- cout << "Number of iteration: " << n << endl;
- cout << "Checking the resulting root: " << function(c) << endl;
- }
- double SimpleIteration()
- {
- int n = 0;
- double a = 0, b = 0, eps = 0, c = 0, x1 = 0, x0 = 0;
- cout << "~~~~Simple Iteration Method~~~~" << endl;
- cout << "Enter left border: "; cin >> a;
- cout << "Enter right border: "; cin >> b;
- cout << "Enter eps: "; cin >> eps; cout << endl;
- cout << "Enter X1: "; cin >> x1; cout << endl;
- cout << "Left border: " << a << " || Right border: " << b << " || Eps: " << eps << endl;
- cout << "Equation: x^3 + 3*x + 1 = 0\n";
- //c = (a + b) / 2;
- do
- {
- n++;
- x0 = x1;
- x1 = function(x0);
- } while (fabs(x1 - x0) <= eps);
- cout << "\n~~~~Result~~~~\n";
- cout << "Root of the equation: " << c << endl;
- cout << "Number of iteration: " << n << endl;
- cout << "Checking the resulting root: " << function(c) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement