MATLAB 2D and 3D Plotting 5 — Questions and Answers
Question 1: Which MATLAB function creates a quiver (vector field) plot?
- vectorplot
- quiver (Correct answer)
- arrow
- fieldplot
Correct answer: quiver
quiver(x,y,u,v) draws arrows at (x,y) with direction and magnitude given by (u,v), used to visualize 2D vector fields.
Question 2: In MATLAB, how do you make a plot line semi-transparent with 50% opacity?
- plot(x,y,'Alpha',0.5)
- Set the line handle's 'Color' property to include an alpha value: [r g b 0.5] (Correct answer)
- set(gca,'Transparency',0.5)
- plot(x,y,'Opacity',0.5)
Correct answer: Set the line handle's 'Color' property to include an alpha value: [r g b 0.5]
In MATLAB, transparency for line objects is set via the Color property using a 4-element [R G B Alpha] vector where Alpha ranges from 0 (transparent) to 1 (opaque).
Question 3: What does the 'shading interp' command do to a surface plot in MATLAB?
- Removes the mesh lines and applies smooth color interpolation across each face (Correct answer)
- Applies flat uniform shading to each mesh cell
- Adds a lighting effect to the surface
- Interpolates missing z data across the surface
Correct answer: Removes the mesh lines and applies smooth color interpolation across each face
'shading interp' removes the mesh edge lines and smoothly interpolates colors across each face using vertex color values.
Question 4: Which function in MATLAB plots a filled 2D polygon given vertex coordinates?
- patch
- fill (Correct answer)
- polygon
- area
Correct answer: fill
fill(x,y,c) creates a filled 2D polygon by connecting the x and y coordinates and filling with color c.
Question 5: When using 'surf(X,Y,Z)' in MATLAB, what must be true about X, Y, and Z?
- They must all be 1D vectors of the same length
- X and Y must be 2D grids (from meshgrid) and Z must be a 2D matrix of the same size (Correct answer)
- Z must be a flat plane (constant values)
- X, Y, Z must each have at least 100 elements
Correct answer: X and Y must be 2D grids (from meshgrid) and Z must be a 2D matrix of the same size
surf() requires X and Y to be 2D coordinate grids (typically from meshgrid) and Z to be a 2D matrix of the same size giving the height at each grid point.
Question 6: What is the purpose of 'view(az, el)' in MATLAB 3D plots?
- Sets the camera position using azimuth and elevation angles (Correct answer)
- Zooms the view to fit the data
- Switches between 2D and 3D display modes
- Sets the axis ranges based on the viewable window
Correct answer: Sets the camera position using azimuth and elevation angles
view(az, el) sets the viewpoint for 3D plots by specifying the azimuth (horizontal rotation) and elevation (vertical angle) in degrees.
Question 7: Which MATLAB command would you use to display a 2D matrix as an image with scaled color mapping?
- imshow
- imagesc (Correct answer)
- matplot
- colorimage
Correct answer: imagesc
imagesc(C) displays matrix C as an image with the colormap scaled to span the full range of values in C.
Which MATLAB function creates a quiver (vector field) plot?