Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
peisenha committed Dec 1, 2020
1 parent bde67da commit 17c35fb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 75 deletions.
8 changes: 1 addition & 7 deletions lectures/approximation/approximation_auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@


def get_uniform_nodes(n, a=-1, b=1):

nodes = np.tile(np.nan, n)

for i in range(1, n + 1):
nodes[i - 1] = a + (i - 1) / (n - 1) * (b - a)

return nodes
return np.linspace(a, b, num=n)


def get_chebyshev_nodes(n, a=-1, b=1):
Expand Down
11 changes: 6 additions & 5 deletions lectures/approximation/approximation_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from approximation_problems import problem_reciprocal_exponential
from approximation_problems import problem_runge
from numpy.polynomial import Chebyshev as T
from numpy.polynomial import Polynomial as P


def plot_problem_runge():
Expand All @@ -28,8 +29,8 @@ def plot_runge_multiple():

for degree in [5, 9]:
xnodes = np.linspace(a, b, degree)
c = np.polyfit(xnodes, problem_runge(xnodes), degree)
yfit = np.polyval(c, xvals)
poly = P.fit(xnodes, problem_runge(xnodes), degree)
yfit = poly(xvals)
ax.plot(xvals, yfit, label=" 9th-order")

ax.legend()
Expand All @@ -56,11 +57,11 @@ def plot_reciprocal_exponential(a=-5, b=5):
ax.plot(yvals, problem_reciprocal_exponential(yvals))


def plot_approximation_nodes(num_nodes, strategy="uniform"):
def plot_approximation_nodes(num_nodes, nodes="uniform"):

if strategy == "uniform":
if nodes == "uniform":
get_nodes = get_uniform_nodes
elif strategy == "chebychev":
elif nodes == "chebychev":
get_nodes = get_chebyshev_nodes

fig, ax = plt.subplots()
Expand Down
4 changes: 2 additions & 2 deletions lectures/approximation/approximation_solutions_exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def test_exercise_1():

index = product([10, 20, 30, 40, 50], np.linspace(-5, 5, 1000))
index = product([10, 20, 30, 40, 50], np.linspace(-1, 1, 1000))

index = pd.MultiIndex.from_tuples(index, names=("Degree", "Point"))
df = pd.DataFrame(columns=["Value", "Approximation"], index=index)
Expand All @@ -19,7 +19,7 @@ def test_exercise_1():

for degree in [10, 20, 30, 40, 50]:

xnodes = get_uniform_nodes(degree, -5, 5)
xnodes = get_uniform_nodes(degree, -1, 1)
poly = P.fit(xnodes, problem_runge(xnodes), degree)

xvalues = df.index.get_level_values("Point").unique()
Expand Down
121 changes: 60 additions & 61 deletions lectures/approximation/notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"Regardless of how the $n$ basis functions and nodes are chosen, computing the basis coefficients reduces to solving a linear equation.\n",
"\n",
"\\begin{align*}\n",
"\\sum_{j=1}^n = c_j \\phi_j(x) = f(x), \\qquad i = 1, ..., n\n",
"\\sum_{j=1}^n c_j \\phi_j(x) = f(x), \\qquad i = 1, ..., n\n",
"\\end{align*}\n",
"\n",
"Interpolation schemes differ only in how the basis functions $\\phi_j$ and interpolation nodes $x_j$ are chosen.\n",
Expand All @@ -69,7 +69,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -143,7 +143,32 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mget_uniform_nodes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m <no docstring>\n",
"\u001b[0;31mSource:\u001b[0m \n",
"\u001b[0;32mdef\u001b[0m \u001b[0mget_uniform_nodes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinspace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFile:\u001b[0m ~/external-storage/ownCloud/office/OpenSourceEconomics/teaching/scientific-computing/course/lectures/approximation/approximation_auxiliary.py\n",
"\u001b[0;31mType:\u001b[0m function\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"??get_uniform_nodes"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
Expand All @@ -160,7 +185,7 @@
}
],
"source": [
"plot_approximation_nodes([5, 10, 15, 20], strategy=\"uniform\")"
"plot_approximation_nodes([5, 10, 15, 20], nodes=\"uniform\")"
]
},
{
Expand All @@ -172,7 +197,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -252,37 +277,6 @@
"plot_basis_functions(\"monomial\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mget_uniform_nodes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m <no docstring>\n",
"\u001b[0;31mSource:\u001b[0m \n",
"\u001b[0;32mdef\u001b[0m \u001b[0mget_uniform_nodes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mnodes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnan\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mnodes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mnodes\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFile:\u001b[0m ~/external-storage/ownCloud/office/OpenSourceEconomics/teaching/scientific-computing/course/lectures/approximation/approximation_auxiliary.py\n",
"\u001b[0;31mType:\u001b[0m function\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"??get_uniform_nodes"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -296,7 +290,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -325,7 +319,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -342,7 +336,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -369,7 +363,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -393,9 +387,16 @@
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since we have a good understanding what is causing the warning, we can simply turn it of going forward. A documentation that shows how to deal with more fine-grained filters is available [here](https://pymotw.com/3/warnings/)."
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -411,16 +412,16 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x7f20237c4510>"
"<matplotlib.legend.Legend at 0x7fd0e8103890>"
]
},
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
},
Expand Down Expand Up @@ -453,7 +454,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -478,7 +479,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand All @@ -487,7 +488,7 @@
"-0.3581719202049975"
]
},
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -511,7 +512,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 34,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -549,7 +550,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -587,7 +588,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand All @@ -604,7 +605,7 @@
}
],
"source": [
"plot_approximation_nodes([5, 10, 15, 20], strategy=\"chebychev\")"
"plot_approximation_nodes([5, 10, 15, 20], nodes=\"chebychev\")"
]
},
{
Expand All @@ -623,7 +624,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand All @@ -639,9 +640,7 @@
"\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m <no docstring>\n",
"\u001b[0;31mSource:\u001b[0m \n",
"\u001b[0;32mdef\u001b[0m \u001b[0mget_interpolator_monomial_flexible_nodes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdegree\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnodes\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"uniform\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;32mdef\u001b[0m \u001b[0mget_interpolator_monomial_flexible_nodes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdegree\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnodes\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"uniform\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mnodes\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"uniform\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mget_nodes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_uniform_nodes\u001b[0m\u001b[0;34m\u001b[0m\n",
Expand All @@ -666,7 +665,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand All @@ -676,7 +675,7 @@
" 0.1289, 0.0418, -0.0053])"
]
},
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -695,7 +694,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -738,7 +737,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -827,7 +826,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -883,7 +882,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 21,
"metadata": {},
"outputs": [
{
Expand Down

0 comments on commit 17c35fb

Please sign in to comment.