/
aytsan_ns
/
Num_meth
Обзор
Документация
Войти
/
aytsan_ns
/
Num_meth
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Lab8/Lab_1/Source.c
340 строк
11 KB
aytsan-ns
Оформлена часть отчета 10 лабы, сделаны графики зависимостей ошибки и числа итераций от точности.
28 авг 2024, 20:14
28 авг 2024, 20:14
baf4b0a
Код
Авторство
О чём код?
#define _CRT_SECURE_NO_WARNINGS #include <math.h> #include <stdlib.h> #include <stdio.h> #include <time.h> #define PI 3.14159265358979323846 double function1(double x) { return (log(x + 2) + x); } double function2(double x) { return (x * x * x - 0.1 * x * x + 0.4 * fabs(x) + 2); } double* CreateUniMesh(int n, double a, double b) { double* points = malloc(sizeof(double) * n); if (points == NULL) { printf("Error!"); return NULL; } double h = (b - a) / (n - 1); for (int i = 0; i < n; i++) { points[i] = a + i * h; } return points; } double* CreateChebMesh(int n, double a, double b) { double* points = malloc(sizeof(double) * n); if (points == NULL) { printf("Error!"); return NULL; } for (int i = 0; i < n; i++) { points[i] = ((b - a) / 2) * cos(PI * (2 * (n - i - 1) + 1) / (2 * (n - 1) + 2)) + ((a + b) / 2); } return points; } double ComputeBracket(int n, double* values, double* points) { double result = 0; for (int i = 0; i <= n; i++) { double denominator = 1; for (int j = 0; j <= n; j++) { if (j != i) denominator *= (points[i] - points[j]); } result += values[i] * (1 / denominator); } return result; } double Newton(double x, int n, double* values, double* points) { double result = values[0]; double product_term = 1; for (int i = 1; i < n; i++) { product_term *= (x - points[i - 1]); result += ComputeBracket(i, values, points) * product_term; } return result; } double* values(double* points, int n, double(*func)(double)) { double* values = malloc(sizeof(double) * n); if (values == NULL) { printf("Error!"); return NULL; } for (int i = 0; i < n; i++) { values[i] = func(points[i]); } return values; } double omega(double x, int n, double* points) { double result = 1; for (int i = 0; i <= n; i++) { result *= x - points[i]; } return result; } double TeorError(double x, int n, double* points, double* values, double(*func)(double)) { double y = func(x); for (int i = 0; i < n; i++) { if (x == points[i]) return 0; } double* new_points = malloc(sizeof(double) * (n + 1)); if (new_points == NULL) { printf("Error!"); return 0; } for (int i = 0; i < n; i++) { new_points[i] = points[i]; } new_points[n] = x; double* new_values = malloc(sizeof(double) * (n + 1)); if (new_values == NULL) { printf("Error!"); return 0; } for (int i = 0; i < n; i++) { new_values[i] = values[i]; } new_values[n] = y; return ComputeBracket(n, new_values, new_points) * omega(x, n, points); } // ����������� ������ ������ ������������ ��������� /*void launche1() { double a1 = -1.5; double b1 = 2; double a2 = -1; double b2 = 1; int n = 5; double* points1 = CreateUniMesh(n, a1, b1); FILE* result_points1 = fopen("points1.txt", "w"); double* values1 = values(points1, n, function1); FILE* result_values1 = fopen("values1.txt", "w"); double* points_Cheb1 = CreateChebMesh(n, a1, b1); FILE* result_points_Cheb1 = fopen("points_cheb1.txt", "w"); double* values_Cheb1 = values(points_Cheb1, n, function1); FILE* result_values_Cheb1 = fopen("values_cheb1.txt", "w"); double* points2 = CreateUniMesh(n, a2, b2); FILE* result_points2 = fopen("points2.txt", "w"); double* values2 = values(points2, n, function2); FILE* result_values2 = fopen("values2.txt", "w"); double* points_Cheb2 = CreateChebMesh(n, a2, b2); FILE* result_points_Cheb2 = fopen("points_cheb2.txt", "w"); double* values_Cheb2 = values(points_Cheb2, n, function2); FILE* result_values_Cheb2 = fopen("values_cheb2.txt", "w"); for (int i = 0; i < n; i++) { fprintf(result_points_Cheb1, "%.10lf\n", points_Cheb1[i]); fprintf(result_points_Cheb2, "%.10lf\n", points_Cheb2[i]); fprintf(result_values_Cheb1, "%.10lf\n", values_Cheb1[i]); fprintf(result_values_Cheb2, "%.10lf\n", values_Cheb2[i]); fprintf(result_points1, "%.10lf\n", points1[i]); fprintf(result_points2, "%.10lf\n", points2[i]); fprintf(result_values1, "%.10lf\n", values1[i]); fprintf(result_values2, "%.10lf\n", values2[i]); } FILE* result_Newton1 = fopen("Newton1.txt", "w"); FILE* result_Newton2 = fopen("Newton2.txt", "w"); FILE* result_Newton_Cheb1 = fopen("Newton_cheb1.txt", "w"); FILE* result_Newton_Cheb2= fopen("Newton_cheb2.txt", "w"); FILE* result_error_Newton1 = fopen("error_Newton1.txt", "w"); FILE* result_error_Newton2 = fopen("error_Newton2.txt", "w"); FILE* result_error_Newton_Cheb1 = fopen("error_Newton_cheb1.txt", "w"); FILE* result_error_Newton_Cheb2 = fopen("error_Newton_cheb2.txt", "w"); FILE* result_teor_error_Newton1 = fopen("teor_error_Newton1.txt", "w"); FILE* result_teor_error_Newton2 = fopen("teor_error_Newton2.txt", "w"); FILE* result_teor_error_Newton_Cheb1 = fopen("teor_error_Newton_cheb1.txt", "w"); FILE* result_teor_error_Newton_Cheb2 = fopen("teor_error_Newton_cheb2.txt", "w"); for (double i = a1; i <= b1; i += 0.01) { double compute_Newton = Newton(i, n, values1, points1); double compute_Newton_Cheb = Newton(i, n, values_Cheb1, points_Cheb1); fprintf(result_Newton1, "%.10lf\n", compute_Newton); fprintf(result_Newton_Cheb1, "%.10lf\n", compute_Newton_Cheb); fprintf(result_error_Newton1, "%.10lf\n", fabs(compute_Newton - function1(i))); fprintf(result_error_Newton_Cheb1, "%.10lf\n", fabs(compute_Newton_Cheb - function1(i))); fprintf(result_teor_error_Newton1, "%.10lf\n", fabs(TeorError(i, n, points1, values1, function1))); fprintf(result_teor_error_Newton_Cheb1, "%.10lf\n", fabs(TeorError(i, n, points_Cheb1, values_Cheb1, function1))); } for (double i = a2; i <= b2; i += 0.01) { double compute_Newton = Newton(i, n, values2, points2); double compute_Newton_Cheb = Newton(i, n, values_Cheb2, points_Cheb2); fprintf(result_Newton2, "%.10lf\n", compute_Newton); fprintf(result_Newton_Cheb2, "%.10lf\n", compute_Newton_Cheb); fprintf(result_error_Newton2, "%.10lf\n", fabs(compute_Newton - function2(i))); fprintf(result_error_Newton_Cheb2, "%.10lf\n", fabs(compute_Newton_Cheb - function2(i))); fprintf(result_teor_error_Newton2, "%.10lf\n", fabs(TeorError(i, n, points2, values2, function2))); fprintf(result_teor_error_Newton_Cheb2, "%.10lf\n", fabs(TeorError(i, n, points_Cheb2, values_Cheb2, function2))); } _fcloseall(); free(points_Cheb1); free(points_Cheb2); free(points1); free(points2); free(values1); free(values2); free(values_Cheb1); free(values_Cheb2); }*/ // ����������� ������ ������������ �� ������� ����������������� �������� /*void launche2() { double a1 = -1.5; double b1 = 2; double a2 = -1; double b2 = 1; FILE* result_errorN_Newton1 = fopen("errorN_Newton1.txt", "w"); FILE* result_errorN_Newton2 = fopen("errorN_Newton2.txt", "w"); FILE* result_errorN_Newton_Cheb1 = fopen("errorN_Newton_cheb1.txt", "w"); FILE* result_errorN_Newton_Cheb2 = fopen("errorN_Newton_cheb2.txt", "w"); for (int n = 5; n <= 100; n += 1) { double max_error1 = 0; double max_error2 = 0; double max_error_Cheb1 = 0; double max_error_Cheb2 = 0; double* points1 = CreateUniMesh(n, a1, b1); double* values1 = values(points1, n, function1); double* points_Cheb1 = CreateChebMesh(n, a1, b1); double* values_Cheb1 = values(points_Cheb1, n, function1); for (double i = a1; i <= b1; i += 0.01) { double d1 = fabs(function1(i) - Newton(i, n, values1, points1)); if (d1 >= max_error1) max_error1 = d1; double d2 = fabs(function1(i) - Newton(i, n, values_Cheb1, points_Cheb1)); if (d2 >= max_error_Cheb1) max_error_Cheb1 = d2; } double* points2 = CreateUniMesh(n, a2, b2); double* values2 = values(points2, n, function2); double* points_Cheb2 = CreateChebMesh(n, a2, b2); double* values_Cheb2 = values(points_Cheb2, n, function2); for (double i = a2; i <= b2; i += 0.01) { double d1 = fabs(function2(i) - Newton(i, n, values2, points2)); if (d1 >= max_error2) max_error2 = d1; double d2 = fabs(function2(i) - Newton(i, n, values_Cheb2, points_Cheb2)); if (d2 >= max_error_Cheb2) max_error_Cheb2 = d2; } fprintf(result_errorN_Newton1, "%.15lf\n", max_error1); fprintf(result_errorN_Newton2, "%.15lf\n", max_error2); fprintf(result_errorN_Newton_Cheb1, "%.15lf\n", max_error_Cheb1); fprintf(result_errorN_Newton_Cheb2, "%.15lf\n", max_error_Cheb2); free(points_Cheb1); free(points_Cheb2); free(points1); free(points2); free(values1); free(values2); free(values_Cheb1); free(values_Cheb2); } _fcloseall(); }*/ // � ��������� ������ void launche3() { double a1 = -1.5; double b1 = 2; double a2 = -1; double b2 = 1; double point1 = -0.73; double point2 = 0.48; FILE* result_error_point1_Newton1 = fopen("error_point1_Newton1.txt", "w"); FILE* result_error_point1_Newton2 = fopen("error_point1_Newton2.txt", "w"); FILE* result_error_point1_Newton_Cheb1 = fopen("error_point1_Newton_cheb1.txt", "w"); FILE* result_error_point1_Newton_Cheb2 = fopen("error_point1_Newton_cheb2.txt", "w"); FILE* result_error_point2_Newton1 = fopen("error_point2_Newton1.txt", "w"); FILE* result_error_point2_Newton2 = fopen("error_point2_Newton2.txt", "w"); FILE* result_error_point2_Newton_Cheb1 = fopen("error_point2_Newton_cheb1.txt", "w"); FILE* result_error_point2_Newton_Cheb2 = fopen("error_point2_Newton_cheb2.txt", "w"); for (int n = 5; n <= 100; n += 1) { double* points1 = CreateUniMesh(n, a1, b1); double* values1 = values(points1, n, function1); double* points_Cheb1 = CreateChebMesh(n, a1, b1); double* values_Cheb1 = values(points_Cheb1, n, function1); double* points2 = CreateUniMesh(n, a2, b2); double* values2 = values(points2, n, function2); double* points_Cheb2 = CreateChebMesh(n, a2, b2); double* values_Cheb2 = values(points_Cheb2, n, function2); fprintf(result_error_point1_Newton1, "%.15lf\n", fabs(function1(point1) - Newton(point1, n, values1, points1))); fprintf(result_error_point1_Newton_Cheb1, "%.15lf\n", fabs(function1(point1) - Newton(point1, n, values_Cheb1, points_Cheb1))); fprintf(result_error_point1_Newton2, "%.15lf\n", fabs(function2(point1) - Newton(point1, n, values2, points2))); fprintf(result_error_point1_Newton_Cheb2, "%.15lf\n", fabs(function2(point1) - Newton(point1, n, values_Cheb2, points_Cheb2))); fprintf(result_error_point2_Newton1, "%.15lf\n", fabs(function1(point2) - Newton(point2, n, values1, points1))); fprintf(result_error_point2_Newton_Cheb1, "%.15lf\n", fabs(function1(point2) - Newton(point2, n, values_Cheb1, points_Cheb1))); fprintf(result_error_point2_Newton2, "%.15lf\n", fabs(function2(point2) - Newton(point2, n, values2, points2))); fprintf(result_error_point2_Newton_Cheb2, "%.15lf\n", fabs(function2(point2) - Newton(point2, n, values_Cheb2, points_Cheb2))); free(points_Cheb1); free(points_Cheb2); free(points1); free(points2); free(values1); free(values2); free(values_Cheb1); free(values_Cheb2); } _fcloseall(); } void main() { //launche1(); //launche2(); launche3(); }