221 lines
5.9 KiB
Plaintext
221 lines
5.9 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "fda98006",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Solution of second-order linear ordinary differential equation"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "7fced95c",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"def write_to_file(name: str, array):\n",
|
||
" with open(f\"{name}\", 'w') as file:\n",
|
||
" for element in array:\n",
|
||
" file.write(f\"{element} \")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "d20bde93",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"import matplotlib.pyplot as plt"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "8af07a56",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Solution of $5u'' + 4u' + 1 = 0, u'(0) = u(0), u(10) = 5$"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "d0fe0703",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"def u(x):\n",
|
||
" return (9 * np.exp(8) * (x - 30) + 155 * np.exp(8 - 4 * x / 5) - 5 * (x + 1)) / (20 - 36 * np.exp(8))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "efac514a",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Linear element"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "18bd929b",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"def show_plot_linear(filename: str, elements: int):\n",
|
||
" with open(filename, 'r') as file:\n",
|
||
" data_str = file.read()\n",
|
||
"\n",
|
||
" x = np.linspace(0, 10, elements + 1)\n",
|
||
" y = np.fromstring(data_str, sep=' ')\n",
|
||
" y_real = u(x)\n",
|
||
"\n",
|
||
" plt.plot(x, y, label=\"Linear element solution\", color='red')\n",
|
||
" plt.plot(x, y_real, label=\"Numeral solution\", color='black')\n",
|
||
" plt.title(f\"Linear element, elements = {elements}\")\n",
|
||
" plt.grid(True)\n",
|
||
" plt.xlabel(\"X\")\n",
|
||
" plt.ylabel(\"Y\")\n",
|
||
" plt.legend()\n",
|
||
" plt.savefig(f\"linear_{elements}.png\", dpi=300)\n",
|
||
" plt.show()\n",
|
||
"\n",
|
||
" write_to_file(f\"linear_real_y_{elements}.txt\", y_real)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d9a38740",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Cubic element"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "73525bc7",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"def show_plot_cubic(filename: str, elements: int):\n",
|
||
" with open(filename, 'r') as file:\n",
|
||
" data_str = file.read()\n",
|
||
"\n",
|
||
" x = np.linspace(0, 10, 3 * elements + 1)\n",
|
||
" y = np.fromstring(data_str, sep=' ')\n",
|
||
" y_real = u(x)\n",
|
||
"\n",
|
||
" plt.plot(x, y, label=\"Cubic element solution\", color=\"green\")\n",
|
||
" plt.plot(x, y_real, label=\"Numeral solution\", color='black')\n",
|
||
" plt.title(f\"Cubic element, elements = {elements} \")\n",
|
||
" plt.xlabel(\"X\")\n",
|
||
" plt.ylabel(\"Y\")\n",
|
||
" plt.grid(True)\n",
|
||
" plt.legend()\n",
|
||
" plt.savefig(f\"cubic_{elements}.png\", dpi=300)\n",
|
||
" plt.show()\n",
|
||
"\n",
|
||
" write_to_file(f\"cubic_real_y_{elements}.txt\", y_real)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "3584baa4",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Main part"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "b8a772e3",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"N = 20\n",
|
||
"show_plot_linear(\"matrix_linear.txt\", N)\n",
|
||
"show_plot_cubic(\"matrix_cubic.txt\", N)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "22fe3de6",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Errors"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "e0e9266d",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"def read_data_from_file(filename):\n",
|
||
" with open(filename, 'r') as file:\n",
|
||
" content = file.read().strip()\n",
|
||
" data = np.array([float(x) for x in content.split()])\n",
|
||
" return data\n",
|
||
"\n",
|
||
"def show_error(program_file: str, analytical_file: str):\n",
|
||
" # Считывание данных из файлов\n",
|
||
" real = read_data_from_file(analytical_file)\n",
|
||
" matrix = read_data_from_file(program_file)\n",
|
||
" \n",
|
||
" absolute_errors = np.abs(real - matrix)\n",
|
||
" relative_errors = absolute_errors / (np.abs(real))\n",
|
||
" relative_errors_percent = relative_errors * 100\n",
|
||
"\n",
|
||
" max_relative_error = np.max(relative_errors)\n",
|
||
" max_relative_error_percent = np.max(relative_errors_percent)\n",
|
||
" max_error_index = np.argmax(relative_errors)\n",
|
||
"\n",
|
||
" print(f\"\\nМаксимальная относительная погрешность: {max_relative_error:.6f} ({max_relative_error_percent:.2f}%)\")\n",
|
||
" print(f\"Индекс точки с максимальной погрешностью: {max_error_index}\")\n",
|
||
" print(f\"Значения в точке с максимальной погрешностью:\")\n",
|
||
" print(f\" cubic_real: {real[max_error_index]}\")\n",
|
||
" print(f\" matrix_cubic: {matrix[max_error_index]}\")\n",
|
||
" print(f\" Абсолютная разность: {absolute_errors[max_error_index]:.6f}\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "89918171",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"show_error(\"matrix_linear.txt\", \"linear_real_y_20.txt\")"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": ".venv",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.12.3"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|