diff --git a/lectures/approximation/approximation_auxiliary.py b/lectures/approximation/approximation_auxiliary.py index 530fac0..619ce8c 100644 --- a/lectures/approximation/approximation_auxiliary.py +++ b/lectures/approximation/approximation_auxiliary.py @@ -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): diff --git a/lectures/approximation/approximation_plots.py b/lectures/approximation/approximation_plots.py index e813b43..b61ed9c 100755 --- a/lectures/approximation/approximation_plots.py +++ b/lectures/approximation/approximation_plots.py @@ -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(): @@ -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() @@ -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() diff --git a/lectures/approximation/approximation_solutions_exercises.py b/lectures/approximation/approximation_solutions_exercises.py index 338b57d..8eaf329 100644 --- a/lectures/approximation/approximation_solutions_exercises.py +++ b/lectures/approximation/approximation_solutions_exercises.py @@ -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) @@ -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() diff --git a/lectures/approximation/notebook.ipynb b/lectures/approximation/notebook.ipynb index 5b9d835..73ce9b8 100644 --- a/lectures/approximation/notebook.ipynb +++ b/lectures/approximation/notebook.ipynb @@ -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", @@ -69,7 +69,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -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 \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": [ { @@ -160,7 +185,7 @@ } ], "source": [ - "plot_approximation_nodes([5, 10, 15, 20], strategy=\"uniform\")" + "plot_approximation_nodes([5, 10, 15, 20], nodes=\"uniform\")" ] }, { @@ -172,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -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 \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": {}, @@ -296,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -325,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -342,7 +336,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -369,7 +363,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -393,9 +387,16 @@ "" ] }, + { + "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": [ @@ -411,16 +412,16 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 12, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" }, @@ -453,7 +454,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -478,7 +479,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -487,7 +488,7 @@ "-0.3581719202049975" ] }, - "execution_count": 14, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -511,7 +512,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -549,7 +550,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -587,7 +588,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -604,7 +605,7 @@ } ], "source": [ - "plot_approximation_nodes([5, 10, 15, 20], strategy=\"chebychev\")" + "plot_approximation_nodes([5, 10, 15, 20], nodes=\"chebychev\")" ] }, { @@ -623,7 +624,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -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 \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", @@ -666,7 +665,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -676,7 +675,7 @@ " 0.1289, 0.0418, -0.0053])" ] }, - "execution_count": 16, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -695,7 +694,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -738,7 +737,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -827,7 +826,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -883,7 +882,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": {}, "outputs": [ {