mirror of
https://github.com/ArthurDanjou/ArtStudies.git
synced 2026-01-14 13:54:06 +01:00
TP2 in Numerical Opti
This commit is contained in:
@@ -288,44 +288,90 @@
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-02-18T17:42:16.515141Z",
|
||||
"start_time": "2025-02-18T17:42:16.461572Z"
|
||||
"end_time": "2025-02-25T21:29:40.198527Z",
|
||||
"start_time": "2025-02-25T21:29:40.188272Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"def total_time(x):\n",
|
||||
" # t = d / v\n",
|
||||
" run_speed = 8\n",
|
||||
" swim_speed = 3\n",
|
||||
" distance_run = x\n",
|
||||
" distance_swim = np.sqrt((6 - x) ** 2 + 2 ** 2)\n",
|
||||
" return distance_run / run_speed + distance_swim / swim_speed\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def d_total_time(x):\n",
|
||||
" run_speed = 8\n",
|
||||
" swim_speed = 3\n",
|
||||
" distance_swim = np.sqrt((6 - x) ** 2 + 2 ** 2)\n",
|
||||
" return 1 / run_speed - (6 - x) / (swim_speed * distance_swim)\n",
|
||||
"# Objective function\n",
|
||||
"def objective_function(x):\n",
|
||||
" return x / 8 + np.sqrt((6 - x) ** 2 + 4) / 3\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"x0 = 6.0\n",
|
||||
"optimal_x, iterations = Newton(total_time, d_total_time, x0)\n",
|
||||
"print(f\"Optimal distance to run: {optimal_x} km\")\n",
|
||||
"print(f\"Number of iterations: {iterations}\")"
|
||||
"# Gradient of the objective function\n",
|
||||
"def gradient(x):\n",
|
||||
" return 1 / 8 + (6 - x) / (3 * np.sqrt((6 - x) ** 2 + 4))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Hessian of the objective function\n",
|
||||
"def hessian(x):\n",
|
||||
" return -((6 - x) ** 2 + 4) / (3 * ((6 - x) ** 2 + 4) ** (3 / 2))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Newton's method for optimization\n",
|
||||
"def newton_method(initial_guess, tolerance=1e-6, max_iterations=100):\n",
|
||||
" x = initial_guess\n",
|
||||
" iterations = 0\n",
|
||||
" while iterations < max_iterations:\n",
|
||||
" grad = gradient(x)\n",
|
||||
" hess = hessian(x)\n",
|
||||
" if np.abs(grad) < tolerance:\n",
|
||||
" break\n",
|
||||
" x = x - grad / hess\n",
|
||||
" iterations += 1\n",
|
||||
" return x, iterations\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Dichotomy method for optimization\n",
|
||||
"def dichotomy_method(func, aL, aR, eps=1e-6, max_iterations=100):\n",
|
||||
" iterations = 0\n",
|
||||
" while (aR - aL) / 2 > eps and iterations < max_iterations:\n",
|
||||
" b = (aL + aR) / 2\n",
|
||||
" if func(aL) * func(b) < 0:\n",
|
||||
" aR = b\n",
|
||||
" else:\n",
|
||||
" aL = b\n",
|
||||
" iterations += 1\n",
|
||||
" return (aL + aR) / 2, iterations\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Initial guess for Newton's method\n",
|
||||
"initial_guess_newton = 3\n",
|
||||
"\n",
|
||||
"# Run Newton's method\n",
|
||||
"optimal_point_newton, iterations_newton = newton_method(initial_guess_newton)\n",
|
||||
"print(f\"Optimal point (Newton): {optimal_point_newton}\")\n",
|
||||
"print(f\"Objective function value at optimal point (Newton): {objective_function(optimal_point_newton)}\")\n",
|
||||
"print(f\"Number of iterations (Newton): {iterations_newton}\")\n",
|
||||
"\n",
|
||||
"# Initial interval for dichotomy method\n",
|
||||
"aL, aR = 0, 6\n",
|
||||
"\n",
|
||||
"# Run dichotomy method\n",
|
||||
"optimal_point_dichotomy, iterations_dichotomy = dichotomy_method(objective_function, aL, aR)\n",
|
||||
"print(f\"Optimal point (Dichotomy): {optimal_point_dichotomy}\")\n",
|
||||
"print(f\"Objective function value at optimal point (Dichotomy): {objective_function(optimal_point_dichotomy)}\")\n",
|
||||
"print(f\"Number of iterations (Dichotomy): {iterations_dichotomy}\")"
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Optimal distance to run: inf km\n",
|
||||
"Number of iterations: Method diverges\n"
|
||||
"Optimal point (Newton): 6.809045443967482\n",
|
||||
"Objective function value at optimal point (Newton): 1.5702779015852644\n",
|
||||
"Number of iterations (Newton): 7\n",
|
||||
"Optimal point (Dichotomy): 5.999999284744263\n",
|
||||
"Objective function value at optimal point (Dichotomy): 1.416666577259742\n",
|
||||
"Number of iterations (Dichotomy): 22\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 104
|
||||
"execution_count": 14
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
|
||||
Reference in New Issue
Block a user