Section 8.1: The simplest PDE and the method of characteristics
When solving PDEs in two variables, it is sometimes desirable to graph the solution as a function of two variables (that is, as a surface), rather than plotting snapshots of the solution. This is particularly appropriate when neither variable is time.
Two-dimensional graphics in MATLAB
Recall that to plot a function of one variable, we create a grid using the linspace command and then evaluate the desired function on the grid. We can then call the plot command. For a function of two variables, the procedure is similar. However, we need to create a grid on a rectangle rather than on an interval, which is a bit more complicated.
The meshgrid command takes two one-dimensional grids, on the intervals a<x<b and c<y<d, and creates the necessary grid on the rectangle {(x,y) : a<x<b and c<y<d}. This grid is represented as two matrices X and Y; the points in the grid are then (Xij,Yij). Evaluating f(x,y) on the grid means producing a matrix Z such that
This last step is easy, since MATLAB supports vectorized operations.
Here is how I created Figure 8.2 (page 314):
First, I create the two one-dimensional grids:
x=linspace(-5,5,41)';
y=linspace(0,10,41)';
Next, I invoke meshgrid to create the two-dimensional grid:
[X,Y]=meshgrid(x,y);
Finally, I compute the function u on the grid:
z=1./(1+(X+0.5*Y).^2);
The command for plotting the surface z=f(x,y) is called mesh:
mesh(x,y,z)
This surface looks better when viewed from a different angle (see "help view" for details):
view(20,45)
I can add the axis labels as follows:
Share with your friends: |