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).


ValueError if one of fig or ax is passed, but not both.


An initialization function for the animation.

Plots the initial data and stores the lines created.

Return type:list of matplotlib.lines.Line2D
Returns:List of the updated matplotlib line objects, with length equal to \(n\) (coming from solver).

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 of matplotlib.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.
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).

  • 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.

  • 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:



Interpolation object for the reference


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 in y_vals (i.e. multiple \(p(x)\)) then each column of the result will corresponding to each of these polynomials evaluated at all_x.
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 the num_points possible \(x\)-values in all_x and the \(\ell_j\) (corresponding to columns in \(M\)) are the Lagrange interpolating polynomials interpolated on the points in x_vals.

  • 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:



The matrix \(M\).

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.

  • 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 to get_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.

  • 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 a solution and node_points.
  • ax (matplotlib.artist.Artist) – An axis to be used for plotting.
Return type:

list of matplotlib.lines.Line2D


List of the updated matplotlib line objects.