Matlab
Matlab
Weston Barger
1 MATLAB
1.1 Mesh Grid
Suppose we want to use MATLAB to visualize a function of two variables u(x , t ). In MATLAB, we can
represent functions of two dimensions as a grid of points. Let’s say we want to represent u(x , t ) for
(x , t ) ∈ [a, b] × [0, T ]. Then we use a 2D array, let’s call it U , whose value U (i , j ) corresponds to the
ij -th point in a grid on [a, b] × [0, T ]. To restate, we first represent a plane in 2D space as a grid of discrete
points, call it G. Then, we evaluate u(xi , tj ) for (xi , tj ) ∈ G and set U (i , j ) = u(xi , tj ).
To acomplish this in MATLAB, we can make use of the meshgrid function. We represent a 2D plane as
a pair of two dimensional arrays X and T . The function u is represented as a single 2D array, U , evaluated
at those pairs.
Example 1.1. Suppose we want to visualize the function u(x , t ) = cos(t ) sin(x ) on [0, 1] × [0, 1].
Note that X and T have the same dimension, so they can be added, subtracted, pointwise multiplied
etc. Each row of X , X (i , :), represents the interval [0, 1] at the corresponding time T (i , :). Suppose
that U is our grid representation of u(x , t ). We want U (i , j ) = u(X (i , j ), T (i , j )). For example,
u(.2, .5) = U (3, 2) = u(X (3, 2), T (3, 2)).
1
4. Functions can act on whole grids. So, cos(T ) is an array with the same size as T with
Similarly,
5. Now we can compute u(x , t ) on the grid. Here, we are sure to use the pointwise multiplication
operator “.∗” . So, U = cos(T ). ∗ sin(X ). Using “∗” for multiplication here will give the incorrect
result, because in MATLAB, “ ∗ ” means matrix multiplication. An example of how to use the plotting
features is give in the accompanying .m file.