Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define _USE_MATH_DEFINES
- #include <stdio.h>
- #include <conio.h>
- #include <locale.h>
- #include <time.h>
- #include <math.h>
- #include <iostream>
- #include <Windows.h>
- using namespace std;
- const double PI = 3.1415926535897932384626433832795;
- typedef double(*functiontype)(double x);
- typedef struct Node
- {
- double x, y;
- }Node;
- double Myfunk(double x)
- {
- return cos(x);
- }
- long double R01(long double *v_v[2], long double X, int n)
- {
- double x = X - v_v[0][0];
- x *= (X - v_v[0][1]);
- long int fact = 1;
- for (int k = 2; k < n; ++k)
- {
- fact *= k;
- x *= (X - v_v[0][k]);
- x = x / fact;
- }
- return fabs(x);
- }
- long double lag1(long double* v_v[2], long double x, int n)
- {
- long double L = 0;
- long double* l = new long double[n];
- for (int i = 0; i < n; i++)
- l[i] = 1;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (i != j)
- l[i] *= (x - v_v[0][j]) / (v_v[0][i] - v_v[0][j]);
- for (int i = 0; i < n; i++)
- L += v_v[1][i] * l[i];
- return L;
- }
- long double lag(Node *Array, long double x, int Count)
- {
- long double L = 0;
- long double* l = new long double[Count];
- for (int i = 0; i < Count; i++)
- l[i] = 1;
- for (int i = 0; i < Count; i++)
- for (int j = 0; j < Count; j++)
- if (i != j)
- l[i] *= (x - Array[j].x) / (Array[i].x - Array[j].x);
- for (int i = 0; i < Count; i++)
- L += Array[i].y * l[i];
- return L;
- }
- long double R0(Node *Array, long double X, int Count)
- {
- double x = X - Array[0].x;
- x *= (X - Array[1].x);
- long int fact = 1;
- for (int k = 2; k < Count; ++k)
- {
- fact *= k;
- x *= (X - Array[k].x);
- x = x / fact;
- }
- return fabs(x);
- }
- void ValueUniformTable(functiontype *f, Node *Array, double Initial, double End, int Count)
- { // Создание равномерной таблицы значений
- double step = abs(Initial - End) / (Count - 1);
- Array[0].x = Initial;
- Array[0].y = (*f)(Array[0].x);
- cout << " " << "x" << "\t" << "y" << endl;
- cout << " " << Array[0].x << "\t" << Array[0].y << endl;
- for (int i = 1; i < Count; i++)
- {
- Array[i].x = Array[i - 1].x + step;
- Array[i].y = (*f)(Array[i].x);
- cout << " " << Array[i].x << "\t" << Array[i].y << endl;
- }
- cout << endl << endl;
- }
- void ValueChebyshevTable( Node *Array, double Initial, double End, int Count)
- { // Создание таблицы Чебышевских значений
- cout << " " << "x" << "\t\t" << "y" << endl;
- cout << " " << Array[0].x << "\t" << Array[0].y << endl;
- for (int i = 0; i < Count; i++)
- {
- long double coss = cos(M_PI * (2 * (i + 1) - 1) / (2 * Count));
- Array[i].x = 0.5 * (Initial + End) + 0.5 * (End - Initial) * coss;
- Array[i].y = cos(Array[i].x);
- cout << " " << Array[i].x << "\t" << Array[i].y << endl;
- }
- }
- double maxt(double Initial, double End, int Count, char ans)
- {
- {
- long double* v_v[2];
- v_v[0] = new long double[Count];
- v_v[1] = new long double[Count];
- if (ans == 'y')
- for (int i = 0; i < Count; i++)
- {
- long double coss = cos(M_PI * (2 * (i + 1) - 1) / (2 * Count));
- v_v[0][i] = 0.5 * (Initial + End) + 0.5 * (End - Initial) * coss;
- v_v[1][i] = cos(v_v[0][i]);
- }
- long double a1 = Initial;
- if (ans == 'n')
- for (int i = 0; i < Count; i++)
- {
- v_v[0][i] = a1;
- v_v[1][i] = cos(v_v[0][i]);
- a1 += (fabs(Initial) + fabs(End)) / long double(Count - 1);
- }
- long double c = R01(v_v, End, Count);
- for (long double x = End; x >= Initial; x -= 0.01f) // O(100,85) - center
- {
- if (c < fabs((lag1(v_v, x, Count) - cos(x)))) c = fabs((lag1(v_v, x, Count) - cos(x)));
- }
- delete[] v_v[0];
- delete[] v_v[1];
- return c;
- }
- }
- double maxf(double Initial, double End, int Count, char ans)
- {
- long double* v_v[2];
- v_v[0] = new long double[Count];
- v_v[1] = new long double[Count];
- if (ans == 'y')
- for (int i = 0; i < Count; i++)
- {
- long double coss = cos(M_PI * (2 * (i + 1) - 1) / (2 * Count));
- v_v[0][i] = 0.5 * (Initial + End) + 0.5 * (End - Initial) * coss;
- v_v[1][i] = cos(v_v[0][i]);
- }
- long double a1 = Initial;
- if (ans == 'n')
- for (int i = 0; i < Count; i++)
- {
- v_v[0][i] = a1;
- v_v[1][i] = cos(v_v[0][i]);
- a1 += (fabs(Initial) + fabs(End)) / long double(Count - 1);
- }
- long double c = R01(v_v, End, Count);
- for (long double x = End; x >= Initial; x -= 0.01f) // O(100,85) - center
- {
- if (c < fabs(R01(v_v, x, Count))) c = fabs(R01(v_v, x, Count));
- }
- delete[] v_v[0];
- delete[] v_v[1];
- return c;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- srand(static_cast<int>(time(0)));
- long double* v_v[2];
- int CountNodes;
- long double a, b;
- char ans;
- functiontype Func = &Myfunk;
- cout << "Введите количество точек: ";
- cin >> CountNodes;
- cout << "Введите промежуток [a, b] через пробел: ";
- cin >> a >> b;
- Node *ArrayNodes = new Node[CountNodes];
- cout << "Использовать узлы Чебышёва?(y/n): ";
- cin >> ans;
- if (ans == 'y')
- ValueChebyshevTable(ArrayNodes, a, b, CountNodes);
- if (ans == 'n')
- ValueUniformTable(&Func, ArrayNodes, a, b, CountNodes);
- long double d;
- HDC hDC = GetDC(GetConsoleWindow());
- HPEN PenW =
- CreatePen(PS_SOLID, 2, RGB(255, 255, 255));
- HPEN PenR = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
- HPEN PenG = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
- HPEN PenB = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
- SelectObject(hDC, PenW);
- MoveToEx(hDC, 100 - a * 20, 500, NULL);
- LineTo(hDC, 500 - a * 20, 500);
- MoveToEx(hDC, 300 - a * 20, 300, NULL);
- LineTo(hDC, 300 - a * 20, 700);
- for (int i = 0; i < 5; i++)
- {
- MoveToEx(hDC, 300 - a * 20 - (i + 1) * 30, 500 - 3, NULL);
- LineTo(hDC, 300 - a * 20 - (i + 1) * 30, 500 + 3);
- MoveToEx(hDC, 300 - a * 20 - 3, 500 + (i + 1) * 30, NULL);
- LineTo(hDC, 300 - a * 20 + 3, 500 + (i + 1) * 30);
- MoveToEx(hDC, 300 - a * 20 + (i + 1) * 30, 500 - 3, NULL);
- LineTo(hDC, 300 - a * 20 + (i + 1) * 30, 500 + 3);
- MoveToEx(hDC, 300 - a * 20 - 3, 500 - (i + 1) * 30, NULL);
- LineTo(hDC, 300 - a * 20 + 3, 500 - (i + 1) * 30);
- }
- for (d = a - 3; d <= b + 3; d += 0.01f) // O(100,85) - center
- {
- MoveToEx(hDC, 30 * d + 300 - a * 20, -30 * cos(d) + 500, NULL);//10 - scale
- LineTo(hDC, 30 * d + 300 - a * 20, -30 * cos(d) + 500);
- }
- SelectObject(hDC, PenR);
- for (d = a - 3; d <= b + 3; d += 0.01f) // O(100,85) - center
- {
- MoveToEx(hDC, 30 * d + 300 - a * 20, -30 * lag(ArrayNodes, d, CountNodes) + 500, NULL);//10 - scale
- LineTo(hDC, 30 * d + 300 - a * 20, -30 * lag(ArrayNodes, d, CountNodes) + 500);
- }
- SelectObject(hDC, PenW);
- MoveToEx(hDC, 700 - a * 30, 500, NULL);
- LineTo(hDC, 1100 - a * 30, 500);
- MoveToEx(hDC, 900 - a * 30, 300, NULL);
- LineTo(hDC, 900 - a * 30, 700);
- for (int i = 0; i < 5; i++)
- {
- MoveToEx(hDC, 900 - a * 30 - (i + 1) * 30, 500 - 3, NULL);
- LineTo(hDC, 900 - a * 30 - (i + 1) * 30, 500 + 3);
- MoveToEx(hDC, 900 - a * 30 - 3, 500 + (i + 1) * 30, NULL);
- LineTo(hDC, 900 - a * 30 + 3, 500 + (i + 1) * 30);
- MoveToEx(hDC, 900 - a * 30 + (i + 1) * 30, 500 - 3, NULL);
- LineTo(hDC, 900 - a * 30 + (i + 1) * 30, 500 + 3);
- MoveToEx(hDC, 900 - a * 30 - 3, 500 - (i + 1) * 30, NULL);
- LineTo(hDC, 900 - a * 30 + 3, 500 - (i + 1) * 30);
- }
- HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(handle, FOREGROUND_RED);
- cout << "\t R0 - отн. погр., ";
- SelectObject(hDC, PenR);
- for (d = a; d <= b; d += 0.01f) // O(100,85) - center
- {
- MoveToEx(hDC, 30 * d + 900 - a * 30, -30 * fabs(R0(ArrayNodes, d, CountNodes)) + 500, NULL);//10 - scale
- LineTo(hDC, 30 * d + 900 - a * 30, -30 * fabs(R0(ArrayNodes, d, CountNodes)) + 500);
- }
- SetConsoleTextAttribute(handle, FOREGROUND_GREEN);
- cout << "Rт - точ. погр.";
- SelectObject(hDC, PenG);
- for (d = a; d <= b; d += 0.01f) // O(100,85) - center
- {
- MoveToEx(hDC, 30 * d + 900 - a * 30, -30 * fabs(lag(ArrayNodes, d, CountNodes) - cos(d)) + 500, NULL);//10 - scale
- LineTo(hDC, 30 * d + 900 - a * 30, -30 * fabs(lag(ArrayNodes, d, CountNodes) - cos(d)) + 500);
- }
- cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nВведите количество проверок: ";
- cin >> CountNodes;
- int *n1 = new int[CountNodes];
- cout << "Введите: ";
- for (int i = 0; i < CountNodes; i++)
- cin >> n1[i];
- for (int i = 0; i < CountNodes; i++)
- {
- cout << "При n = " << n1[i] << " max R0 = " << maxf(a, b, n1[i], ans) << "\t" << "; max Rт = " << maxt(a, b, n1[i], ans) << endl;
- }
- delete[] n1;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement