2014_11_07_lecture_33

advertisement
EGR 115 Introduction to Computing for
Engineers
Complex Numbers & 3D Plots – Part 2
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Lecture Outline
• Multi-Dimensional Arrays and 3D Plots
 Multi-Dimensional arrays
 A Variety of 3D plotting functions
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 2 of 14
Multi-Dimensional arrays
• A row/col vector => A 1-dimensional array
• A Matrix => A 2-dimensional array
% A matrix (2-dim array)
a = [1 2 3 4;
5 6 7 8];
 Size of (a) is: 2 X 4
• A Multi-Dimensional array:
% A multi-dimensional array
b(:,:,1) = a;
b(:,:,2) = 2*a;
b(:,:,3) = [ -1 -2 -3 4;
0
3 2 9];
 Size of (b) is: 2 X 4 X 3
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 3 of 14
Multi-Dimensional arrays
• What is the value of:
 b(2,2,2)?
% A matrix (2-dim array)
a = [1 2 3 4;
5 6 7 8];
% A multi-dim array
b(:,:,1) = a;
b(:,:,2) = 2*a;
b(:,:,3) = [ -1 -2 -3 4;
0
3 2 9];
 b(:,:,1) ?
=a
 b(1,2,3)?
• Can provide a convenient way to store an array of
matrices
 E.g., rand(3,3,6); or zeros(5,6,20); or ones(1,2,4);
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 4 of 14
Three Dimensional Plots
• One of the key features of MATLAB is the ease of
which plots/figures are produced
• This is especially true for three dimensional plots
which are especially convenient in the MATLAB
environment
• Three dimensional plots can be categorized into:





Line plots -> plot3
Mesh plots -> mesh
Surface plots -> surf
Contour plots -> contour
Additional 3D plots
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 5 of 14
Three Dimensional Plots
The Plot3 Function
EGR115: A 3D Line Plot
• The plot3() function
10
z-axis
%% A Plot3 Example
z = 0:.1:10;
x = exp(-0.2*z) .* cos(2*z);
y = exp(-0.2*z) .* sin(2*z);
plot3(x, y, z, 'r', 'LineWidth', 3)
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis')
title('EGR115: A 3D Line Plot')
grid
8
6
4
2
0
1
0.5
1
0.5
0
0
-0.5
y-axis
-0.5
-1
-1
x-axis
• Use the Rotate 3D
icon in the plot window
MATLAB Code
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 6 of 14
Three Dimensional Plots
The Plot3 Function
• An In-class example:
 Plot the following coordinates of a UAV in flight:
o
x = cost(t), y = sin(t), and z = sin(5*t)
 For 0 ≤ t ≤ 
1
0.5
0
-0.5
-1
1
1
0.5
0.5
0
-0.5
0
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
-1
Slide 7 of 14
Three Dimensional Plots
The Mesh Plot Function
• Creates a mesh or wireframe plot of arguments
 Effectively plots a matrix where x/y coordinates are row/col
indices and z is the value stored at the row/col address.
o
E.g., mesh(z)
%% A Mesh 3D Example
my_grid = [ 0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 2 2 2 1 0
0 1 2 3 2 1 0
0 1 2 2 2 1 0
0 1 1 1 1 1 0
0 0 0 0 0 0 0];
figure,
mesh(my_grid,'LineWidth',2);
3
2.5
2
1.5
1
0.5
0
8
6
8
6
4
4
2
2
0
0
MATLAB Code
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 8 of 14
Three Dimensional Plots
The Mesh Plot Function
• A more sophisticated example: The 3D sinc function
 E.g., mesh(x, y, z)
Look at: mesh(X) and mesh(Y)
The 3D Sinc Function
1
0.5
z-axis
% The 3D Sinc Function
[X,Y] = meshgrid(-10:.5:10);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure,
mesh(X,Y,Z)
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis')
title('The 3D Sinc Function')
0
-0.5
10
5
10
5
0
0
-5
MATLAB Code
y-axis
-5
-10
-10
x-axis
NOTE: Now x, y, and z are all matrices of the same size
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 9 of 14
Three Dimensional Plots
The Surface Plot
• A more sophisticated example: The 3D sinc function
 E.g., surf(x, y, z)
The 3D Sinc Function
1
0.5
z-axis
% The 3D Sinc Function
[X,Y] = meshgrid(-10:.5:10);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure,
surf(X,Y,Z)
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis')
title('The 3D Sinc Function')
0
-0.5
10
5
10
5
0
0
-5
y-axis
NOTE: Very similar to the mesh plot
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
-5
-10
-10
x-axis
MATLAB Code
Slide 10 of 14
Three Dimensional Plots
The
• A more sophisticated example: The 3D sinc function
 E.g., contour(x, y, z)
The 3D Sinc Function
10
8
6
4
2
y-axis
% The 3D Sinc Function
[X,Y] = meshgrid(-10:.5:10);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure,
contour(X,Y,Z)
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis')
title('The 3D Sinc Function')
0
-2
-4
-6
-8
-10
-10
-8
-6
-4
Think of topo-map height contour lines
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
-2
0
x-axis
2
4
6
8
10
MATLAB Code
Slide 11 of 14
Three Dimensional Plots
Other 3D Plotting Functions
• More 3D Plotting Functions:
 stem3 – height at an x, y coordinate
• Plot the following coordinates of a UAV in flight:
 x = cost(t), y = sin(t), and z = sin(5*t)
o
For 0 ≤ t ≤ 
% The 3D stem3 Function
t = 0:0.05:pi;
x = cos(t);
y = sin(t);
z = sin(5*t);
figure,
stem3(x, y, z, 'r')
1
0.5
0
-0.5
-1
1
1
0.5
0.5
MATLAB Code
Friday 07 Nov 2014
0
-0.5
0
EGR 115 Introduction to Computing for Engineers
-1
Slide 12 of 14
Three Dimensional Plots
Other 3D Plotting Functions
• Another stem3 example
3
2.5
% The 3D stem3 Function
my_grid = [ 0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 2 2 2 1 0
0 1 2 3 2 1 0
0 1 2 2 2 1 0
0 1 1 1 1 1 0
0 0 0 0 0 0 0];
figure,
stem3(my_grid,'LineWidth',2);
2
1.5
1
0.5
0
8
6
8
6
4
4
2
2
0
0
MATLAB Code
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 13 of 14
Next Lecture
• More 3D Plotting
Friday 07 Nov 2014
EGR 115 Introduction to Computing for Engineers
Slide 14 of 14
Download