{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In lecture 4 we demonstrate rational approximations with prescribed accuracy and continued fractions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Rational Approximations with Prescribed Accuracy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make a rational approximation with a prescribed accuracy we do\n", "\n", "1. evaluate the number as a floating-point number in the desired precision\n", "\n", "2. converted the evaluated number into a rational number." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, compute a rational approximation of $\\pi$ accurate to three decimal places." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "333/106" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "QQ(pi.n(digits=3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Continued Fractions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A continued fraction is defined by a list of natural numbers. Let look first at an example." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/latex": [ "$$\\newcommand{\\Bold}[1]{\\mathbf{#1}}1\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle 2\n", "+ \\frac{\\displaystyle 1}{\\displaystyle \\dots}}}}}}}}}}$$" ], "text/plain": [ "[1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ...]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c = continued_fraction(sqrt(2))\n", "show(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the continued fraction of $\\sqrt{2}$ is very regular." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``convergents`` of the continued fraction define a sequence of rational approximations of increasing accuracy." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "lazy list [1, 3/2, 7/5, ...]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.convergents()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``lazy`` aspect of the outcome refers to the delayed evaluation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us force the computation of the convergents and ask for the first 8 convergents." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cvg = c.convergents()\n", "L = cvg[:8].list()\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To compute the relative errors of the approximations, we run a list comprehension." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "['2.9e-01',\n", " '6.1e-02',\n", " '1.0e-02',\n", " '1.7e-03',\n", " '3.0e-04',\n", " '5.1e-05',\n", " '8.8e-06',\n", " '1.5e-06']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "['%.1e' % (abs(sqrt(2) - x)/abs(sqrt(2))) for x in L]" ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 10.3", "language": "sage", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 4 }