Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <iomanip>
- #include <string>
- using namespace std;
- int precision, spacing;
- double x0, eps, k;
- double abs(double x)
- {
- if (x > 0)
- return x;
- else
- return -x;
- }
- bool continueWorking()
- {
- string response;
- cout << "Continue working?(y,n)";
- cin >> response;
- cout << endl;
- return response == "y" || response == "Y";
- }
- double f(double x, int k)
- {
- return x - k * cos(x);
- }
- double y1(double x)
- {
- return x - 5 * cos(x);
- }
- double y2(double x)
- {
- return x - 10 * cos(x);
- }
- double derivedF(double x, int k)
- {
- return 1 + k * sin(x);
- }
- void menu()
- {
- precision = 0;
- cout << "x0 = ";
- cin >> x0;
- cout << "k = ";
- cin >> k;
- cout << "eps = ";
- cin >> eps;
- cout << endl;
- double counter = eps;
- while(counter < 1)
- {
- precision++;
- counter *= 10;
- }
- spacing = precision + 4;
- }
- void task()
- {
- double current, last, beginOfInterval, endOfInterval, root;
- int n_max = 100000;
- int j = 0;
- menu();
- current = x0;
- do //Simple
- {
- j++;
- last = current;
- current = last - f(last, k);
- }
- while((abs(last - current) > eps) && (j < n_max));
- cout << "Simple iteration: ";
- cout << fixed << setw(spacing) << setprecision(precision) << current << endl;
- cout << "Number of iterations = " << j << endl << endl;
- j = 0;
- current = x0;
- do //Neuton
- {
- j++;
- last = current;
- current = last - f(last, k) / derivedF(last, k);
- }
- while((abs(last - current) > eps) && (j < n_max));
- cout << "Neuton method: ";
- cout << fixed << setw(spacing) << setprecision(precision) << current << endl;
- cout << "Number of iterations = " << j << endl << endl;
- j = 0;
- beginOfInterval = -k; //halving
- endOfInterval = k;
- root = (beginOfInterval + endOfInterval) / 2;
- while(abs(f(root, k) ) > eps)
- {
- j++;
- if (f(beginOfInterval, k) * f(root, k) < 0)
- endOfInterval = root;
- else
- beginOfInterval = root;
- root = (beginOfInterval + endOfInterval) / 2;
- }
- cout << "Halving method: ";
- cout << fixed << setw(spacing) << setprecision(precision) << root << endl;
- cout << "Number of iterations = " << j << endl << endl;
- }
- int universal(double (*f)(double) )
- {
- double beginOfInterval, endOfInterval, root, xl, xr;
- bool hasRoots = false;
- precision = 0;
- cout << "eps (in format 10^) = ";
- cin >> eps;
- cout << "xl = ";
- cin >> xl;
- cout << "xr = ";
- cin >> xr;
- cout << "Roots: " << endl;
- double counter = eps;
- while(counter < 1)
- {
- precision++;
- counter *= 10;
- }
- spacing = precision + 4;
- int j = 0;
- double step = 0.1;
- for(double i = xl; i < xr; i += step)
- {
- beginOfInterval = i;
- endOfInterval = i + step;
- root = (beginOfInterval + endOfInterval) / 2;
- if(f(beginOfInterval) * f(endOfInterval) < 0)
- {
- hasRoots = true;
- while(abs(f(root)) > eps)
- {
- j++;
- if (f(beginOfInterval) * f(root) < 0)
- endOfInterval = root;
- else
- beginOfInterval = root;
- root = (beginOfInterval + endOfInterval) / 2;
- }
- cout << fixed << setw(spacing) << setprecision(precision) << root << endl;
- }
- }
- if(hasRoots == false)
- cout << "None." << endl << endl;
- else
- cout << endl;
- return 0;
- }
- int main()
- {
- int answer;
- cout << "We are solving: x - k * cos(x) = 0" << endl;
- do
- {
- cout << "Type 3 to use universal function and any number to do tasks 1 or 2: ";
- cin >> answer;
- cout << endl;
- if(answer == 3)
- {
- universal(y1);
- universal(y2);
- }
- else
- task();
- }
- while(continueWorking());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement