{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from typing import Callable" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "precision = 3\n", "eps = 1 / precision\n", "\n", "def dichotomy(a: float, b: float, func: Callable[[float], float]) -> float:\n", " while True:\n", " x = (a + b) / 2\n", "\n", " x1 = x - eps / 2\n", " f1 = func(x1)\n", "\n", " x2 = x + eps / 2\n", " f2 = func(x2)\n", "\n", " if f1 > f2:\n", " a = x1\n", " else:\n", " b = x2\n", " \n", " if abs(a - b) <= 2 * eps:\n", " break\n", " return round(func((a + b) / 2), precision)\n", "\n", "\n", "def fibonacci(a: float, b: float, func: Callable[[float], float]) -> float:\n", " n: int = 1000\n", " fibonacciSequence = [1, 1]\n", " \n", " for i in range(1, n - 1):\n", " fibonacciSequence.append(fibonacciSequence[i - 1] + fibonacciSequence[i])\n", " \n", " L = b - a\n", " while n > 2:\n", " x1 = a + L * fibonacciSequence[n - 2] / fibonacciSequence[n - 1]\n", " x2 = b - L * fibonacciSequence[n - 2] / fibonacciSequence[n - 1]\n", "\n", " f1 = func(x1)\n", " f2 = func(x2)\n", "\n", " if f1 > f2:\n", " b = x1\n", " f1 = f2\n", " x1 = x2\n", " L = b - a\n", " x2 = a + (b - x1)\n", " f2 = func(x2)\n", " else:\n", " a = x2\n", " f2 = f1\n", " x2 = x1\n", " L = b - a\n", " x1 = b - (x2 - a)\n", " f1 = func(x1)\n", " \n", " n -= 1\n", " return round(min(f1, f2), precision)\n", "\n", "\n", "def goldenRatio(a: float, b: float, func: Callable[[float], float]) -> float:\n", " ratio = 0.618\n", " while True:\n", " L: float = abs(a - b)\n", "\n", " x1 = a + L * ratio\n", " x2 = b - L * ratio\n", "\n", " f1 = func(x1)\n", " f2 = func(x2)\n", "\n", " if f1 > f2:\n", " b = x1\n", " f1 = f2\n", " x1 = x2\n", " L = b - a\n", " x2 = a + (b - x1)\n", " f2 = func(x2)\n", " else:\n", " a = x2\n", " f2 = f1\n", " x2 = x1\n", " L = b - a\n", " x1 = b - (x2 - a)\n", " f1 = func(x1)\n", " \n", " if L <= eps:\n", " break\n", " return round(min(f1, f2), precision)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Исходные данные:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "func = lambda x: (x - 15)**2 + 5\n", "a = 12\n", "b = 20" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Метод дихотомии:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0003370667" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dichotomy(a, b, func)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Метод фибоначчи:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fibonacci(a, b, func)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Метод золотого сечения:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0000883205" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "goldenRatio(a, b, func)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10.4" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "d68a9a56d9a25e4bd7befbd53d862537883bbf75717698e3f2bd31fa571ddf98" } } }, "nbformat": 4, "nbformat_minor": 2 }