23.10.2014 Views

Matlab Chapter6.pdf

Matlab Chapter6.pdf

Matlab Chapter6.pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The matrix a is 30 × 30. The element in the middle a(15, 15) is 1, all the elements in row 7<br />

are 0.1, and all the remaining elements in column 15 are 0.2. mesh(a) then interprets the rows<br />

and columns of a as an x-y coordinate grid, with the values a(i, j) forming the mesh surface<br />

above the points (i, j).<br />

2.2.1 Contour plots<br />

If you managed to draw a plot try the command<br />

≫ contour(u)<br />

You should get a contour plot of the heat distribution for example. Here’s the code:<br />

≫ [x, y] = meshgrid(−2.1 : 0.15 : 2.1, −6 : 0.15 : 6);<br />

≫ u = 80 ∗ y. ∧ 2. ∗ exp(−x. ∧ 2 − 0.3 ∗ y. ∧ 2);<br />

≫ contour(u)<br />

Remark 2.2 The function contour can take a second input variable. It can be a scalar specifying<br />

how many contour levels to plot, or it can be a vector specifying the values at which to<br />

plot the contour levels.<br />

Remark 2.3 A simple contour plotting facility is provided by ezcontour. ezcontour in the<br />

following example produces contours for the function sin(3y − x 2 + 1) + cos(2y 2 − 2x) over the<br />

range −2 ≤ x ≤ 2 and −1 ≤ y ≤ 1;<br />

subplot(211)<br />

ezcontour( ′ sin(3 ∗ y − x ∧ 2 + 1) + cos(2 ∗ y ∧ 2 − 2 ∗ x) ′ , [−2 2 − 1 1]);<br />

x = −2 : .01 : 2; y = −1 : .01 : 1;<br />

[X, Y]=meshgrid(x, y);<br />

Z = sin(3 ∗ Y − X. ∧ 2 + 1) + cos(2 ∗ Y. ∧ 2 − 2 ∗ X);<br />

subplot(212)<br />

contour(x, y, Z, 20)<br />

Example 2.2 (meshc) A 3-D contour may be drawn under a surface as follows:<br />

≫ [x y] = meshgrid(−2 : .2 : 2);<br />

≫ z = x. ∗ exp(−x. ∧ 2 − y. ∧ 2);<br />

≫ meshc(z)<br />

Example 2.3 (Cropping a surface with NaNs) If a matrix for a surface plot contains a<br />

data of type NaNs, these elements are not plotted. This enables you to cut away (crop) parts<br />

of a surface.<br />

[x y] = meshgrid(−2 : .2 : 2, −2 : .2 : 2);<br />

z = x. ∗ exp(−x. ∧ 2 − y. ∧ 2);<br />

c = z; % preserve the original surface<br />

c(1 : 11, 1 : 21) = nan ∗ c(1 : 11, 1 : 21);<br />

mesh(c), xlabel(x-axis), ylabel(y-axis)<br />

Example 2.4 Consider the scalar function of two variables V = x 2 + y. The gradient of V is<br />

defined as the vector field<br />

( ∂V<br />

∇V =<br />

∂x , ∂V )<br />

= (2x, 1).<br />

∂y<br />

11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!