assignment1.plotting module¶
Plotting helpers for dg1
solver.
-
class
assignment1.plotting.
DG1Animate
(solver, fig=None, ax=None, interp_points=None)[source]¶ Bases:
object
Helper for animating a solution.
Assumes the solution (which is updated via
solver
) produces a solution that remains in the same bounding box as \(u(x, 0)\) (give or take some noise).Parameters: - solver (
dg1.DG1Solver
) – The solver which computes and updates the solution. - fig (
matplotlib.figure.Figure
) – (Optional) A figure to use for plotting. Intended to be passed when creating amatplotlib.animation.FuncAnimation
. - ax (
matplotlib.artist.Artist
) – (Optional) An axis to be used for plotting. - interp_points (int) – (Optional) The number of points to use to represent
polynomials on an interval. Defaults to
INTERVAL_POINTS
.
Raises: ValueError
if one offig
orax
is passed, but not both.-
init_func
()[source]¶ An initialization function for the animation.
Plots the initial data and stores the lines created.
Return type: list
ofmatplotlib.lines.Line2D
Returns: List of the updated matplotlib line objects, with length equal to \(n\) (coming from solver
).
-
update_plot
(frame_number)[source]¶ Update the lines in the plot.
First advances the solver and then uses the updated value to update the
matplotlib.lines.Line2D
objects associated to each interval.Parameters: frame_number (int) – (Unused) The current frame. Return type: list
ofmatplotlib.lines.Line2D
Returns: List of the updated matplotlib line objects, with length equal to \(n\) (coming from solver
).Raises: ValueError
if the frame number doesn’t make the current step on the solver.
- solver (
-
assignment1.plotting.
INTERVAL_POINTS
= 10¶ Number of points to use when plotting a polynomial on an interval.
-
class
assignment1.plotting.
PolynomialInterpolate
(x_vals, num_points=None)[source]¶ Bases:
object
Polynomial interpolation from node points.
Assumes the first and last \(x\)-value are the endpoints of the interval.
Using Lagrange basis polynomials we can write our polynomial as
\[p(x) = \sum_{j} y_j \ell_j(x)\]and we can compute \(\ell_j(x)\) of our data without ever computing the coefficients. We do this by computing all pairwise differences of our \(x\)-values and the interpolating values. Then we take the products of these differences (leaving out one of the interpolating values).
Parameters: - x_vals (
numpy.ndarray
) – List of \(x\)-values that uniquely define a polynomial. The degree is one less than the number of points. - num_points (int) – (Optional) The number of points to use to represent
the polynomial. Defaults to
INTERVAL_POINTS
.
-
classmethod
from_solver
(solver, num_points=None)[source]¶ Polynomial interpolation factory from a solver.
The reference interval for the interpolation is assumed to be in the first column of the \(x\)-values stored on the solver.
Parameters: - solver (
dg1.DG1Solver
) – A solver containing \(x\)-values. - num_points (int) – (Optional) The number of points to use to represent
the polynomial. Defaults to
INTERVAL_POINTS
.
Return type: Returns: Interpolation object for the reference
- solver (
-
interpolate
(y_vals)[source]¶ Evaluate interpolated polynomial given \(y\)-values.
We’ve already pre-computed the values \(\ell_j(x)\) for all the \(x\)-values we use in our interval (
num_points
in all, using the interpolating \(x\)-values to compute the \(\ell_j(x)\)). So we simply use them to compute\[p(x) = \sum_{j} y_j \ell_j(x)\]using the \(y_j\) from
y_vals
.Parameters: y_vals ( numpy.ndarray
) – Array of \(y\)-values that uniquely define our interpolating polynomial. If 1D, converted into a column vector before returning.Return type: numpy.ndarray
Returns: 2D array containing \(p(x)\) for each \(x\)-value in the interval ( num_points
in all). If there are multiple columns iny_vals
(i.e. multiple \(p(x)\)) then each column of the result will corresponding to each of these polynomials evaluated atall_x
.
- x_vals (
-
assignment1.plotting.
make_lagrange_matrix
(x_vals, all_x)[source]¶ Make matrix where \(M_{ij} = \ell_j(x_i)\).
This matrix contains the Lagrange interpolating polynomials evaluated on the interval given by
x_vals
. The \(x_i\) (corresponding to rows in \(M\)) are thenum_points
possible \(x\)-values inall_x
and the \(\ell_j\) (corresponding to columns in \(M\)) are the Lagrange interpolating polynomials interpolated on the points inx_vals
.Parameters: - x_vals (
numpy.ndarray
) – 1D array of \(x\)-values used to interpolate data via Lagrange basis functions. - all_x (
numpy.ndarray
) – 1D array of points to evaluate the \(\ell_j(x)`\) at.
Return type: Returns: The matrix \(M\).
- x_vals (
-
assignment1.plotting.
plot_convergence
(p_order, interval_sizes, colors, solver_factory, interval_width=1.0, total_time=1.0, points_on_ref_int=None)[source]¶ Plot a convergence plot for a given order.
Creates a side-by-side of error plots and the solutions as the mesh is refined.
Parameters: - p_order (int) – The order of accuracy desired.
- interval_sizes (
numpy.ndarray
) – Array of \(n\) values to use for the number of sub-intervals. - colors (list) – List of triples RGB (each a color). Expected to be the
same length as
interval_sizes
. - solver_factory (type) – Class that can be used to construct a solver.
- interval_width (float) – (Optional) The width of the interval where the solver works. Defaults to 1.0.
- total_time (float) – (Optional) The total time to run the solver. Defaults to 1.0.
- points_on_ref_int (
function
) – (Optional) The method used to partition the reference interval \(\left[0, h\right]\) into node points. Defaults toget_evenly_spaced_points()
.
-
assignment1.plotting.
plot_solution
(color, num_cols, interp_func, solver, ax)[source]¶ Plot the solution and return the newly created lines.
Helper for
DG1Animate
.Parameters: - color (str) – The color to use in plotting the solution.
- num_cols (int) – The number of columsn in the
solution
. - interp_func (
PolynomialInterpolate
) – The polynomial interpolation object used to map a solution onto a set of points. - solver (
dg1.DG1Solver
) – A solver containing asolution
andnode_points
. - ax (
matplotlib.artist.Artist
) – An axis to be used for plotting.
Return type: Returns: List of the updated matplotlib line objects.