You are on page 1of 67

MATLAB

(Roger Jang)
jang@mirlab.org
http://mirlab.org/jang

MATLAB

4-1

mesh surf

mesh Mesh
Plots
surf Surface
Plots
z = [0 2 1; 3 2 4; 4 4 4; 7 6 8];

4-1:plotxyz001.m
mesh(z);
xlabel('X = column index');
ylabel('Y = row index');

% X
% Y

Matrix Indexing
Coordinate

MATLAB

Conversion from matrix indexing to x-y


coordinates

i (= y)

j (= x)

jx
iy

8
(i, j) = (3, 2)
(x, y) = (2, 3)

MATLAB

4-1

4-1 plotxyz001.m
8
6
4
2
0
4
3
2
Y = row index

1.5

2.5

X = column index

MATLAB

4-1

4-2 plotxyz002.m
x y
mesh
z = [0 2 1; 3 2 4; 4 4 4; 7 6 8];
mesh(z);
xlabel('X = column index');

% X

ylabel('Y = row index');

% Y

for i=1:size(z,1)
for j=1:size(z,2)
h=text(j, i, z(i,j), num2str(z(i, j)));

set(h, 'hori', 'center', 'vertical', 'bottom', 'color', 'r'); %

end
end

MATLAB

4-1

4-2 plotxyz002.m
8

7
6

4
4

4
4
2

0
4

2
3
2

Y = row index

0
1

1.5

2.5

X = column index

MATLAB

Y
axis xy:

axis ij:

7
6

4
4

8
4

4
2
3

0
4

2
1

2
3
2

Y = row index

0
1

1.5

2.5

X = column index

00
1
2
3
Y = row index

Hint: Use the command after plotting the


surface/mesh.

1.5

2.5

X = column index

MATLAB

4-1

4-3 plotxyz011.m
meshgrid x y
Grid Points
xx yy x
y

MATLAB

4-1
4

1 5

2 18 24 30 36

2 3

2 6

3 7

4 24 32 40 48

4 3

4 8

5 27 36 45 54

5 3

5 9

3 21 28 35 42

zz

=3

xx

1
1 3

1 15 20 25 30

z=x*y
x=3:6 y=5:9
x
1

.*

.*

yy

Generated by meshgrid!

MATLAB

4-1

4-3 plotxyz011.m
x = 3:6;
y = 5:9;
[xx, yy] = meshgrid(x, y);

% xx yy

zz = xx.*yy;

% zz

subplot(2,2,1); mesh(xx);
title('xx'); axis tight
subplot(2,2,2); mesh(yy);
title('yy'); axis tight
subplot(2,2,3); mesh(xx, yy, zz);
title('zz xx yy '); axis tight

MATLAB

4-1

4-3 plotxyz011.m
xx

yy

6
5

3
4

zz xx yy

40
20
8

MATLAB

4-1
meshgrid
for-loop

x
= 3:6;
plotxyz011.m
y = 5:9;
zz=zeros(length(y), length(x));
50

for i=1:length(y)
for j=1:length(x)
zz(i,j)=y(i)*x(j);
end
end
mesh(x, y, zz);
xlabel('X'); ylabel('Y'); zlabel('Z'); axis
tight

40
Z

30
20
9
8

6
7

5
6

4
5

MATLAB

4-1

4-4 plotxyz01.m
linspace
x2 y2

z xe
x = linspace(-2, 2, 25);

% x [-2,2] 25

y = linspace(-2, 2, 25);

% y [-2,2] 25

[xx, yy] = meshgrid(x, y); % xx yy 2525


zz = xx.*exp(-xx.^2-yy.^2);

mesh(xx, yy, zz);

% zz 2525

MATLAB

4-1

4-4 plotxyz01.m
0.5

-0.5
2
1
0
-1
-2

-2

-1

MATLAB

4-1

4-5 plotxyz02.m
surf mesh
x = linspace(-2, 2, 25);

% x [-2,2] 25

y = linspace(-2, 2, 25);

% y [-2,2] 25

[xx,yy] = meshgrid(x, y);

% xx yy 2525

zz = xx.*exp(-xx.^2-yy.^2);

surf(xx, yy, zz);

% zz 252
%

MATLAB

4-1

4-5 plotxyz02.m
0.5

-0.5
2
1
0
-1
-2

-2

-1

MATLAB

4-1

peaks

MATLAB
peaks
Local Maxima
Local Minima

z 31 x e

2 x 2 y 1 2

x 3 5 x 2 y 2 1 x 1 2 y 2
10 x y e
e
3
5

MATLAB

4-1

MATLAB
peaks
z = 3*(1-x).^2.*exp(-(x.^2) (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2)
...
- 1/3*exp(-(x+1).^2 - y.^2)

MATLAB

4-1

peaks

Peaks

-5
2
0
-2
y

-3

-2

-1
x

MATLAB

4-1

meshz

meshz

4-6 plotxyz03.m
[x, y, z] = peaks;
meshz(x,y,z);
axis tight;

MATLAB

4-1

4-6 plotxyz03.m

-5
2
0
-2

-3

-2

-1

MATLAB

4-1

waterfall

waterfall x y

4-7 plotxyz04.m
[x, y, z] = peaks;
waterfall(x,y,z);
axis tight;

MATLAB

4-1

4-7 plotxyz04.m

-5
2
0
-2

-3

-2

-1

MATLAB

4-1

meshc

meshc
Contours
4-8 plotxyz05.m
[x, y, z] = peaks;
meshc(x, y, z);
axis tight;

MATLAB

4-1

4-8 plotxyz05.m

-5

-10
2
0
-2

-3

-2

-1

MATLAB

4-1

plot3

plot3
4-9 plotxyz06.m
t = linspace(0,20*pi, 501); % 0 20*pi 501
plot3(t.*sin(t), t.*cos(t), t); % tsin(t),tcos(t),t

MATLAB

4-1

4-9 plotxyz06.m

80
60
40
20
0
100
50
0
-50
-100

-100

-50

50

100

MATLAB

4-1

plot3

4-10 plotxyz07.m
t = linspace(0, 10*pi, 501);
plot3(t.*sin(t), t.*cos(t), t, t.*sin(t), t.*cos(t), -t);

MATLAB

4-1

4-10 plotxyz07.m
40
20
0
-20
-40
40
20
0
-20
-40

-40

-20

20

40

MATLAB

4-1

plot3

x y z plot3

4-11 plotxyz08.m
[x, y] = meshgrid(-2:0.1:2);
z = y.*exp(-x.^2-y.^2);
plot3(x, y, z);

MATLAB

4-1

4-11 plotxyz08.m
0.5

-0.5
2
1
0
-1
-2

-2

-1

MATLAB

4-1

plot3

MATLAB

griddata

MATLAB

4-1

4-12 plotxyz09.m

x = 6*rand(100,1)-3;

% x [-3, 3] 100

y = 6*rand(100,1)-3;

% y [-3, 3] 100

z = peaks(x, y);

% z peaks 100

[X, Y] = meshgrid(-3:0.1:3);
Z = griddata(x, y, z, X, Y, 'cubic');
mesh(X, Y, Z);
hold on
plot3(x, y, z, '.', 'MarkerSize', 16);
hold of
axis tight

% 100

MATLAB

4-1

4-12 plotxyz09.m

6
4
2
0
-2

2
0
-2

-3

-2

-1

MATLAB

4-1

mesh, ezmesh

meshc, ezmeshc

meshz

surf, ezsurf

surfc, ezsurfc

surfl

MATLAB

4-1

plot3, ezplot3

surface

Surf

line3

Plot3

contour,
ezcontour

contour3

pcolor

MATLAB

4-1

ezmesh, ezsurf

ezmesh
ezsurf
4-13 plotxyz091.m
subplot(2,2,1);

ezmesh('sin(x)/x*sin(y)/y');

subplot(2,2,2);

ezsurf('sin(x*y)/(x*y)');

subplot(2,2,3);

ezmeshc('sin(x)/x*sin(y)/y');

subplot(2,2,4);

ezsurfc('sin(x*y)/(x*y)');

MATLAB

4-1

Quiz!

span=4*pi*[-1, 1];
ezplot('sin(x)/x', span);

Proof:
1.Taylor expansion
2.lHospitals rule
3 .

sin(x)/x
1
0.8
0.6
0.4
0.2
0

Quiz!

-0.2
-10

-5

0
x

sin x
lim
1
x 0
x

10

MATLAB

Result of ez-family
sin(x)/x sin(y)/y

sin(x y)/(x y)

0.5

0.5

-0.5

-0.5

-1

-1
5
0
y

-5

-6

-4

-2

0
y

-5

sin(x)/x sin(y)/y

0.5

0.5

-0.5

-0.5

-1

-1
5

-5

-6

-4

-2

sin(x y)/(x y)

-6

-4

-2
x

5
0
y

-5

-6

-4

-2
x

MATLAB

4-2

hidden of

MATLAB

hidden of
hidden on
4-14 plotxyz10.m
[x,y,z] = peaks;
mesh(x,y,z);
hidden of
axis tight

MATLAB

4-2

4-14 plotxyz10.m

-5
2
0
-2

-3

-2

-1

MATLAB

4-2

on/of

hidden
on/off

on off

zoom on/off

on off

rotate3d
on/off

on off

axis on/off

on off

box on/off

on off

hold on/off

on
off

more on/off

on off

echo on/off

on off

MATLAB

4-2

rotate3d on

3D
peaks
rotate3d on

MATLAB

4-2

Azimuth Elevation
z
y

Elevation

Azimuth

MATLAB

4-2

Azimuth =
0 Elevation = 90
Azimuth = -37.5 Elevation
= 30 view

peaks;4-15 plotxyz11.m
view([0,-30]);

MATLAB

4-2

4-15 plotxyz11.m
Peaks

8
6
4
2
-2

-4

-6
-3

-2

-1

0
x

-2

MATLAB

4-2

NaN

Quiz!

NaN nan Not


a Number
MATLAB NaN

4-16
plotxyz12.m
[X, Y, Z]
= peaks;
Z(10:20,10:20) = nan;
surf(X, Y, Z);
axis tight

% Z nan

MATLAB

4-2

4-16 plotxyz12.m

-5
2
0
-2

-3

-2

-1

MATLAB

4-3

colorbar

colorbar MATLAB

peaks
colorbar
Peaks

8
6
4

2
0
0
-5

-2
2
0

2
0

-4
-6

MATLAB

4-3

RGB

Red

Green

Blue

black

white

red

green

blue

yellow

magenta

cyan

gray

0.5

0.5

0.5

dark red

0.5

copper

0.62

0.4

aquamarine

0.49

0.83

Quiz!

MATLAB

4-3

colormap

MATLAB colormap
>> cm = colormap;
>> size(cm)
ans =

64

cm 643 MATLAB
cm
cm

colorbar cm

MATLAB

4-3

colormap


colormap
4-17 plotxyz13.m
peaks;
colormap(rand(64,3));
colorbar;

MATLAB

4-3

4-17 plotxyz13.m
Peaks

8
6
4

2
0
0
-5

-2
2
2

0
y

-2

0
-2

-4
-6

MATLAB

4-3

MATLAB

MATLAB

Change Color Map

colormap cool

4-18
plotxyz14.m
peaks;
colormap cool;

Result

Peaks

8
6
4

2
0
0

colorbar
-5

-2
2
2

0
y

-2

0
-2

-4
-6

MATLAB

Change Color Map

colormap hot

Result

peaks;

Peaks

8
6
4

colormap hot;
colorbar

2
0
0
-5

-2
2
2

0
y

-2

0
-2

-4
-6

MATLAB

colormap

peaks;

peaks;

colormap hsv

colormap hsv

colorbar

colorbar

colormap(colormap.^2)

colormap(colormap.^.5)

colormap(colormap.^2)

colormap(colormap.^.5)

colormap(colormap.^2)

colormap(colormap.^.5)

colormap(colormap.^2)

colormap(colormap.^.5)

MATLAB

4-3

surf mesh

surf mesh
4

4-19 plotxyz15.m
[X, Y, Z] = peaks;
surf(X, Y, Z, gradient(Z));
axis tight;
colormap hot
Quiz: How to compute gradient?

-5
2
0
-2

-3

-2

-1

MATLAB

4-3

surf mesh

4-20 plotxyz16.m
[X, Y, Z] = peaks;
surf(X, Y, Z, del2(Z));
axis tight;
colormap hot

-5
2
0
-2

-3

-2

-1

MATLAB

4-3

brighten

brighten

4-21 plotxyz17.m
colormap copper
subplot(3, 1, 1); rgbplot(colormap);
brighten(colormap, 0.5)
subplot(3, 1, 2); rgbplot(colormap);
brighten(colormap, -0.8)
subplot(3, 1, 3); rgbplot(colormap);

MATLAB

4-3

4-21 plotxyz17.m
1
0.5
0

10

20

30

40

50

60

70

10

20

30

40

50

60

70

10

20

30

40

50

60

70

1
0.5
0
1
0.5
0

MATLAB

4-3

True Color

MATLAB
Indexed Color

24
2^24

True Color
Quiz:
1.Definition of true and indexed color
2.Their strength and weakness

MATLAB

4-3

4-22 plotxyz18.m
Z = peaks(50);
C(:, :, 1) = rand(50);

% C(:,:,1) R Red

C(:, :, 2) = rand(50);

% C(:,:,2) G Green

C(:, :, 3) = rand(50);

% C(:,:,3) B Blue

surf(Z, C);
axis tight

MATLAB

4-3

shading

shading
peaks
shading interp

MATLAB

4-3

shading interp
Bilinear Interpolation

shading flat

shading
faceted

MATLAB

4-3

colormap shading

colormap shading

4-23 plotxyz19.m
surfl(peaks);
axis tight
colormap(pink);
shading interp

MATLAB

4-3

4-23 plotxyz19.m

You might also like