Introduction to Matlab Matlab Graphics 3 D Graphics
Introduction to Matlab: Matlab Graphics 3 D Graphics S. Awad, Ph. D. M. Corless, M. S. E. E. E. C. E. Department University of Michigan-Dearborn
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department 3 D Graphics Topics n n n Creating Mesh and Surface Plots Viewing a Pseudocolor Matrix Creating Contour Plots Adding a Color-Bar Interpolating 3 D Data 2
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Mesh and Surface Plots n n Useful for displaying functions of 2 variables or to visualize large matrices Given a function f(x, y) of two variables: Assume that: » x=[-1: 0. 2: 1]; y=[-1: 0. 2: 1]; We can plot the function z as a mesh or surface plot 3
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Calculating xx Height = Length of y Row = Vector x 4
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Generating xx in Matlab » x=[-1: 0. 5: 1]; » xx=ones(length(x), 1)*x xx = -1. 0000 -0. 5000 0 0 0. 5000 1. 0000 5
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Calculating yy Length = Length of y Column = y Transpose 6
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Generating yy in Matlab » y=[-1: 0. 5: 1]; » yy=y'*ones(1, length(y)) yy = -1. 0000 -0. 5000 0 0. 5000 1. 0000 7
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Find xx & yy Using Meshgrid » [xx, yy] = meshgrid(x, y) xx = -1. 0000 yy = -1. 0000 -0. 5000 0 0. 5000 1. 0000 -0. 5000 0 0 0. 5000 1. 0000 -1. 0000 -0. 5000 0 0. 5000 1. 0000 8
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Find z n Find z using element by element computations » z=xx. *exp(-xx. ^2 -yy. ^2) z = -0. 1353 -0. 2865 -0. 3679 -0. 2865 -0. 1353 -0. 1433 -0. 3033 -0. 3894 -0. 3033 -0. 1433 0 0 0. 1433 0. 3033 0. 3894 0. 3033 0. 1433 0. 1353 0. 2865 0. 3679 0. 2865 0. 1353 9
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Mesh Plot z » mesh(x, y, z) n Finer increments of x and y will smooth the surface 10
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Mesh for a Larger x & y n » » » » » Create a new mesh plot complete with labels x =[-2: 0. 2: 2]; y =[-2: 0. 2: 2]; [xx, yy]=meshgrid(x, y); z=xx. *exp(-xx. ^2 -yy. ^2); mesh(x, y, z) title('Larger Mesh'); xlabel('X Data'); ylabel('Y Data'); zlabel('z=xx. *exp(-xx. ^2 -yy. ^2)'); 11
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Surface Plot of z » surf(x, y, z) n Plots the colored parametric surface where color is proportional to surface height 12
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Pcolor » pcolor(x, y, z) n n pseudocolor or "checkerboard" plot of matrix The values of the elements specify the color in each cell of the plot 13
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Contour Plots n contour plot of matrix where z contains heights above a plane Number of Contour Lines » contour(x, y, z, 16); » title('Contour'); 14
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Displaying Contour Heights n To write the values identifying the heights: » cs=contour(x, y, z, 16); » clabel(cs); » title('Contour Heights'); 15
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Meshc Contour Plots n meshc is same as mesh except that a contour plot is drawn beneath the mesh » meshc(x, y, z) n Only works for surfaces defined on a rectangular grid 16
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Adding a Colorbar n colorbar adds a vertical line (color scale) to show the contour plots 17
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department 3 D Interpolation n Rough or scattered data can be interpolated using mesh n The original data is plotted as a mesh by: » » » » x=[-3: 3]; y=[1: 5]; [xx, yy]=meshgrid(x, y); z= xx + yy; mesh(x, y, z); title('Original Mesh Grid'); xlabel('X Data'); ylabel('y Data'); zlabel('z = xx + yy'); 18
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Mesh of Rough Data n Original Course Surface where: x = [-3: 3] y = [1: 5] z = xx+yy 19
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Interpolation of Data n The original mesh from the data is can be improved by creating interpolated values using griddata: » » » » » xi=[-3: 0. 5: 4]; % Interpolated x axis yi=[ 0: 0. 2: 5]; % Interpolated y axis [xxi, yyi]=meshgrid(xi, yi); % zi = interpolated values zi=griddata(xx, yy, z, xxi, yyi); mesh(xxi, yyi, zi); % Better surface title('Interpolated Mesh Grid'); xlabel('X Data'); ylabel('y Data'); zlabel('z = xx + yy'); 20
MATLAB Graphics: 3 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department 3 D Interpolation Plot n Interpolated Smooth Surface where: xi = [-3: 0. 5: 4] yi = [ 0: 0. 2: 5] 21
- Slides: 21