{ "cells": [ { "cell_type": "markdown", "id": "1f57a06f-9b32-4663-abee-4d0f0d639740", "metadata": {}, "source": [ "\n", "# Basel problem and Richardson extrapolation" ] }, { "cell_type": "markdown", "id": "0c3d02c2-70ab-40e5-8c7f-e5c2fc342599", "metadata": {}, "source": [ "\n", "The Basel problem asks for the precise summation of the following infinite series:\n", "$$\n", " S = \\sum_{n=1}^{\\infty }\\frac{1}{n^2} = \\frac{1}{1^2} + \\frac{1}{2^2} + \\frac{1}{3^2} + \\cdots .\n", "$$\n", "The problem was posed in 1650 and solved by Leonhard Euler in\n", "1734. Euler found the exact sum to be $\\pi^{2}/6$.\n", "Since the problem had withstood the attacks of the leading\n", "mathematicians of the day, Euler's solution brought him immediate\n", "fame. The problem is named after Basel, hometown of Euler." ] }, { "cell_type": "markdown", "id": "f359e073-67e3-4018-8ca0-75ec02cff727", "metadata": {}, "source": [ "\n", "The Basel series converges very slowly: direct summation of the first 100 terms (which is close to the limit that people in the 17th century could do by hand) gives an absolute error of about 0.01. Your task is to evaluate the sum with an absolute error of about 0.0001 (i.e. 100 times smaller) by using Richardson extrapolation, while still summing not more than 100 terms in the series." ] }, { "cell_type": "code", "execution_count": null, "id": "cf9ee858-d021-4f8d-bae7-90afcde950aa", "metadata": {}, "outputs": [], "source": [ "\n", "\"\"\"\n", " res = mybasel(n)\n", "\n", "Your description of the function and the parameter(s) goes here\n", "\"\"\"\n", "function mybasel(n)\n", " s = 0.0\n", " for i = 1:n\n", " s += 1/i^2\n", " end\n", " return s\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "4629377d-9a4e-4d90-a158-9f14243dd08a", "metadata": {}, "outputs": [], "source": [ "\n", "?mybasel" ] }, { "cell_type": "code", "execution_count": null, "id": "19251dc7-7661-4a59-a73f-0a2a62e1e7b9", "metadata": {}, "outputs": [], "source": [ "\n", "exact = pi^2/6" ] }, { "cell_type": "markdown", "id": "461cd3da-ec22-4857-9c45-b8c7e49f42a2", "metadata": {}, "source": [ "### Leading error term of the finite Basel sum" ] }, { "cell_type": "code", "execution_count": null, "id": "91b72a6c-4d47-4f18-b477-c4a3a9acda9c", "metadata": {}, "outputs": [], "source": [ "\n", "ndp = 10\n", "err = zeros(ndp);\n", "ns = zeros(ndp);" ] }, { "cell_type": "code", "execution_count": null, "id": "d4107273-0ad2-40ad-aa11-933cd8a2bc83", "metadata": {}, "outputs": [], "source": [ "\n", "for i = 1:ndp\n", " ns[i] = 2^(i+2)\n", " err[i] = abs(mybasel(ns[i]) - exact) \n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "f656979d-70d6-4a8b-abdd-3506df392940", "metadata": {}, "outputs": [], "source": [ "\n", "using PyPlot" ] }, { "cell_type": "code", "execution_count": null, "id": "10d5568a-a2e7-448d-9db4-7d92e618e12a", "metadata": {}, "outputs": [], "source": [ "\n", "loglog(ns, err, label=\"numerical experiment\", marker=\".\")\n", "grid(true)\n", "legend()\n", "xlabel(\"n\")\n", "ylabel(\"absolute error\")\n", "title(\"Finite Basel sum\");" ] }, { "cell_type": "code", "execution_count": null, "id": "2d4df83c-aff0-4f72-bb90-80a6789e1ef7", "metadata": {}, "outputs": [], "source": [ "\n", "loglog(ns, err, label=\"numerical experiment\", marker=\".\")\n", "loglog(ns, ns .^ (-1), label=L\"1/n\", linestyle=\"dashed\")\n", "loglog(ns, ns .^ (-2), label=L\"1/n^2\", linestyle=\"dashed\")\n", "loglog(ns, ns .^ (-3), label=L\"1/n^3\", linestyle=\"dashed\")\n", "grid(true)\n", "legend()\n", "xlabel(\"n\")\n", "ylabel(\"absolute error\")\n", "title(\"Finite Basel sum\");" ] }, { "cell_type": "code", "execution_count": null, "id": "c25cf3d1-9e04-43a5-bbd6-2a26b3a96599", "metadata": {}, "outputs": [], "source": [ "\n", "s1 = mybasel(50)\n", "s2 = mybasel(100)" ] }, { "cell_type": "code", "execution_count": null, "id": "84ae9559-9dec-4663-8f72-6ece5ef1e237", "metadata": {}, "outputs": [], "source": [ "round(abs(s2 - exact), sigdigits=1)" ] }, { "cell_type": "code", "execution_count": null, "id": "7ff60861-dc7d-437c-a8b0-130061832b7a", "metadata": {}, "outputs": [], "source": [ "\n", "richardson = 2*s2 - s1\n", "round(abs(richardson - exact), sigdigits=1)" ] }, { "cell_type": "code", "execution_count": null, "id": "ecf219fc-9dff-42d4-8383-2b7dfdf70795", "metadata": {}, "outputs": [], "source": [ "\n", "np = 10000\n", "round(abs(mybasel(np) - exact), sigdigits=1)" ] }, { "cell_type": "code", "execution_count": null, "id": "87a997a9-9813-4876-85c9-b2deebcb90c1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.11.6", "language": "julia", "name": "julia-1.11" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }