Skip to content

Refining Relaxation

import numpy as np
import matplotlib.pyplot as plt
import numpy.linalg as linalg

What is Refinement?

At the end of the last notebook we noticed that if we wanted to make nicer plots of the potential in a 2D region that we were going to have to compute the potential on lattices with more and more points. When you don't change the extension of a space that you are discretizing, but you increase the number of lattice points in its discretization, we call that making a finer lattice and the process refinement. For the example of the 2D square and rectangular lattices we have been considering, this means that we keep \(\ell_x\) and \(\ell_y\) fixed while increasing the number of lattice points, \(N_x\) and \(N_y\), in each direction.

So far, we have written code that computes the solution to Laplace's equation on a single fixed 2D lattice. Our task in this notebook, then, is to write a function that will solve the equation on a lattice with a user specified number of lattice points. Let's break this into manageable steps.

Refer back to the 2DRelaxation notebook as often as you need to in solving the exercises below.

Refinement in Practice

Exercise (a): Define a function called plot_lat that takes the number of lattice points in the \(x\)- and \(y\)-directions, \(N_x\) and \(N_y\), as well as the length of the space in the \(x\)- and \(y\)-directions, \(\ell_x\) and \(\ell_y\), as input, and plots a lattice with those properties. Design the function so that the default inputs for \(\ell_x\) and \(\ell_y\) are both 1.

Exercise (b): Before moving on, update your plot_lat function so that it colors the internal lattice points and those on the boundary different colors. It's fine to do this in the somewhat crude way that I did in the previous notebook, that is, to plot all the lattice points with one color and then to plot over that with only the internal lattice points in a different color.

The real point of this exercise is to understand how to pick out the internal lattice points. One way to do this is to define xin as some slicing of x and yin as some slicing of y. You will need to think carefully about what these slicings are. This will be valuable for the exercises below.

Exercise (c): Define a function called BuildM that takes the number of lattice points in the \(x\)- and \(y\)-directions, \(N_x\) and \(N_y\), as input, and fills in the matrix \(M\) of coefficients of the linear equation you will need to solve for relaxation. This matrix should only involve the interior points of the lattice and so it does not have dimensions \((N_x\cdot N_y) \times (N_x \cdot N_y)\). Before you begin programming work out what dimensions it does have. Use the \(M\) of the \(5\times 5\) lattice you worked on in the last notebook as an example to help you generalize to this case. Test your new function by making sure that it reproduces the correct \(M\) for the \(5\times5\) lattice.

Exercise (d): Define a function called BndyVals that takes the number of lattice points in the \(x\)- and \(y\)-directions, \(N_x\) and \(N_y\), as input, and fills in the boundry value vector \(b\) on the right side of the linear equation you will need to solve for relaxation. For this excercise, use the same boundary conditions as in the last notebook, namely: (i) along three of the walls, those at \(x=0\), \(x=1\), and at \(y=0\), the plates are grounded; and (ii) at \(y=1\) the potential is \(V(x,y=1)=\sin(\pi x)\). (You might notice that, for simplicity, at this point we are assuming \(\ell_x=1\). You will generalize this aspect of the problem in the next notebook.)

Exercise (e): Define a function called RelaxLaplace that sets up and solves the 2D relaxation problem for Laplace's equation using your BuildM and BndyVals functions. Take this function to have \(N_x\) and \(N_y\) as inputs and again use the boundary conditions: (i) along three of the walls, those at \(x=0\), \(x=1\), and at \(y=0\), the plates are grounded; and (ii) at \(y=1\) the potential is \(V(x,y=1)=\sin(\pi x)\). When combining your solution to the linear algebra problem and the boundary values you will need to use the reshape function, and this is where having thought through the slicing of the arrays setup by meshgrid will be valuable.

Exercise (f): Plot the potential you obtained in exercise (e), including boundary values, on a 2D contour plot. You no longer need to use a small number of contours as you can find your solution on as refined a lattice as you like. Compare your plot to that from exercise (d) of the 2DRelaxation notebook solution. Are you satisfied that your new result is simply a higher resolution version of the previous one?