5_Advanced Plotting_Class_S2024_P (1)
5_Advanced Plotting_Class_S2024_P (1)
MATLAB
for Engineering Applications
Fourth Edition
Chapter 05
Advanced Plotting
Generating a plot
Example: The following MATLAB session plots 𝑦 = 0.4 1.8𝑥 𝑓𝑜𝑟 0 ≤× ≤ 52,
where 𝑦 represents the height of a rocket after launch, in miles, and 𝑥 is the
horizontal (downrange) distance in miles.
x = 0:0.1:52;
y = 0.4*sqrt(1.8*x);
plot(x,y);
xlabel('Distance (miles) ');
ylabel('Height (miles) ');
title('Rocket Height as a Function of Downrange Distance ');
The plot will appear in the Figure window. You can obtain a hard copy of the plot
in several ways:
1. Use the menu system. Select Print on the File menu in the Figure window.
Answer OK when you are prompted to continue the printing process.
2. Type print at the command line. This command sends the current plot
directly to the printer.
3. Save the plot to a file to be printed later or imported into another application
such as a word processor. You need to know something about graphics file
formats to use this file properly. See the subsection Exporting Figures.
The grid command displays gridlines at the tick marks corresponding to the
tick labels. Type grid on to add gridlines; type grid off to stop plotting
gridlines.
You can use the axis command to override the MATLAB selections for the axis
limits. The basic syntax is axis([xmin xmax ymin ymax]). This command
sets the scaling for the x- and y-axes to the minimum and maximum values
indicated.
x= [1.0:.01:2.0];
y = cos(tan(x) - tan(sin(x)));
plot(x,y)
The fplot function chooses a small enough spacing to display the function’s full
behavior.
𝑓 = cos(tan 𝑥 ) − tan(sin 𝑥 )
To plot the polynomial 3×5 + 2×4 − 100×3 + 2×2 −7× + 90 over the range − 6 ≤
× ≤ 6 with a spacing of 0.01, you type
x = -6:0.01:6;
p = [3,2,-100,2,-7,90];
plot(x,polyval(p,x));
xlabel('x');
ylabel('p');
title('Plotting Polynomial');
Q2- Use the fplot command to plot and investigate the function
tan cos 𝑥 − 𝑠𝑖𝑛(𝑡𝑎𝑛 𝑥)
𝑓𝑜𝑟 0 ≤ 𝑥 ≤ 2𝜋. How many values of x are needed to obtain the same plot using the plot
command?
Hint: fplot can be used to count the number of sample points
Q2- Use the fplot command to plot and investigate the function
tan cos 𝑥 − 𝑠𝑖𝑛(𝑡𝑎𝑛 𝑥)
𝑓𝑜𝑟 0 ≤ 𝑥 ≤ 2𝜋. How many values of x are needed to obtain the same plot using the plot
command?
Hint: fplot can be used to count the number of sample points
Plotting Functions
Other useful plotting functions are title and gtext. These functions place text
on the plot. Both accept text within parentheses and single quotes, as with the
xlabel function.
The title function places the text at the top of the plot.
The gtext function places the text at the point on the plot where the cursor is
located when you click the left mouse button.
x = 0:0.01:2*pi;
y = sin(x);
plot(x,y),xlabel('x'),ylabel('y'), gtext('sin(x)');
title(‘My Plot')
Subplots 1
You can use the subplot command to obtain several smaller “subplots” in the
same figure. The syntax is subplot(m,n,p). This command divides the
Figure window into an array of rectangular panes with m rows and n columns.
The variable p tells MATLAB to place the output of the plot command
following the subplot command into the pth pane.
x = 0:0.01:5;
y = exp(-1.2*x).*sin(10*x+5);
subplot(1,2,1)
plot(x,y),xlabel('x'),ylabel('y')
,axis([0 5 -1 1])
x = -6:0.01:6;
y = abs(x.^3-100);
subplot(1,2,2)
plot(x,y),xlabel('x'),ylabel('y')
,axis([-6 6 0 350])
Subplots 2
The following script file created a figure, which shows the plots of the functions
𝑦 = 𝑒 −1.2𝑥 sin(10𝑥 + 5) for 0 ≤ 𝑥 ≤ 5 and 𝑦 = |𝑥 3 − 100| for −6 ≤ 𝑥 ≤ 6.
x = 0:0.01:5;
y = exp(-1.2*x).*sin(10*x+5);
subplot(3,2,2)
plot(x,y),axis([0 5 -1 1])
x = -6:0.01:6;
y = abs(x.^3-100);
subplot(3,2,3)
plot(x,y),axis([-6 6 0 350])
y= x.^2;
subplot(3,2,6)
plot(x,y),axis([-6 6 -5 50])
To plot y versus x with a solid line and u versus v with a dashed line, type
plot(x,y,u,v,‘--’), where the symbols ‘--’ represent a dashed line.
To plot y versus x with asterisks (*) connected with a dotted line, you must
plot the data twice by typing plot(x,y,‘*’,x,y,‘:’).
x=1:1:10;
y=sqrt(x);
plot(x,y,'*',x,y,':')
To plot y versus x with green asterisks () connected with a red dashed line,
you must plot the data twice by typing plot(x,y,‘g*’,x,y,‘r--’).
x=1:1:10;
y=sqrt(x);
plot(x,y, 'g*',x,y, 'r--')
†Other data markers are available. Search for “markers” in MATLAB help.
x = 0:0.01:2;
y = sinh(x);z = tanh(x);
plot(x,y,x,z,'r--'),xlabel('x'),...
ylabel('Hyperbolic Sine and Tangent'),
legend('sinh(x)','tanh(x)')
x=0:.01:2*pi;
y=cos(x);
plot(x,y);
hold
y=sin(x);
plot(x,y);
Q3- Pick a suitable spacing for 𝑡 = 0: 0.01: 8; 𝑎𝑛𝑑 𝑣 = −8: 0.01: 8; and then use the subplot
command to plot the following functions
Q4- Plot the following two data sets on the same plot. For each set, x = 0, 1, 2, 3, 4, 5. Use a
different data marker for each set. Connect the markers for the first set with solid lines.
Connect the markers for the second set with dashed lines. Use a legend, and label the plot axes
appropriately. The first set is y = 11, 13, 8, 7, 5, 9. The second set is y = 2, 4, 5, 3, 2, 4.
Logarithmic Plots 1
Logarithmic Plots 4
MATLAB has three commands for generating plots having log scales. The
appropriate command depends on which axis must have a log scale.
2. Use the semilogx(x,y) command to have the x scale logarithmic and the
y scale rectilinear.
3. Use the semilogy(x,y) command to have the y scale logarithmic and the
x scale rectilinear.
Polarplot Plots
𝒑𝒐𝒍𝒂𝒓𝒑𝒐𝒍𝒂𝒓 𝜽, 𝝆 :plot using polar coordinates , in radians, versus the radius 𝜌.
x = 0:0.01:2*pi;
y = sin(x);
polarplot(x,y), title('Polar
Plot of y = Sin(x)')
Q5- The spiral of Archimedes is described by the polar coordinates (𝜃, 𝑟), where 𝑟 = 𝑎𝜃.
Obtain a polar plot of this spiral for 0 ≤ 𝜃 ≤ 4𝜋, with the parameter 𝑎 = 2.
Q6- Plot the function 𝑦 = 8𝑥 3 𝑓𝑜𝑟 − 1 ≤ 𝑥 ≤ 1 with a tick spacing of 0.25 on the 𝑥 axis
and 2 on the y axis.
Command Description
bar(x,y) Creates a bar chart of y versus x.
plotyy(x1,y1,x2,y2) Produces a plot with two y-axes, y1 on the left and y2 on
the right.
polar(theta,r,’type’) Produces a polar plot from the polar coordinates theta
and r, using the line type, data marker, and colors
specified in the string type.
𝒙𝟐
cos 𝒙 ≈ 𝟏 −
𝟐
errorbar(x,y,err) plots y versus x and draws
a vertical error bar at each data point.
MATLAB provides the function fimplicit(f) to plot the implicit function defined by the
equation f(x,y) = 0 over the default interval [−5 5] for x and y. For example, to plot the
hyperbola defined by x² − y² − 1 = 0 over the default interval of [−5 5], you type
If the limits for x are [−2 2] and the limits for y are [−4 4], you would type
A live script is an interactive document that contains output, including graphics, along with
the code that produced them, together in a single interactive environment called the Live
Editor. You can also include formatted text, images, hyperlinks, and equations to produce
an interactive shareable narrative. Live scripts are stored in a file with the extension .mlx.
You can convert the scripts to HTML or PDF files for publication.
x=0:0.01:6;
p=[3,2,-100,2,-7,90];
plot(x,polyval(p,x)), xlabel ('x'),
ylabel ('p')
The following program uses the plot3 function to generate the spiral curve.
𝑥 = 𝑒 − 0.05𝑡 sin 𝑡 , 𝑦 = 𝑒 − 0.05𝑡 cos 𝑡 , 𝑧 = 𝑡
t = 0:pi/50:10*pi;
plot3(exp(-0.05*t).*sin(t),exp(-0.05*t).*cos(t),t),
xlabel('x'),ylabel('y'),zlabel('z'),grid
Surface Plots
mesh command creates a three-dimensional surface plot that has solid edge colors and
no face
The following session shows how to generate the surface plot of the function
2 2
𝑧 = 𝑥𝑒 −[ 𝑥−𝑦 +𝑦 2 ] for −2 ≤ x ≤ 2 and −2 ≤ y ≤ 2, with a spacing of 0.1.
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-((X-Y.^2).^2+Y.^2));
mesh(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z');
2 2
A plot of the surface 𝑧 = 𝑥𝑒 −[ 𝑥−𝑦 +𝑦 2 ] created with the mesh function.
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-((X- Y.^2).^2+Y.^2));
contour(X,Y,Z),xlabel('x'),ylabel('y');
2 2
A contour plot of the surface 𝑧 = 𝑥𝑒 −[ 𝑥−𝑦 +𝑦 2 ]
You can specify the interval with the syntax 𝑓𝑖𝑚𝑝𝑙𝑖𝑐𝑖𝑡3(𝑓, 𝑖𝑛𝑡𝑒𝑟𝑣𝑎𝑙).
For example, to plot the upper half of the hyperboloid 𝑥 2 + 𝑦 2 − 𝑧 2 = 0 you specify
the interval as for z as [0 5], and for x and y, use the default interval [−5 5], as
follows.
f = @(x,y,z) x.^2 + y.^2 - z.^2;
fimplicit3(f) ;
interval = [-5 5 -5 5 0 5];
Function Description
surf(x,y,z) Creates a shaded 3D mesh surface plot.
surfc(x,y,z) Same as surf but draws contours under the
surface.
waterfall(x,y,z) Same as mesh but draws mesh lines in one
direction only.
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-(X.^2+Y.^2));
subplot(2,2,1);
xlabel('x')
ylabel('y') surf(X,Y,Z) surfc(X,Y,Z)
surf(X,Y,Z)
subplot(2,2,2);
surfc(X,Y,Z)
subplot(2,2,3);
waterfall(X,Y,Z)
subplot(2,2,4);
meshc(X,Y,Z);
waterfall(X,Y,Z) meshc(X,Y,Z);
Q7- Use plot3 and fplot3 to plot the 3-D line plot described by 𝑥 = sin(𝑡), 𝑦 = cos(𝑡), 𝑧 =
ln(𝑡) for 0 < 𝑡 < 30.
fplot3 Plot 3-D parametric curve
Example: fplot3(@(t) cos(t) ,@(t)myfunc(t),@(t) sqrt(t), [-pi,pi], '--*‘)
Q8 Use mesh, fmesh, contour, and fcontour to create a surface plot and a contour plot of the
function 𝑧 = (𝑥 − 2)2 +2𝑥𝑦 + 𝑦 2 .
Q9- Use the fimplicit3 function to create a surface plot of the function
𝑥2 − 𝑦2 − 𝑧2 = 0