Introduction to Matlab for Engineers Instructor: Thai Nhan Math 111, Ohlone, Spring 2016 Introduction to Matlab for Engineers Ohlone, Spring 2016 1/19
Today s lecture 1. The subplot command 2. Logarithmic Plots 3. 3D Graphing Introduction to Matlab for Engineers Ohlone, Spring 2016 2/19
Subplot command Subplot is a method of dividing a single figure window into multiple parts. It uses the command subplot(m,n,p). This creates an m n grid of individual plots, with the specific graph shown in element p. figure(1); subplot(1,3,2);% Divides Figure 1 into 3 sections (1 row, 3 columns, position 2) plot(...); % Plot your graph in second element subplot(1,3,3); plot(...); % Plot your graph in third element Introduction to Matlab for Engineers Ohlone, Spring 2016 3/19
Logarithmic Plots We have learned the syntax plot(x,y) which generates a linear plot of vectors x and y. The x- and y-axes are divided into equally spaced intervals. Sometimes, values of a variable ranges over many orders of magnitude. So we may want to use a logarithmic scale on one or both of the axes. Logarithmic plots are useful for representing data that vary exponentially. Data can be graphed without compressing the smaller values. (Reading: Section 5.3.2, and Appendix B, Moore s textbook.) Introduction to Matlab for Engineers Ohlone, Spring 2016 4/19
Logarithmic Plots The Matlab commands for generating logarithmic plots of the vectors x and y: semilogx(x,y) a plot with a logarithmic scale (base 10) for x and a linear scale for y semilogy(x,y) a plot with a linear scale for x and a logarithmic scale (base 10) for y loglog(x,y) a plot with a logarithmic scale (base 10) for both x and y Table: Logarithmic Plots in Matlab. Introduction to Matlab for Engineers Ohlone, Spring 2016 5/19
Logarithmic Plots Download the m-file Lecture 10 loglog.m to see examples of semilog and loglog plots. Introduction to Matlab for Engineers Ohlone, Spring 2016 6/19
3D Graphing For common use, there are two types of three-dimensional graphs: curves (wires) and surfaces. Both curves and some types of surfaces require the use of parametric functions, material typically found in Calculus III. Download the m-file Lecture 10 3D plot.m to see examples for 3D plotting. (Reading: Section 5.4, Moore s textbook.) Introduction to Matlab for Engineers Ohlone, Spring 2016 7/19
3D Graphing: Curves A three-dimensional curve has one variable and three coordinates to plot the position of the points. This is called a parametric curve. The command used is plot3. For example, the following code plots the curve z(t) = (cos(t), sin(t), sin(6t)), t [0, 2π]: t=linspace(0,2*pi,64); plot3(cos(t),sin(t),sin(6*t)); Introduction to Matlab for Engineers Ohlone, Spring 2016 8/19
3D Graphing: Surfaces To create surfaces, Matlab uses a grid on the x-y plane. This is called a meshgrid. The surface is then defined in terms of this grid. The standard 3D plot command is called surf. For example: [x,y]=meshgrid(-2:0.1:2);% Create a rectangular grid in 2D surf(x,y,x.ˆ2+y.ˆ2); Introduction to Matlab for Engineers Ohlone, Spring 2016 9/19
3D Graphing: Surfaces Another 3D plot command is called mesh. Try the following and observe the difference. [x,y]=meshgrid(-2:0.1:2); mesh(x,y,x.ˆ2+y.ˆ2); Introduction to Matlab for Engineers Ohlone, Spring 2016 10/19
3D Graphing: Surfaces An alternative for surfaces is to show a surface with contour lines in the x-y plane. It uses the surfc (or meshc) command. [x,y]=meshgrid(-2:0.1:2); surfc(x,y,x.ˆ2+y.ˆ2); Introduction to Matlab for Engineers Ohlone, Spring 2016 11/19
3D Graphing: Surfaces An alternative for surfaces is to use two independent variables, say u and v, and then define the three position variables in terms of these variables. This is called a parametric surface. It still uses the surf command. [u,v]=meshgrid(0:0.1:2,0:0.1:5*pi); x=u.*cos(v); y=u.*sin(v); z=4-u.ˆ2+v.ˆ2; surf(x,y,z); Introduction to Matlab for Engineers Ohlone, Spring 2016 12/19
3D Graphing: Surfaces A nice use for parametric surfaces is to express 3 space dimensions and a fourth characteristic, say temperature. [u,v]=meshgrid(0:.1:4,0:.1:2*pi); x=u.*cos(v); y=u.*sin(v); z=4-u.ˆ2+v.ˆ2; t=4-(x-0.5).ˆ2-(y+0.75).ˆ2; surf(x,y,z,t); Introduction to Matlab for Engineers Ohlone, Spring 2016 13/19
Surf Options: Eliminate the Surface Mesh To eliminate the mesh on the surface, use the shading option. Learn more about shading, use doc or help features. [u,v]=meshgrid(0:.1:4,0:.1:2*pi); x=u.*cos(v); y=u.*sin(v); z=4-u.ˆ2+v.ˆ2; surf(x,y,z); shading interp Introduction to Matlab for Engineers Ohlone, Spring 2016 14/19
Surf Options: Colormap A colormap is a set of colors that are used for shading your surface. There are several built-in maps or you can create your own. The built-in colormaps are: lines, jet, hsv, hot, cool, spring, summer, autumn, winter. Introduction to Matlab for Engineers Ohlone, Spring 2016 15/19
Surf Options: Transparency When displaying multiple surfaces, it is often nice to have the surfaces transparent. This is done with the alpha command. [u,v]=meshgrid(-1.5:0.1:1.5,linspace(0,2*pi,64)); x=cos(v); y=sin(v); z=u; s1=surf(x,y,z); set(s1, FaceColor, Red, FaceAlpha,0.5); hold on; s2=surf(z,x,y); set(s2, FaceColor, Blue, FaceAlpha,0.5); axis ([-2 2-2 2-2 2]); Introduction to Matlab for Engineers Ohlone, Spring 2016 16/19
Comet in 2D The comet command will show a parametric 2-dimensional curve as it is being drawn. t=linspace(0,4*pi,400); x=(16-t).*cos(t); y=(16-t).*sin(t); comet(x,y,0.05); Introduction to Matlab for Engineers Ohlone, Spring 2016 17/19
Comet in 2D An astroid can be drawn by comet command as follows. t=linspace(0,2*pi,400); x=2*(cos(t)).ˆ3; y=2*(sin(t)).ˆ3; comet(x,y,0.05); Introduction to Matlab for Engineers Ohlone, Spring 2016 18/19
Comet in 3D The comet3 command will show a parametric 3-dimensional curve as it is being drawn t=linspace(0,4*pi,400); x=(16-t).*cos(t); y=(16-t).*sin(t); z=sin(4*t); comet3(x,y,z,0.05); Introduction to Matlab for Engineers Ohlone, Spring 2016 19/19