Sie sind auf Seite 1von 2

MATLAB mesh example

Weston Barger

June 30, 2016

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

1. We discretize the x coordinate with x = 0 : .2 : 1. This gives us 6 x grid points.

2. We discretize the t coordinate with t = 0 : .25 : 1. This gives us 5 t grid points.

3. We mesh the space with [X , T ] = meshgrid(x , t ). Then

0 0.2 0.4 0.6 0.8 1 0 0 0 0 0 0


0 0.2 0.4 0.6 0.8 1 .25 .25 .25 .25 .25 .25
X = 0 0.2 0.4 0.6 0.8 1 T = .5 .5 .5 .5 .5 .5
0 0.2 0.4 0.6 0.8 1 .75 .75 .75 .75 .75 .75
0 0.2 0.4 0.6 0.8 1 1 1 1 1 1 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

1.0000 1.0000 1.0000 1.0000 1.0000 1.0000


0.9689 0.9689 0.9689 0.9689 0.9689 0.9689
cos(T ) = 0.8776 0.8776 0.8776 0.8776 0.8776 0.8776
0.7317 0.7317 0.7317 0.7317 0.7317 0.7317
0.5403 0.5403 0.5403 0.5403 0.5403 0.5403

Similarly,

0 0.1987 0.3894 0.5646 0.7174 0.8415


0 0.1987 0.3894 0.5646 0.7174 0.8415
sin(X ) = 0 0.1987 0.3894 0.5646 0.7174 0.8415
0 0.1987 0.3894 0.5646 0.7174 0.8415
0 0.1987 0.3894 0.5646 0.7174 0.8415

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.

Das könnte Ihnen auch gefallen