def plot_two_dimensional_problem(): """Plot two-dimensional problem.""" x_fit = get_uniform_nodes(50) y_fit = get_uniform_nodes(50) X_fit, Y_fit = np.meshgrid(x_fit, y_fit) Z_fit = problem_two_dimensions(X_fit, Y_fit) fig = plt.figure() ax = fig.gca(projection="3d") ax.plot_surface(X_fit, Y_fit, Z_fit) ax.set_ylabel("y") ax.set_xlabel("x") ax.set_zlabel("f(x, y)")
def plot_two_dimensional_grid(nodes): """Plot two-dimensional grid.""" if nodes == "chebychev": x = get_chebyshev_nodes(10) y = get_chebyshev_nodes(10) elif nodes == "uniform": x = get_uniform_nodes(10) y = get_uniform_nodes(10) X, Y = np.meshgrid(x, y) # grid of point fig, ax = plt.subplots() for i in range(len(x)): ax.plot(X[i, :], Y[i, :], marker=".", color="k", linestyle="none")
def plot_runge_function_cubic(): """Plot cubic runge function.""" xvalues = get_uniform_nodes(10000, -1, 1) for degree in [5, 10, 15]: x_fit = get_uniform_nodes(degree, -1, 1) interp = interp1d(x_fit, problem_runge(x_fit), kind="cubic") yfit = interp(xvalues) fig, ax = plt.subplots() ax.plot(xvalues, problem_runge(xvalues), label="True") ax.plot(xvalues, yfit, label="Approximation") ax.legend() ax.set_title(f"Degree {degree}")
def test_exercise_2(): """Run test exercise 2.""" index = product( ["runge", "reciprocal_exponential", "kinked"], ["linear", "cubic", "chebychev"], [10, 20, 30], get_uniform_nodes(1000, -1, 1), ) index = pd.MultiIndex.from_tuples(index, names=("Function", "Method", "Degree", "Point")) df = pd.DataFrame(columns=["Value", "Approximation"], index=index) test_functions = {} test_functions["runge"] = problem_runge test_functions["reciprocal_exponential"] = problem_reciprocal_exponential test_functions["kinked"] = problem_kinked points = df.index.get_level_values("Point").unique() for function in df.index.get_level_values("Function").unique(): test_function = test_functions[function] for method in df.index.get_level_values("Method").unique(): for degree in df.index.get_level_values("Degree").unique(): interp = get_interpolator(method, degree, test_function) index = (function, method, degree, slice(None)) df.loc[index, "Approximation"] = interp(points) df.loc[index, "Value"] = test_function(points) df["Error"] = df["Value"] - df["Approximation"] df.groupby(["Function", "Method", "Degree"]).apply(compute_interpolation_error_df)
def get_interpolator(name, degree, func): """Return interpolator.""" args = (degree, -1, 1) if name in ["linear", "cubic"]: xnodes = get_uniform_nodes(*args) interp = interp1d(xnodes, func(xnodes), name) elif name in ["chebychev"]: xnodes = get_chebyshev_nodes(*args) interp = np.polynomial.Polynomial.fit(xnodes, func(xnodes), degree) return interp
def test_exercise_1(): """Run test exercise 1.""" 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) df["Value"] = problem_runge(df.index.get_level_values("Point")) for degree in [10, 20, 30, 40, 50]: xnodes = get_uniform_nodes(degree, -1, 1) poly = np.polynomial.Polynomial.fit(xnodes, problem_runge(xnodes), degree) xvalues = df.index.get_level_values("Point").unique() yvalues = poly(xvalues) df.loc[(degree, slice(None)), "Approximation"] = yvalues df["Error"] = df["Value"] - df["Approximation"] df.groupby("Degree").apply(compute_interpolation_error_df).plot()