def compare_approxiation(x, y, x_approx, y_approx, error=None, name="1", title=""): """ Compare the input data with the fitted function """ _, ax = plt.subplots(2, 1) ax[0].plot(x, y, "o", label="Original data", markersize=2) ax[0].plot(x_approx, y_approx, label="Fitted line") ax[0].set_xlabel("$x$") ax[0].set_ylabel("$y$") ax[0].legend() ax[0].set_title(title) if y.shape == y_approx.shape: error = y - y_approx if error is not None: ax[1].plot(x, error, "o", label="Error", markersize=2) ax[1].plot(x_approx, np.zeros(y_approx.shape), label="Fitted line") ax[1].set_title("Error") ax[1].set_xlabel("$x$") ax[1].set_ylabel("$\Delta y$") else: print("Cannot compare difference, x and x_approx need to be equal") plt.tight_layout() show_and_save("task1_approx_{}".format(name))
def plot_phase_portrait(A, range=[[-10, 10], [-10, 10]], resolution=[100, 100], trajectory=None, name="phaseportrait", title=""): """ Create a phase portrait for matrix A in the given range with a given resolution. Optionally add a trajectory to plot in the phase portrait. Includes a filename and plot-title. """ x = np.linspace(range[0][0], range[0][1], num=resolution[0]) y = np.linspace(range[1][0], range[1][1], num=resolution[1]) xv, yv = np.meshgrid(x, y) xy = np.stack((np.ravel(xv), np.ravel(yv)), axis=-1) uv = xy @ A u = uv[:, 0].reshape(resolution) v = uv[:, 1].reshape(resolution) plt.figure() ax = plt.subplot(1, 1, 1) ax.set_aspect('equal') ax.set_xlim([-10, 10]) ax.set_ylim([-10, 10]) ax.set_title(title) ax.streamplot(x, y, u, v) if trajectory is not None: ax.plot(trajectory[:, 0], trajectory[:, 1]) show_and_save("task2_{}".format(name))
def plot_3d_data(x, y, z, name="", labels=["x", "y", "z"], title=""): """ Plot and save 3-D data. Parameters ---------- x : (N, 1) array-like x coordinates of the data. y : (N, 1) array-like y coordinates of the data. z : (N, 1) array-like z coordinates of the data. name : string, optional Filename addition. Default: "" labels : list, optional Labels of axes. Default: ["x", "y", "z"] title : string, optional Title of the plot. Default: "" """ # Create the figure fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # Plot the values ax.plot(x, y, z, linewidth=0.4) ax.set_xlabel(labels[0]) ax.set_ylabel(labels[1]) ax.set_zlabel(labels[2]) show_and_save("task4_{}".format(name))
def plot_data(x, y, name="", labels=["x", "y"], title=""): """ Plot and save 2-D data. Parameters ---------- x : (N, 1) array-like x coordinates of the data. y : (N, 1) array-like y coordinates of the data. name : string, optional Filename addition. Default: "" labels : list, optional Labels of axes. Default: ["x", "y"] title : string, optional Title of the plot. Default: "" """ plt.figure() plt.plot(x, y, label=title) plt.title(title) plt.legend() plt.xlabel(labels[0]) plt.ylabel(labels[1]) ax = plt.gca() ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func)) show_and_save("task5_{}".format(name))
def nonlinear_vf(x0, x1, yi): plt.figure() plt.quiver(x0[:, 0], x0[:, 1], x1[:, 0], yi) plt.title("Vector field generated with RBF") plt.xlabel("$x$") plt.ylabel("$y$") show_and_save("task3_{}".format("nonlinear_vf")) # Compute the mean squared error mse = ((np.array(list(zip(x1[:, 0], yi))) - x1)**2).mean(axis=0) print("Mean squared error: {}".format(mse))
def linear_vf(x0, x1, dt, A, t_end=0.1): # Solve x_dot=A*x with all x_0^(k) as initial points until x = task2.create_trajectory(x0, A, dt, t_end) # The resulting points are estimates for x_1^(k). plt.figure() plt.quiver(x0[:, 0], x0[:, 1], x[-1][:, 0], x[-1][:, 1]) plt.title("Vector field generated with linear operator") plt.xlabel("$x$") plt.ylabel("$y$") show_and_save("task3_{}".format("linear_vf")) # Compute the mean squared error to x1 mse = ((x[-1] - x1)**2).mean(axis=0) print("Mean squared error: {}".format(mse))
def scatter_3d_data(x, y, z, name="", labels=["x", "y", "z"], title="", c=None, cmap=None, norm=None): """ Scatter and save 3-D data. Parameters ---------- x : (N, 1) array-like x coordinates of the data. y : (N, 1) array-like y coordinates of the data. z : (N, 1) array-like z coordinates of the data. name : string, optional Filename addition. Default: "" labels : list, optional Labels of axes. Default: ["x", "y", "z"] title : string, optional Title of the plot. Default: "" """ # Create the figure fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # Scatter the values sc = ax.scatter(x, y, z, s=1, c=c, cmap=cmap, norm=norm) ax.set_title(title) ax.set_xlabel(labels[0]) ax.set_ylabel(labels[1]) ax.set_zlabel(labels[2]) cb = plt.colorbar(sc, pad=0.1) cb.set_ticks([0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200]) show_and_save("task5_{}".format(name))
def plot_vectorfields(*args, labels=[], name="", title=""): """ Plot multiple vectorfields (len(args)). Include optional labels, a filename and plot-title """ marker = ["o", "x"] plt.figure() for idx, arg in enumerate(args): if idx < len(labels): plt.plot(arg[:, 0], arg[:, 1], marker[idx % len(marker)], label=labels[idx], markersize=3) else: plt.plot(arg[:, 0], arg[:, 1], marker[idx % len(marker)], markersize=3) plt.title(title) plt.legend() plt.xlabel("$x$") plt.ylabel("$y$") show_and_save("task2_{}".format(name))
def scatter_data(x, y, name="", labels=["x", "y"], title=""): """ Scatter and save 2-D data. Parameters ---------- x : (N, 1) array-like x coordinates of the data. y : (N, 1) array-like y coordinates of the data. name : string, optional Filename addition. Default: "" labels : list, optional Labels of axes. Default: ["x", "y"] title : string, optional Title of the plot. Default: "" """ plt.figure() plt.scatter(x, y, label=title) plt.title(title) plt.legend() plt.xlabel(labels[0]) plt.ylabel(labels[1]) show_and_save("task3_{}".format(name))