forked from ParkSuMin/MemMAPR-MKE
5.9 KiB
5.9 KiB
In [ ]:
def write_to_file(name: str, array):
with open(f"{name}", 'w') as file:
for element in array:
file.write(f"{element} ")In [ ]:
import numpy as np
import matplotlib.pyplot as pltIn [ ]:
def u(x):
return (9 * np.exp(8) * (x - 30) + 155 * np.exp(8 - 4 * x / 5) - 5 * (x + 1)) / (20 - 36 * np.exp(8))In [ ]:
def show_plot_linear(filename: str, elements: int):
with open(filename, 'r') as file:
data_str = file.read()
x = np.linspace(0, 10, elements + 1)
y = np.fromstring(data_str, sep=' ')
y_real = u(x)
plt.plot(x, y, label="Linear element solution", color='red')
plt.plot(x, y_real, label="Numeral solution", color='black')
plt.title(f"Linear element, elements = {elements}")
plt.grid(True)
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.savefig(f"linear_{elements}.png", dpi=300)
plt.show()
write_to_file(f"linear_real_y_{elements}.txt", y_real)In [ ]:
def show_plot_cubic(filename: str, elements: int):
with open(filename, 'r') as file:
data_str = file.read()
x = np.linspace(0, 10, 3 * elements + 1)
y = np.fromstring(data_str, sep=' ')
y_real = u(x)
plt.plot(x, y, label="Cubic element solution", color="green")
plt.plot(x, y_real, label="Numeral solution", color='black')
plt.title(f"Cubic element, elements = {elements} ")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True)
plt.legend()
plt.savefig(f"cubic_{elements}.png", dpi=300)
plt.show()
write_to_file(f"cubic_real_y_{elements}.txt", y_real)In [ ]:
N = 20
show_plot_linear("matrix_linear.txt", N)
show_plot_cubic("matrix_cubic.txt", N)In [ ]:
def read_data_from_file(filename):
with open(filename, 'r') as file:
content = file.read().strip()
data = np.array([float(x) for x in content.split()])
return data
def show_error(program_file: str, analytical_file: str):
# Считывание данных из файлов
real = read_data_from_file(analytical_file)
matrix = read_data_from_file(program_file)
absolute_errors = np.abs(real - matrix)
relative_errors = absolute_errors / (np.abs(real))
relative_errors_percent = relative_errors * 100
max_relative_error = np.max(relative_errors)
max_relative_error_percent = np.max(relative_errors_percent)
max_error_index = np.argmax(relative_errors)
print(f"\nМаксимальная относительная погрешность: {max_relative_error:.6f} ({max_relative_error_percent:.2f}%)")
print(f"Индекс точки с максимальной погрешностью: {max_error_index}")
print(f"Значения в точке с максимальной погрешностью:")
print(f" cubic_real: {real[max_error_index]}")
print(f" matrix_cubic: {matrix[max_error_index]}")
print(f" Абсолютная разность: {absolute_errors[max_error_index]:.6f}")In [ ]:
show_error("matrix_linear.txt", "linear_real_y_20.txt")