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 0b4ee0585d
commit adb50052b8
2 changed files with 675 additions and 655 deletions

View File

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

File diff suppressed because one or more lines are too long