Beruflich Dokumente
Kultur Dokumente
Matlab Graphics
3D graph
y In this section we will focus on the basic
MATLAB instructions
i t ti for
f plotting
l tti a 3-D
3 D graph
h
and controlling its appearance.
y Ref: help graph3d
1
meshgrid
• meshgrid generates x and y grid coordinates of a
rectangular
g mesh.
• Example:
x=linspace(-5,5,30);
y=linspace(-2,2,20);
[X,Y]=meshgrid(x,y);
will
ill produce
d two
t 5 by
b 6 matrices
t i xii and
d yii
which contain the x and y coords, respectively, of a 5 by 6
rectangular grid.
xi and yi can now be used as templates when interpolating randomly
scattered data into a uniform grid for contour and surface plotting
routines as shown by the next slide.
plot3
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
1
plot3(X,Y,Z); 0.5
-0.5
-1
2
1 5
0
0
-1
-2 -5
2
mesh
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
mesh(X,Y,Z);
meshc
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
meshc(X,Y,Z);
3
meshz
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
meshz(X,Y,Z);
waterfall
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y); 1
waterfall(X,Y,Z); 0.5
-0.5
-1
2
1 5
0
0
-1
-2 -5
4
surf
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
surf(X,Y,Z);
surl
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
surfl(X,Y,Z);
10
5
surfnorm
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
surfnorm(X,Y,Z);
11
shading
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
surf(X,Y,Z);
shading flat;
12
6
shading
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
surf(X,Y,Z);
shading interp;
13
pcolour
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
Z=sin(X).*cos(Y);
pcolor(X,Y,Z);
shading interp;
14
7
contour
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y); 2 0.8
contour(X,Y,Z); 1 0.4
0 0
-0.5 -0.2
-1 -0.4
-1.5 -0.6
-2 -0.8
-5 -4 -3 -2 -1 0 1 2 3 4 5
15
contour
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y); 2
0.8
Z=sin(X).*cos(Y); 1.5
0.6
contour(X,Y,Z,30); 1
0.4
colorbar; 0.5
0.2
0 0
-0.2
-0.5
-0.4
-1
-0.6
-1.5
-0.8
-2
-5 -4 -3 -2 -1 0 1 2 3 4 5
16
8
contourf
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y); 2
0.8
Z=sin(X).*cos(Y); 1.5
0.6
contourf(X,Y,Z,30); 1
0.4
0
0
-0.2
-0.5
-0.4
-1
-0.6
-1.5
-0.8
-2
-5 -4 -3 -2 -1 0 1 2 3 4 5
17
contoure3
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y);
0.8
Z=sin(X).*cos(Y); 1 0.6
contour3(X,Y,Z,30); 0.5
0.4
colorbar; 0
0.2
-0.5 -0.2
-0.4
-1
2
1 5 -0.6
0
0 -0.8
-1
-2 -5
18
9
quiver
x=linspace(-5,5,30);
y=linspace(-2,2,30);
li ( )
[X,Y]=meshgrid(x,y); 40
Z=sin(X).*cos(Y); 35
quiver(X,Y,Z); 30
25
20
15
10
-5
0 5 10 15 20 25 30 35
19
ezmesh, ezsurf
y Example:
⎧ x = 3cos u sin v
⎪
⎨ y = 2 cos u cos v
⎪ z = sin u
⎩
ezmesh('3*cos(u)*sin(v)','2*cos(u)*cos(v)','sin(u)',[-2*pi,2*pi,-2*pi,2*pi])
20
10
ezmesh, ezsurf
y Example:
ezsurf('u*sin(v)','u*cos(v)','4*v',[-2*pi,2*pi,-2*pi,2*pi])
21
ezmesh, ezsurf
y Example:
ezsurf('u*sin(v)','u*cos(v)','u',[-2*pi,2*pi,-2*pi,2*pi])
22
11
ezsurf
ezsurf('sin(sqrt(x^2+y^2))/sqrt(x^2+y^2)',[-6*pi,6*pi])
ezsurf('x*exp(-x^2
ezsurf( x exp( x 2 - yy^2)')
2) )
ezsurf('x*(y^2)/(x^2 + y^4)')
ezsurf('x*y','circ')
ezsurf('real(atan(x + i*y))')
ezsurf('exp(-x)*cos(t)',[-4*pi,4*pi,-2,2])
ezsurf('s*cos(t)','s*sin(t)','t')
ezsurf('s*cos(t)','s*sin(t)','s')
ezsurf('exp(
ezsurf( exp(-s)*cos(t)
s)*cos(t)','exp(
exp(-s)*sin(t)
s)*sin(t)','t't ,[0,8,0,4*pi])
[0 8 0 4*pi])
ezsurf('cos(s)*cos(t)','cos(s)*sin(t)','sin(s)',[0,pi/2,0,3*pi/2])
ezsurf('(s-sin(s))*cos(t)','(1-cos(s))*sin(t)','s',[-2*pi,2*pi])
23
Special Plots
•Sphere
p •Cylinder
•Bar
B Plot
Pl t •Pie
Pi Chart
Ch t
24
12
Sphere Plot
[X,Y,Z]=sphere(20);
surf(X,Y,Z);
xlabel('x'); ylabel('y'); zlabel('z');
axis square
title('Plot of a Sphere')
25
Sphere Plot
sphere(50)
shading interp
axis off
light
pause(2)
lighting none
pause(2)
lighting gouraud
pause(2)
lighting phong
pause(2)
colormap (gray) 26
13
Cylinder Plot
t=linspace(0,pi,20);
r=1+sin(t);
[X,Y,Z]=cylinder(r);
surf(X,Y,Z)
axis square
title('Plot of a Cylinder')
27
Cylinder Plot
cylinder(50)
shading interp
axis off
light
pause(2)
lighting none
pause(2)
lighting gouraud
pause(2)
lighting phong
pause(2)
colormap (gray) 28
14
Bar Plot
Y=[1 6 7 ; 2 6 7; 3 5 7; 4 5 7; 3 4 7; 2 3 6; 1 2 7];
bar(Y)
pause(5)
bar3(Y)
title('3D Bar Plot')
29
Pie ploting
x=[2,4,6,8];
pie(x)
i ( )
pause(5)
pie3(x)
pause(5)
pie3(x,[1,1,1,1])
is used to specify slices that should be pulled out from the pie
30
15
The View Command
view(az,el)
31
View Command Example
x=-3:.2:3;
y=-3:.2:3;
[X,Y]=meshgrid(x,y);
Z=1.8.^(-1.5*sqrt(X.^2+Y.^2)).*cos(0.5*Y).*sin(X);
surf(X,Y,Z)
xlabel('x'); ylabel('y'); zlabel('z');
32
16
View Command Example
view(0,25) view(30,25)
view(60 25)
view(60,25) view(90,25)
33
View Command Example
view(0,0) view(90,90)
34
17
Hidden Line Removal
y Hidden lines:
y ON: shows white inside mesh
y OFF: shows transparent mesh
[X,Y,Z] = sphere(12);
subplot(1,2,1);
mesh(X,Y,Z), title('Opaque');
hidden on;
axis square off;
subplot(1,2,2);
mesh(X,Y,Z),title('Transparent');
hidden off;
axis square off;
35
Peaks
The peaks(100)
function creates
a 100x100 array
of values. Since
this is a plot of
a single
variable, we get
100 different
line plots
36
18
The peaks( ) Function
y We'll use peaks( ) to create a z=f(x,y) function that is
interesting and shows off the 3‐D plotting
y see help peaks for more info on this function
[x,y,z]=peaks(30);
mesh(x,y,z)
xlabel('x-axis')
( )
ylabel('y-axis')
zlabel('z-axis')
37
The peaks( ) Function
y We'll use peaks( ) to create a z=f(x,y) function that is
interesting and shows off the 3‐D plotting
y see help peaks for more info on this function
3
6
[x,y,z]=peaks(30); 2
4
contourf(x,y,z,10) 1
2
y-axis
colorbar 0 0
xlabel('x-axis') -1 -2
-4
-2
ylabel('y-axis')
-6
-3
-3 -2 -1 0 1 2 3
x-axis
38
19
The peaks( ) Function
[xx,yy,zz] = peaks(50);
[C,h]
[C,h]=contour(xx,yy,zz,5)
contour(xx,yy,zz,5) % 5 lines
clabel(C,h)
3
0.75885
87 1
3.1
% C is a
85
2 5.6153
0.758
variable
0.7
87 1
5.6
3.
58
15
18
3.1
3
85
71
1 representing
6 94
-1 .6
the contour
5
0.
88
75
3.1871
75 plot
88
0 0.
5
-1 .6
0.75
5
69
58 8
4
8 85
0.7
71
-1
3.
18
-1 .6
69
% h is the
-4 .0 4
0.758 85
9 76
"handle"
-1 .6
-2
4
9 76
6 94
-4 .0 69
.6
-1
-3
-3 -2 -1 0 1 2 3
39
Exercises
40
20
Exercises
41
21