Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // printf, scanf
- #include <conio.h> // getch
- #include <math.h> // fabs, pow, ceil\floor, log, log10, exp, sqrt
- #include <stdlib.h> // randomize, rand
- #include <windows.h> // SetConsoleOutputCP(1251); SetConsoleCP(1251);
- //-------------------------------------------------------------------------—
- const double ee[] = { 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001, 0.00000001, 0.000000001, 0.0000000001 };
- const int key_func[] = { 23, 26 };
- int input(double& a, double& b, int& n) {
- printf("Введите а = ");
- scanf("%lf", &a);
- if ((a <= 0) || (a >= 2))
- {
- printf("Некорректное значение а \n");
- return 1;
- }
- printf("Введите b = ");
- scanf("%lf", &b);
- if ((b >= 2) || (b <= a))
- {
- printf("Некорректное значение b \n");
- return 1;
- }
- printf("Введите n = ");
- scanf("%d", &n);
- if ((n < 1) || (n > 10))
- {
- printf("Некорректное значение n \n");
- return 1;
- }
- return 0;
- }
- double func23(double x)
- {
- double f = 0;
- f = 1.0 / (pow(x, 2.0 / 3) + 0.7 * sin(x) - log(x + 1)) - x;
- return f;
- }
- double func26(double x)
- {
- double f = 0;
- f = exp(-sin(x) * sin(x)) + 3 * x / (7 * (1 + sqrt(x))) - x;
- return f;
- }
- double FindResultMetodDelenieOtrezkaPopolam(double a, double b, long double e, double func(double x), int& k1) {
- double x;
- k1 = 0;
- do
- {
- x = (b + a) / 2.0;
- k1++;
- if (func(x) * func(b) < 0) {
- a = x;
- }
- else {
- b = x;
- }
- } while (b - a >= e);
- return x;
- }
- double FindResultMetodSekushih(double a, double b, long double e, double func(double x), int& k2) {
- double x1, x2, x3, p;
- k2 = 0;
- x1 = a;
- x2 = b;
- do
- {
- k2++;
- x3 = x1 - func(x1) / ((func(x2) - func(x1)) / (x2 - x1));
- x1 = x2;
- x2 = x3;
- p = x1 - x3;
- } while (fabs(p) >= e);
- return x3;
- }
- void FuncOut(const int* key_func, int j, double a, double b, int n, long double e, double func(double x), int& k1, int& k2)
- {
- int i;
- printf("|-----------------------------------------------------------------------------|\n");
- printf("| Функция №%d | Деление отрезка пополам | Метод секущих |\n", key_func[j]);
- printf("|-----------------|------------------------------|----------------------------|\n");
- printf("| Погрешность | Корень | Итераций | Корень | Итераций |\n");
- printf("|-----------------|-----------------|------------|-----------------|----------|\n");
- for (i = 0; i < n; i++)
- {
- double x1 = FindResultMetodDelenieOtrezkaPopolam(a, b, ee[i], *func, k1);
- double x2 = FindResultMetodSekushih(a, b, ee[i], *func, k2);
- printf("| %-15.*lf | %-15.*lf | %10d | %-15.*lf | %8d |\n"\
- , i + 2, ee[i], i + 2, x1
- , k1, i + 2, x2, k2);
- }
- printf("|-----------------------------------------------------------------------------|\n");
- printf("\n\n");
- }
- int main()
- {
- SetConsoleOutputCP(1251);
- int i, n, k1, k2, j, f1, f2;
- long double e;
- double a, b;
- double(*func[])(double x) = { func23, func26 };
- int anomaliya = input(a, b, n);
- if (anomaliya == 1) {
- printf("Введите правильные исходные данные");
- }
- else
- {
- for ((j = 0); j < 2; j++)
- {
- if (func[j](a) * func[j](b) > 0)
- {
- printf("Нет корня функции №%d на отрезке [A,B] \n", key_func[j]);
- }
- else
- {
- if (j == 0)
- {
- FuncOut(key_func, j, a, b, n, e, func23, k1, k2);
- }
- if (j == 1)
- {
- FuncOut(key_func, j, a, b, n, e, func26, k1, k2);
- }
- }
- }
- }
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement