update: enhance ComputerSession2 and TP4_Ridge_Lasso_and_CV notebooks with execution outputs and code improvements

This commit is contained in:
2025-03-30 20:43:00 +02:00
parent 44c277c8a7
commit 786ebadefc
2 changed files with 675 additions and 655 deletions

View File

@@ -1,8 +1,8 @@
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Computer session 2\n",
"\n",
@@ -30,13 +30,23 @@
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-18T16:19:14.314484Z",
"start_time": "2025-03-18T16:19:13.728014Z"
}
},
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(-2.3283064365386963e-09, 31)\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
@@ -57,21 +67,11 @@
"f = lambda x: np.tanh(x)\n",
"aL, aR = -20, 3\n",
"print(dichotomy(f, aL, aR))"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(-2.3283064365386963e-09, 31)\n"
]
}
],
"execution_count": 1
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solving one-dimensional equation with the Newton and the secant method\n",
"\n",
@@ -93,13 +93,31 @@
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-18T16:19:17.447647Z",
"start_time": "2025-03-18T16:19:17.442560Z"
}
},
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(inf, 'Method diverges')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_25957/3868809151.py:14: RuntimeWarning: overflow encountered in exp\n",
" f = lambda x: np.log(np.exp(x) + np.exp(-x))\n"
]
}
],
"source": [
"def Newton(phi, dphi, x0, eps=1e-10):\n",
" iter = 0\n",
@@ -118,29 +136,11 @@
"x0 = 1.8\n",
"df = lambda x: (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n",
"print(Newton(f, df, x0))"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(inf, 'Method diverges')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_25957/3868809151.py:14: RuntimeWarning: overflow encountered in exp\n",
" f = lambda x: np.log(np.exp(x) + np.exp(-x))\n"
]
}
],
"execution_count": 2
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Secant method\n",
"\n",
@@ -153,13 +153,25 @@
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-18T16:19:19.649523Z",
"start_time": "2025-03-18T16:19:19.456149Z"
}
},
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(inf, 'Method diverges')\n",
"(inf, 'Method diverges')\n",
"(inf, 'Method diverges')\n"
]
}
],
"source": [
"def Secant(phi, x0, x1, eps=1e-8):\n",
" iter = 0\n",
@@ -176,23 +188,11 @@
"\n",
"for x0, x1 in xx:\n",
" print(Secant(f, x0, x1))"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(inf, 'Method diverges')\n",
"(inf, 'Method diverges')\n",
"(inf, 'Method diverges')\n"
]
}
],
"execution_count": 3
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Combining dichotomy and the Newton method\n",
"\n",
@@ -211,13 +211,31 @@
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-18T16:44:41.592150Z",
"start_time": "2025-03-18T16:44:41.584318Z"
}
},
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(inf, 'Method diverges')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_25957/1578277506.py:23: RuntimeWarning: overflow encountered in exp\n",
" f = lambda x: np.log(np.exp(x) + np.exp(-x))\n"
]
}
],
"source": [
"def DichotomyNewton(phi, dphi, aL, aR, s=0.1, eps=1e-10):\n",
" iter = 0\n",
@@ -244,29 +262,11 @@
"f = lambda x: np.log(np.exp(x) + np.exp(-x))\n",
"df = lambda x: (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))\n",
"print(DichotomyNewton(f, df, -20, 3))"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(inf, 'Method diverges')\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/tp/_ld5_pzs6nx6mv1pbjhq1l740000gn/T/ipykernel_25957/1578277506.py:23: RuntimeWarning: overflow encountered in exp\n",
" f = lambda x: np.log(np.exp(x) + np.exp(-x))\n"
]
}
],
"execution_count": 22
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solving an optimisation problem using the Newton method\n",
"\n",
@@ -285,13 +285,28 @@
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-18T17:43:43.061916Z",
"start_time": "2025-03-18T17:43:43.042625Z"
}
},
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimal point (Newton): 0.9299901531755377\n",
"Objective function value at optimal point (Newton): 1.9329918821224974\n",
"Number of iterations (Newton): 100\n",
"Optimal point (Dichotomy): inf\n",
"Objective function value at optimal point (Dichotomy): inf\n",
"Number of iterations (Dichotomy): Method diverges\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
@@ -360,26 +375,11 @@
"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 point (Newton): 0.9299901531755377\n",
"Objective function value at optimal point (Newton): 1.9329918821224974\n",
"Number of iterations (Newton): 100\n",
"Optimal point (Dichotomy): inf\n",
"Objective function value at optimal point (Dichotomy): inf\n",
"Number of iterations (Dichotomy): Method diverges\n"
]
}
],
"execution_count": 38
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Newton method to solve boundary value problems\n",
"\n",
@@ -493,16 +493,16 @@
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"The result is a system of equations for $\\mathbf{y}=(y_1,\\dots,y_{N-1})$ :\n",
"$$G(\\mathbf{y})=0,\\quad G:\\mathbb{R}^{N-1}\\to\\mathbb{R}^{N-1}.$$"
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"This system can be solved using Newton's method. Note that the Jacobian\n",
"$\\partial G/\\partial \\mathbb{y}$ is _tridiagonal_.\n",
@@ -522,8 +522,8 @@
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**Remark:** In the context of numerical optimal control, these two numerical\n",
"methods are often called _indirect method_ (for the shooting method) and _direct\n",
@@ -531,8 +531,8 @@
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Whos the best?\n",
"\n",
@@ -541,9 +541,9 @@
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": ""
"metadata": {},
"source": []
},
{
"cell_type": "code",
@@ -560,16 +560,18 @@
"source": []
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "\n"
"metadata": {},
"outputs": [],
"source": [
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
@@ -583,7 +585,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.21"
"version": "3.12.2"
}
},
"nbformat": 4,

File diff suppressed because one or more lines are too long