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": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "view-in-github",
|
||||
"colab_type": "text"
|
||||
"colab_type": "text",
|
||||
"id": "view-in-github"
|
||||
},
|
||||
"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>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "t9vk9Elugvmi"
|
||||
},
|
||||
"source": [
|
||||
"# **Notebook 16.3: Contraction mappings**\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",
|
||||
"\n",
|
||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||
],
|
||||
"metadata": {
|
||||
"id": "t9vk9Elugvmi"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
],
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "OLComQyvCIJ7"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "4Pfz2KSghdVI"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Define a function that is a contraction mapping\n",
|
||||
"def f(z):\n",
|
||||
" return 0.3 + 0.5 *z + 0.02 * np.sin(z*15)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "4Pfz2KSghdVI"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "zEwCbIx0hpAI"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def draw_function(f, fixed_point=None):\n",
|
||||
" z = np.arange(0,1,0.01)\n",
|
||||
@@ -84,35 +75,36 @@
|
||||
" ax.set_xlabel('Input, $z$')\n",
|
||||
" ax.set_ylabel('Output, f$[z]$')\n",
|
||||
" plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "zEwCbIx0hpAI"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"draw_function(f)"
|
||||
],
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "k4e5Yu0fl8bz"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"draw_function(f)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Now let's find where $\\mbox{f}[z]=z$ using fixed point iteration"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "DfgKrpCAjnol"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Now let's find where $\\text{f}[z]=z$ using fixed point iteration"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "bAOBvZT-j3lv"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Takes a function f and a starting point z\n",
|
||||
"def fixed_point_iteration(f, z0):\n",
|
||||
@@ -125,115 +117,117 @@
|
||||
"\n",
|
||||
"\n",
|
||||
" return z_out"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "bAOBvZT-j3lv"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"Now let's test that and plot the solution"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "CAS0lgIomAa0"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Now let's test that and plot the solution"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "EYQZJdNPk8Lg"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Now let's test that\n",
|
||||
"z = fixed_point_iteration(f, 0.2)\n",
|
||||
"draw_function(f, z)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "EYQZJdNPk8Lg"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "4DipPiqVlnwJ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Let's define another function\n",
|
||||
"def f2(z):\n",
|
||||
" return 0.7 + -0.6 *z + 0.03 * np.sin(z*15)\n",
|
||||
"draw_function(f2)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "4DipPiqVlnwJ"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "tYOdbWcomdEE"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Now let's test that\n",
|
||||
"# TODO Before running this code, predict what you think will happen\n",
|
||||
"z = fixed_point_iteration(f2, 0.9)\n",
|
||||
"draw_function(f2, z)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "tYOdbWcomdEE"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Mni37RUpmrIu"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Let's define another function\n",
|
||||
"# Define a function that is a contraction mapping\n",
|
||||
"def f3(z):\n",
|
||||
" return -0.2 + 1.5 *z + 0.1 * np.sin(z*15)\n",
|
||||
"draw_function(f3)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Mni37RUpmrIu"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "agt5mfJrnM1O"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Now let's test that\n",
|
||||
"# TODO Before running this code, predict what you think will happen\n",
|
||||
"z = fixed_point_iteration(f3, 0.7)\n",
|
||||
"draw_function(f3, z)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "agt5mfJrnM1O"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"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": {
|
||||
"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",
|
||||
"source": [
|
||||
"def f4(z):\n",
|
||||
" return -0.3 + 0.5 *z + 0.02 * np.sin(z*15)"
|
||||
],
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"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",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "GMX64Iz0nl-B"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def fixed_point_iteration_z_plus_f(f, y, z0):\n",
|
||||
" # TODO -- write this function\n",
|
||||
@@ -241,15 +235,15 @@
|
||||
" z_out = 1\n",
|
||||
"\n",
|
||||
" return z_out"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "GMX64Iz0nl-B"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "uXxKHad5qT8Y"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def draw_function2(f, y, fixed_point=None):\n",
|
||||
" z = np.arange(0,1,0.01)\n",
|
||||
@@ -267,15 +261,15 @@
|
||||
" ax.set_xlabel('Input, $z$')\n",
|
||||
" ax.set_ylabel('Output, z+f$[z]$')\n",
|
||||
" plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "uXxKHad5qT8Y"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "mNEBXC3Aqd_1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Test this out and draw\n",
|
||||
"y = 0.8\n",
|
||||
@@ -283,12 +277,23 @@
|
||||
"draw_function2(f4,y,z)\n",
|
||||
"# If you have done this correctly, the red dot should be\n",
|
||||
"# 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