diff --git a/exp-345.ipynb b/exp-345.ipynb index 45ed46107fbb32abb9b35c77f3d033533c75ef1b..3cc7714637173a79fc658618c3ac1b063b008ad6 100644 --- a/exp-345.ipynb +++ b/exp-345.ipynb @@ -13,7 +13,8 @@ "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", - "from IPython.display import set_matplotlib_formats, display, Math\n", + "from matplotlib_inline.backend_inline import set_matplotlib_formats\n", + "from IPython.display import display, Math\n", "set_matplotlib_formats('svg')\n", "\n", "\n", @@ -25,18 +26,17 @@ "n = 80\n", "h = 1/n\n", "\n", - "display(Math(r'\\text{Create }A\\text{ from Example 1.7.1, }\\xi_j=\\begin{cases}2&1\\leq j\\leq40\\\\1&41\\leq j\\leq80\\end{cases},\\ y=A\\xi'))\n", - "A = np.zeros((n, n))\n", - "for i in range(n):\n", - " for j in range(n):\n", - " A[i, j] = h * k((i+0.5)*h, (j+0.5)*h, d)\n", + "display(Math(r'\\text{Create }A\\text{ from Example 1.5.1, }\\xi_j=\\begin{cases}2&1\\leq j\\leq40\\\\1&41\\leq j\\leq80\\end{cases},\\ y=A\\xi'))\n", + "s = (np.arange(n) + 0.5) * h\n", + "t = (np.arange(n) + 0.5) * h\n", + "A = h * k(s[:, np.newaxis], t[np.newaxis, :], d)\n", "\n", "xi = np.ones(n)\n", "xi[:n//2] = 2\n", "y = np.matmul(A, xi)\n", "\n", "j = np.arange(1, n+1)\n", - "plt.title(r\"$xi$\")\n", + "plt.title(r\"$\\xi$\")\n", "plt.plot(j, xi, 'kx')\n", "plt.show()\n", "plt.close()\n", @@ -55,7 +55,7 @@ "display(Math(r'\\tilde y= y+\\delta y'))\n", "np.random.seed(0)\n", "y_tilde = y + 0.02*(np.random.rand(n)-0.5)\n", - "print('||y_tilde-y||/||y|| = {:.4f}'.format(np.linalg.norm(y_tilde-y)/np.linalg.norm(y)))\n", + "display(Math(r\"\\frac{\\|\\tilde y-y\\|_2}{\\|y\\|_2} = \" + f\"{np.linalg.norm(y_tilde-y)/np.linalg.norm(y):.4f}\"))\n", "\n", "plt.title(r\"$\\tilde \\xi = $numpy.linalg.solve$(A, \\tilde y)$\")\n", "plt.plot(j, np.linalg.solve(A, y_tilde), 'k+')\n", @@ -73,13 +73,13 @@ "alphas = np.zeros(100)\n", "for i in range(1, 101):\n", " alpha = 0.005*i\n", - " A_Tikh = np.concatenate((A, np.diag(alpha*np.ones(n))), axis=0)\n", + " A_Tikh = np.concatenate((A, alpha*np.eye(n)), axis=0)\n", " y_tilde_Tikh = np.concatenate((y_tilde, np.zeros(n)), axis=0)\n", " xi_alpha = np.linalg.lstsq(A_Tikh, y_tilde_Tikh, rcond=0)[0]\n", " alphas[i-1] = alpha\n", " error[i-1] = np.linalg.norm(xi_alpha-xi)\n", " if i == 1 or i == 8 or i == 100:\n", - " plt.title(r\"Tikhonov, $\\alpha$ = {}\".format(alpha))\n", + " plt.title(rf\"Tikhonov, $\\alpha$ = {alpha}\")\n", " plt.plot(j, xi_alpha, 'kx')\n", " plt.show()\n", " plt.close()\n", @@ -90,7 +90,7 @@ "plt.plot(alphas, error, 'k+')\n", "plt.show()\n", "plt.close()\n", - "print('alpha_opt = {}'.format(alphas[np.argmin(error)]))" + "display(Math(r'\\alpha_\\text{opt} = ' + f'{alphas[np.argmin(error)]}'))" ], "outputs": [], "execution_count": null @@ -119,7 +119,7 @@ " p_norm = p_norm_new\n", " d = p + gamma*d\n", " if k == 5 or k == 40 or k == 200:\n", - " plt.title(r\"CGNE, $k$ = {}\".format(k))\n", + " plt.title(rf\"CGNE, $k$ = {k}\")\n", " plt.plot(j, xi_tilde, 'kx')\n", " plt.show()\n", " plt.close()\n", @@ -131,10 +131,17 @@ "plt.plot(error, 'k+')\n", "plt.show()\n", "plt.close()\n", - "print('k_opt = {}'.format(np.argmin(error)))\n" + "display(Math(r'k_\\text{opt} = ' + f'{np.argmin(error)}'))" ], "outputs": [], "execution_count": null + }, + { + "cell_type": "code", + "metadata": {}, + "source": [], + "outputs": [], + "execution_count": null } ], "metadata": { @@ -158,5 +165,5 @@ } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 } \ No newline at end of file