/
levg
/
numm
Обзор
Документация
Войти
/
levg
/
numm
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
8/code/graph2.py
81 строка
3 KB
Лев Черняховский
s
29 окт 2024, 10:38
29 окт 2024, 10:38
d9048cd
Код
Авторство
О чём код?
import numpy as np import matplotlib.pyplot as plt import math def load_data(filename): data = np.loadtxt('C:/Users/Aser/source/repos/Chisl2sem1homework/'+filename) return data[:, 0], data[:, 1], data[:, 2], data[:, 3] def compute_theoretical_error(x, nodes_x, max_derivative): n = len(nodes_x) - 1 theoretical_error = np.zeros_like(x) for i in range(len(x)): product = 1.0 for node in nodes_x: if (x[i] - node)!=0: product *= (x[i] - node) if product == 0: print(f"Внимание: Произведение равно 0 для x[{i}]={x[i]}") theoretical_error[i] = abs(product * 10**130 / np.math.factorial(n + 1)) return theoretical_error def plot_functions_and_interpolations(interp_files, labels, title): plt.figure(figsize=(10, 6)) for i, interp_file in enumerate(interp_files): x, f_value, p_value, error = load_data(interp_file) if i == 0: plt.plot(x, f_value, label='Функция', color='black', linewidth=2) plt.plot(x, p_value, label=labels[i]) plt.title(title) plt.xlabel('x') plt.ylabel('y') plt.legend() plt.grid(True) plt.show() def plot_errors(interp_files, labels, title, max_derivative): plt.figure(figsize=(10, 6)) for i, interp_file in enumerate(interp_files): x, f_value, p_value, error = load_data(interp_file) nodes_x = np.unique(x) plt.plot(x, error, label=f'Ошибка: {labels[i]}') theoretical_error = compute_theoretical_error(x, nodes_x, max_derivative) print(theoretical_error) if i==0: plt.plot(x, theoretical_error, label=f'Теоретическая ошибка {labels[i]}', linestyle=':') plt.title(title) plt.xlabel('x') plt.ylabel('Ошибка') plt.legend() plt.grid(True) plt.show() nfunc = 2 interp_files1_uniform = [f'error_uniform_func{nfunc}_5.txt', f'error_uniform_func{nfunc}_6.txt', f'error_uniform_func{nfunc}_7.txt'] interp_files1_chebyshev = [f'error_chebyshev_func{nfunc}_5.txt', f'error_chebyshev_func{nfunc}_6.txt', f'error_chebyshev_func{nfunc}_7.txt'] max_derivative_uniform = 8 max_derivative_chebyshev =8 labels_uniform = [ 'Полином 5 узлов', 'Полином 6 узлов', 'Полином 7 узлов' ] plot_errors(interp_files1_uniform, labels_uniform, f"Ошибка интерполяции для функции {nfunc} (Равномерная сетка)", max_derivative_uniform) plot_errors(interp_files1_chebyshev, labels_uniform, f"Ошибка интерполяции для функции {nfunc} (Чебышевская сетка)", max_derivative_chebyshev)