{
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{
"source": [
"$$\n",
"\\def\\CC{\\bf C}\n",
"\\def\\QQ{\\bf Q}\n",
"\\def\\RR{\\bf R}\n",
"\\def\\ZZ{\\bf Z}\n",
"\\def\\NN{\\bf N}\n",
"$$\n",
"# Exact and float point computations - Stability and unstability in dynamics\n",
"\n",
"## Exact computations and floating point approximations\n",
"\n",
"There are a lot of different numbers in Sage. You will need to choose which kind of numbers you want to use depending on your computations. In this worksheet we will consider four types of numbers:\n",
"\n",
"- rationals\n",
"- floating point numbers\n",
"- algebraic numbers\n",
"\n",
"If you are interested in all possible ways of representing a real number in Sage, you can have a look at the tutorial \"Real and complex numbers in Sage\".\n",
"\n",
"### Floating point and rationals\n",
"\n",
"To create an integer or a rational number, you just write it as you would do on a sheet of paper"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"2 + 3^5 # an integer"
],
"outputs": [],
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"23 / 45 # a rational number"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"To create a floating point number, you need to add a dot"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"1.0"
],
"outputs": [],
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"3.25 + 22.18"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"Contrarily to integers and rationals, a floating point number has limited precision"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"2^100 + 2^10 - 2^100"
],
"outputs": [],
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"2.0^100.0 + 2.0^10.0 - 2.0^100.0"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"Because floating point numbers have limited precision they are much faster. In the following we will see that the the bad choice of numbers influence the computational time and the correctness.\n",
"\n",
"### Maps of the interval: fixed points and iteration\n",
"\n",
"Let us consider the map $f_4(x) = 4 x (1 - x)$ from the interval $[0,1]$ to itself. Prove that $f_4$ is surjective and plot it:"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"# edit here"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"Show that $f_4(3/4) = 3/4$ (in other words, the point $3/4$ is a fixed point of $f_4$). What do you expect from the following two commands (you have to guess whether the answer will be $True$ or $False$ and you can then check your answer by executing the cell):"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"s = 3.0 / 4.0\n",
"4 * s * (1 - s) == s"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"Now let $f_{7/2}(x) = 7/2 x (1 - x)$. Prove that $5/7$ is a fixed point of $f_{7/2}$. Is the following $True$ or $False$ :"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"s = 5.0 / 7.0\n",
"7.0 / 2.0 * s * (1.0 - s) == s"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"Perform the same two computations as above with rational numbers instead of floating point.:"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"# edit here"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"On a computer a floating point number is a number of the form $m\\, 2^n$ where $m$ (the mantissa) and $n$ (the exponent) have some fixed bounds. In particular, floating point numbers have *finite* precision. Computations with floating points numbers are inaccurate but very efficient.\n",
"\n",
"Compare the following computation with rationals:"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"s = 1\n",
"for i in range(10):\n",
" s = (s + 2/s) / 2\n",
"print s\n",
"print s.numerical_approx()"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"and the same computation with floating point numbers:"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"s = 1.0\n",
"for i in range(10):\n",
" s = (s + 2.0 / s) / 2.0\n",
"print s"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"What can you say?\n",
"\n",
"Now compare the following computation with rationals:"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"s = 5 / 7\n",
"for i in range(100):\n",
" s = 7 / 2 * s * (1 - s)\n",
"print s\n",
"print s.numerical_approx()"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"and the same computation with floating point numbers:"
],
"cell_type": "markdown",
"metadata": {}
},
{
"execution_count": null,
"cell_type": "code",
"source": [
"s = 5.0 / 7.0\n",
"for i in range(100):\n",
" s = 7.0 / 2.0 * s * (1.0 - s)\n",
"print s"
],
"outputs": [],
"metadata": {}
},
{
"source": [
"What can you say? What is the difference between the two cases we considered above? What makes the functions $s \\mapsto (s + 2/s) / 2$ and $s \\mapsto 7/2 s (1 - s)$ different?"
],
"cell_type": "markdown",
"metadata": {}
}
],
"metadata": {
"kernelspec": {
"display_name": "sagemath",
"name": "sagemath"
}
}
}