Add files via upload
This commit is contained in:
@@ -1,33 +1,22 @@
|
|||||||
{
|
{
|
||||||
"nbformat": 4,
|
|
||||||
"nbformat_minor": 0,
|
|
||||||
"metadata": {
|
|
||||||
"colab": {
|
|
||||||
"provenance": [],
|
|
||||||
"authorship_tag": "ABX9TyNeCWINUqqUGKMcxsqPFTAh",
|
|
||||||
"include_colab_link": true
|
|
||||||
},
|
|
||||||
"kernelspec": {
|
|
||||||
"name": "python3",
|
|
||||||
"display_name": "Python 3"
|
|
||||||
},
|
|
||||||
"language_info": {
|
|
||||||
"name": "python"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
|
"attachments": {},
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "view-in-github",
|
"colab_type": "text",
|
||||||
"colab_type": "text"
|
"id": "view-in-github"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Notebooks/Chap16/16_3_Contraction_Mappings.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
"<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Notebooks/Chap16/16_3_Contraction_Mappings.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"attachments": {},
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "t9vk9Elugvmi"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"# **Notebook 16.3: Contraction mappings**\n",
|
"# **Notebook 16.3: Contraction mappings**\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -36,38 +25,40 @@
|
|||||||
"Work through the cells below, running each cell in turn. In various places you will see the words \"TO DO\". Follow the instructions at these places and make predictions about what is going to happen or write code to complete the functions.\n",
|
"Work through the cells below, running each cell in turn. In various places you will see the words \"TO DO\". Follow the instructions at these places and make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "t9vk9Elugvmi"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"execution_count": null,
|
||||||
"import numpy as np\n",
|
|
||||||
"import matplotlib.pyplot as plt"
|
|
||||||
],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "OLComQyvCIJ7"
|
"id": "OLComQyvCIJ7"
|
||||||
},
|
},
|
||||||
"execution_count": null,
|
"outputs": [],
|
||||||
"outputs": []
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import matplotlib.pyplot as plt"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "4Pfz2KSghdVI"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Define a function that is a contraction mapping\n",
|
"# Define a function that is a contraction mapping\n",
|
||||||
"def f(z):\n",
|
"def f(z):\n",
|
||||||
" return 0.3 + 0.5 *z + 0.02 * np.sin(z*15)"
|
" return 0.3 + 0.5 *z + 0.02 * np.sin(z*15)"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "4Pfz2KSghdVI"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "zEwCbIx0hpAI"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def draw_function(f, fixed_point=None):\n",
|
"def draw_function(f, fixed_point=None):\n",
|
||||||
" z = np.arange(0,1,0.01)\n",
|
" z = np.arange(0,1,0.01)\n",
|
||||||
@@ -84,35 +75,36 @@
|
|||||||
" ax.set_xlabel('Input, $z$')\n",
|
" ax.set_xlabel('Input, $z$')\n",
|
||||||
" ax.set_ylabel('Output, f$[z]$')\n",
|
" ax.set_ylabel('Output, f$[z]$')\n",
|
||||||
" plt.show()"
|
" plt.show()"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "zEwCbIx0hpAI"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"execution_count": null,
|
||||||
"draw_function(f)"
|
|
||||||
],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "k4e5Yu0fl8bz"
|
"id": "k4e5Yu0fl8bz"
|
||||||
},
|
},
|
||||||
"execution_count": null,
|
"outputs": [],
|
||||||
"outputs": []
|
"source": [
|
||||||
|
"draw_function(f)"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"attachments": {},
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"source": [
|
|
||||||
"Now let's find where $\\mbox{f}[z]=z$ using fixed point iteration"
|
|
||||||
],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "DfgKrpCAjnol"
|
"id": "DfgKrpCAjnol"
|
||||||
}
|
},
|
||||||
|
"source": [
|
||||||
|
"Now let's find where $\\text{f}[z]=z$ using fixed point iteration"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "bAOBvZT-j3lv"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Takes a function f and a starting point z\n",
|
"# Takes a function f and a starting point z\n",
|
||||||
"def fixed_point_iteration(f, z0):\n",
|
"def fixed_point_iteration(f, z0):\n",
|
||||||
@@ -125,115 +117,117 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
" return z_out"
|
" return z_out"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "bAOBvZT-j3lv"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"attachments": {},
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"source": [
|
|
||||||
"Now let's test that and plot the solution"
|
|
||||||
],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "CAS0lgIomAa0"
|
"id": "CAS0lgIomAa0"
|
||||||
}
|
},
|
||||||
|
"source": [
|
||||||
|
"Now let's test that and plot the solution"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "EYQZJdNPk8Lg"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Now let's test that\n",
|
"# Now let's test that\n",
|
||||||
"z = fixed_point_iteration(f, 0.2)\n",
|
"z = fixed_point_iteration(f, 0.2)\n",
|
||||||
"draw_function(f, z)"
|
"draw_function(f, z)"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "EYQZJdNPk8Lg"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "4DipPiqVlnwJ"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Let's define another function\n",
|
"# Let's define another function\n",
|
||||||
"def f2(z):\n",
|
"def f2(z):\n",
|
||||||
" return 0.7 + -0.6 *z + 0.03 * np.sin(z*15)\n",
|
" return 0.7 + -0.6 *z + 0.03 * np.sin(z*15)\n",
|
||||||
"draw_function(f2)"
|
"draw_function(f2)"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "4DipPiqVlnwJ"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "tYOdbWcomdEE"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Now let's test that\n",
|
"# Now let's test that\n",
|
||||||
"# TODO Before running this code, predict what you think will happen\n",
|
"# TODO Before running this code, predict what you think will happen\n",
|
||||||
"z = fixed_point_iteration(f2, 0.9)\n",
|
"z = fixed_point_iteration(f2, 0.9)\n",
|
||||||
"draw_function(f2, z)"
|
"draw_function(f2, z)"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "tYOdbWcomdEE"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "Mni37RUpmrIu"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Let's define another function\n",
|
"# Let's define another function\n",
|
||||||
"# Define a function that is a contraction mapping\n",
|
"# Define a function that is a contraction mapping\n",
|
||||||
"def f3(z):\n",
|
"def f3(z):\n",
|
||||||
" return -0.2 + 1.5 *z + 0.1 * np.sin(z*15)\n",
|
" return -0.2 + 1.5 *z + 0.1 * np.sin(z*15)\n",
|
||||||
"draw_function(f3)"
|
"draw_function(f3)"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "Mni37RUpmrIu"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "agt5mfJrnM1O"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Now let's test that\n",
|
"# Now let's test that\n",
|
||||||
"# TODO Before running this code, predict what you think will happen\n",
|
"# TODO Before running this code, predict what you think will happen\n",
|
||||||
"z = fixed_point_iteration(f3, 0.7)\n",
|
"z = fixed_point_iteration(f3, 0.7)\n",
|
||||||
"draw_function(f3, z)"
|
"draw_function(f3, z)"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "agt5mfJrnM1O"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"attachments": {},
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"source": [
|
|
||||||
"Finally, let's invert a problem of the form $y = z+ f[z]$ for a given value of $y$. What is the $z$ that maps to it?"
|
|
||||||
],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "n6GI46-ZoQz6"
|
"id": "n6GI46-ZoQz6"
|
||||||
}
|
},
|
||||||
|
"source": [
|
||||||
|
"Finally, let's invert a problem of the form $y = z+ f[z]$ for a given value of $y$. What is the $z$ that maps to it?"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"execution_count": null,
|
||||||
"def f4(z):\n",
|
|
||||||
" return -0.3 + 0.5 *z + 0.02 * np.sin(z*15)"
|
|
||||||
],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "dy6r3jr9rjPf"
|
"id": "dy6r3jr9rjPf"
|
||||||
},
|
},
|
||||||
"execution_count": null,
|
"outputs": [],
|
||||||
"outputs": []
|
"source": [
|
||||||
|
"def f4(z):\n",
|
||||||
|
" return -0.3 + 0.5 *z + 0.02 * np.sin(z*15)"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "GMX64Iz0nl-B"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def fixed_point_iteration_z_plus_f(f, y, z0):\n",
|
"def fixed_point_iteration_z_plus_f(f, y, z0):\n",
|
||||||
" # TODO -- write this function\n",
|
" # TODO -- write this function\n",
|
||||||
@@ -241,15 +235,15 @@
|
|||||||
" z_out = 1\n",
|
" z_out = 1\n",
|
||||||
"\n",
|
"\n",
|
||||||
" return z_out"
|
" return z_out"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "GMX64Iz0nl-B"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "uXxKHad5qT8Y"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def draw_function2(f, y, fixed_point=None):\n",
|
"def draw_function2(f, y, fixed_point=None):\n",
|
||||||
" z = np.arange(0,1,0.01)\n",
|
" z = np.arange(0,1,0.01)\n",
|
||||||
@@ -267,15 +261,15 @@
|
|||||||
" ax.set_xlabel('Input, $z$')\n",
|
" ax.set_xlabel('Input, $z$')\n",
|
||||||
" ax.set_ylabel('Output, z+f$[z]$')\n",
|
" ax.set_ylabel('Output, z+f$[z]$')\n",
|
||||||
" plt.show()"
|
" plt.show()"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "uXxKHad5qT8Y"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "mNEBXC3Aqd_1"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Test this out and draw\n",
|
"# Test this out and draw\n",
|
||||||
"y = 0.8\n",
|
"y = 0.8\n",
|
||||||
@@ -283,12 +277,23 @@
|
|||||||
"draw_function2(f4,y,z)\n",
|
"draw_function2(f4,y,z)\n",
|
||||||
"# If you have done this correctly, the red dot should be\n",
|
"# If you have done this correctly, the red dot should be\n",
|
||||||
"# where the cyan curve has a y value of 0.8"
|
"# where the cyan curve has a y value of 0.8"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "mNEBXC3Aqd_1"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
}
|
"metadata": {
|
||||||
|
"colab": {
|
||||||
|
"authorship_tag": "ABX9TyNeCWINUqqUGKMcxsqPFTAh",
|
||||||
|
"include_colab_link": true,
|
||||||
|
"provenance": []
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"name": "python"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user