>> MATLAB Programming
Two-Dimensional (x-y) Plots
5.1 Create plots of the following functions from x 0 to 10.
(a) y ex
(b) y sin1x2
(c) y ax2 bx c, where a 5, b 2, and c 4
(d) y 2x
Each of your plots should include a title, an x-axis label, a y-axis label, and a grid.
5.2 Plot the following set of data:
y [12, 14, 12, 22, 8, 94]
Allow MATLAB® to use the matrix index number as the parameter for the x-axis.
5.3 Plot the following functions on the same graph for x values from - p to p, selecting spacing to create a smooth plot:
y2 sin1 2x2
y3 sin1 3x2
(Hint: Recall that the appropriate MATLAB® syntax for 2x is 2 * x.)
5.4 Adjust the plot created in Problem 5.3 so that:
• Line 1 is red and dashed.
• Line 2 is blue and solid.
• Line 3 is green and dotted.
Do not include markers on any of the graphs. In general, markers are included only on plots of measured data, not for calculated values.
5.5 Adjust the plot created in Problem 5.4 so that the x- axis goes from 6 to 6.
• Add a legend.
• Add a text box describing the plots.
x-y Plotting with Projectiles
Use the following information in Problems 5.6 through 5.10:
The distance a projectile travels when fired at an angle u is a function of time and can be divided into horizontal and vertical distances according to the formulas
horizontal1t2 tV0 cos1u2
and vertical 1t2 tV0 sin1u2 21gt2
horizontal = distance traveled in the x direction
vertical = distance traveled in the y direction
V0 = initial velocity
g = acceleration due to gravity, 9.8 m > s2
t = time, s.
5.6 Suppose the projectile just described is fired at an initial velocity of 100 m/s and a launch angle of p > 4 145 2. Find the distance traveled both horizon-tally and vertically (in the x and y directions) for times from 0 to 20 s with a spacing of .01 seconds.
(a) Graph horizontal distance versus time.
(b) In a new figure window, plot vertical distance versus time (with time on the x-axis).
Don't forget a title and labels.
5.7 In a new figure window, plot horizontal distance on the x-axis and vertical distance on the y-axis.
5.8 Replot horizontal distance on the x-axis and vertical distance on the y-axis using the comet function. If the plot draws too quickly or too slowly on your computer, adjust the number of time values used in your calculations.
A tensile testing machine is used to measure stress and strain and to characterize the behavior of materials as they are deformed.
5.9 Calculate three new vectors for each of the vertical 1v , v , v 2 and hori-
1 21 2 3 p > p > zontal h1, h2, h3 distances traveled, assuming launch angles of 2, 4, and p > 6.
• In a new figure window, graph horizontal distance on the x-axis and verti-cal distance on the y-axis, for all three cases. (You'll have three lines.)
• Make one line solid, one dashed, and one dotted. Add a legend to iden-tify which line is which.
5.10 Re-create the plot from Problem 5.9. This time, create a matrix theta of the three angles, p > 2, p > 4, and p > 6. Use the meshgrid function to cre-ate a mesh of theta and the time vector (t). Then use the two new meshed variables you create to recalculate vertical distance (v) and horizontal dis-tance (h) traveled. Each of your results should be a 2001 3 matrix. Use the plot command to plot h on the x-axis and v on the y-axis.
5.11 A tensile testing machine such as the one shown in Figure P5.11 is used to determine the behavior of materials as they are deformed. In the typical test, a specimen is stretched at a steady rate. The force (load) required to deform the material is measured, as is the resulting deformation. An exam-ple set of data measured in one such test is shown in Table P5.11.
can be used to calculate the applied stress and the resulting strain with the following equations.
(a) Use the provided data to calculate the stress and the corresponding strain for each data pair. The tested sample was a rod of diameter 0.505 in., so you'll need to find the cross-sectional area to use in your calculations.
(b) Create an x-y plot with strain on the x- axis and stress on the y- axis. Connect the data points with a solid black line, and use circles to mark each data point.
(c) Add a title and appropriate axis labels.
(d) The point where the graph changes from a straight line with a steep slope to a flattened curve is called the yield stress or yield point. This corresponds to a significant change in the material behavior. Before the yield point the material is elastic, returning to its original shape if the load is removed-much like a rubber band. Once the material has been deformed past the yield point, the change in shape becomes perma-nent and is called plastic deformation. Use a text box to mark the yield point on your graph.
5.12 In the previous chapter, the accumulated cyclone energy index (ACE) was introduced (Problem 4.5). Use that data to solve the following problems. It may also be available to you as an EXCEL spreadsheet, named ace_data. xlsx.
(a) Create an x-y plot of the year (on the x-axis) versus the ACE index val-ues (on the y-axis.)
(b) Calculate the mean ACE value, and use it to draw the mean value on your graph. (Hint: You just need two points, one at the first year and another at the final year).
(c) Use the filter function to find a running weighted average of the ACE data, over a 10-year period, using the following syntax, assuming you have named the data extracted from the ACE column, ace.
running_avg_ace = filter(ones(1,10)/10,1,ace);
Create a plot of the year (on the x-axis) versus the ACE value and the weighted average on the y- axis. (You will have two lines.) From your graph, do you think hurricane intensity is increasing? You can find out more about the filter function by searching the help documentation.
5.13 In Problem 5.1, you created four plots. Combine these into one figure with four subwindows, using the subplot function of MATLAB®.
5.14 In Problems 5.6, 5.7, and 5.9, you created a total of four plots. Combine these into one figure with four subwindows, using the subplot function of MATLAB®.
5.15 Create a vector of angles from 0 to 2p. Use the polar plotting function to create graphs of the functions that follow. Remember, polar plots expect the angle and the radius as the two inputs to the polar function. Use the subplot function to put all four of your graphs in the same figure.
(a) r sin2 1u) cos2 1u2
(b) r sin1u2
(c) r eu > 5
(d) r sinh1u2
5.16 In Practice Exercises 5.3, you created a number of interesting shapes in polar coordinates. Use those exercises as a help in creating the following figures:
(a) Create a "flower" with three petals.
(b) Overlay your figure with eight additional petals, half the size of the three original ones.
(c) Create a heart shape.
(d) Create a six-pointed star.
(e) Create a hexagon.
5.17 When interest is compounded continuously, the following equation repre-sents the growth of your savings:
Determine the amount in your account at the end of each year if you invest $1000 at 8% (0.08) for 30 years. (Make a table.)
Create a figure with four subplots. Plot time on the x-axis and current balance P on the y-axis.
(a) In the first quadrant, plot t versus P in a rectangular coordinate system.
(b) In the second quadrant, plot t versus P, scaling the x-axis logarithmically.
(c) In the third quadrant, plot t versus P, scaling the y-axis logarithmically.
(d) In the fourth quadrant, plot t versus P, scaling both axes logarithmically.
Which of the four plotting techniques do you think displays the data best?
5.18 According to Moore's law (an observation made in 1965 by Gordon Moore, a cofounder of Intel Corporation; see Figure P5.18), the number of transis-tors that would fit per square inch on a semiconductor integrated circuit doubles approximately every 2 years. Although Moore's law is often reported as predicting doubling every 18 months, this is incorrect. A colleague of Moore took into account the fact that transistor performance is also improv-ing, and when combined with the increased number of transistors results in doubling of performance every 18 months. The year 2005 was the 40th anniversary of the law. Over the last 40 years, Moore's projection has been consistently met. In 1965, the then state-of-the-art technology allowed for 30 transistors per square inch. Moore's law says that transistor density can be predicted by d1t2 30 12t > 22, where t is measured in years.
(a) Letting t 0 represent the year 1965 and t 46 represent 2011, use this model to calculate the predicted number of transistors per square inch for the 46 years from 1965 to 2011. Let t increase in increments of 2 years. Display the results in a table with two columns-one for the year and one for the number of transistors.
(b) Using the subplot feature, plot the data in a linear x-y plot, a semilog x plot, a semilog y plot, and a log-log plot. Be sure to title the plots and label the axes.
5.19 The total transistor count on integrated circuits produced over the last 35 years is shown in Table P5.19. Create a semilog plot (with the y-axis scaled
logarithmically) of the actual data, using circles only to indicate the data points (no lines). Include a second line representing the predicted values using Moore's law, based on the 1971 count as the starting point. Add a leg-end to your plot.
5.20 Many physical phenomena can be described by the Arrhenius equation. For example, reaction-rate constants for chemical reactions are modeled as
k0 constant with units that depend upon the reaction Q activation energy, kJ/kmol
R ideal gas constant, kJ/kmol K T temperature in K.
For a certain chemical reaction, the values of the constants are
Q 1000 J>mol
k0 10 s-1
R 8.314 J>mol K
for T from 300 K to 1000 K. Find the values of k. Create the following two graphs of your data in a single figure window:
(a) Plot T on the x-axis and k on the y-axis.
(b) Plot your results as the log10 of k on the y-axis and 1/T on the x-axis.
Bar Graphs, Pie Charts, and Histograms
5.21 Let the vector
G [68, 83, 61, 70, 75, 82, 57, 5, 76, 85, 62, 71, 96, 78, 76, 68, 72, 75, 83, 93]
represent the distribution of final grades in an engineering course.
(a) Use MATLAB® to sort the data and create a bar graph of the scores.
(b) Create a histogram of the scores.
5.22 In the engineering class mentioned in Problem 5.21, there are 2 A's
(a) Create a vector of the grade distribution
grades 32, 4, 8, 4, 24
Create a pie chart of the grades vector. Add a legend listing the grade names (A, B, C, etc.)
(b) Use the menu text option instead of a legend to add a text box to each slice of pie, and save your modified graph as a .fig file.
(c) Create a three-dimensional pie chart of the same data. Earlier versions of MATLAB® had trouble with legends for many three-dimensional fig-ures, so don't be surprised if your legend doesn't match the pie chart.
202 Chapter 5 Plotting
5.23 The inventory of a certain type of screw in a warehouse at the end of each month is listed in the following table:
Plot the data in a bar graph.
5.24 Use the randn function to create 1000 values in a normal (Gaussian) distri-bution of numbers with a mean of 70 and a standard deviation of 3.5. Create a histogram of the data set you calculated.
Graphs with Two y-Axes
5.25 In the introduction to Problems 5.6 through 5.9, we learned that the equa-tions for the distance traveled by a projectile as a function of time are Horizontal1t2 tV0 cos1u2 Vertical1t2 tV0 sin1u2 12gt2 For time from 0 to 20 s, plot both the horizontal distance versus time and the vertical distance versus time on the same graph, using separate y- axes for each line. Assume a launch angle of 45° ( p > 4 radians) and an initial velocity of 100 m/s. Assume also that the acceleration due to gravity, g, is 9.8 m/s. Be sure to label both y-axes.
5.26 If the equation modeling the vertical distance traveled by a projectile as a function of time is Vertical1t2 tV0 sin1u2 1>2 gt2 then, from calculus, the velocity in the vertical direction is Velocity 1t2 V0 sin1u2 gt Create a vector t from 0 to 20 s, and calculate both the vertical position and the velocity in the vertical direction, assuming a launch angle u of p > 4 radians and an initial velocity of 100 m/s. Plot both quantities on the same graph with separate y-axes. Be sure to label both y-axes.
The velocity should be zero at the point where the projectile is the highest in the vertical direction. Does your graph support this prediction?
5.27 For many metals, deformation changes their physical properties. In a pro-cess called cold work, metal is intentionally deformed to make it stronger.
The following data tabulate both the strength and ductility of a metal that has been cold worked to different degrees:
Percent Cold Work Yield Strength, MPa Ductility, %
Plot these data on a single x-y plot with two y-axes. Be sure to label both y-axes.
Three-Dimensional Line Plots
5.28 Create a vector x of values from 0 to 20 p, with a spacing of p>100. Define vectors y and z as
y x sin1x2
z x cos1x2
(a) Create an x-y plot of x and y.
(b) Create a polar plot of x and y.
(c) Create a three-dimensional line plot of x, y, and z. Don't forget a title and labels.
5.29 Figure out how to adjust your input to plot3 in Problem 5.28 so as to cre-ate a graph that looks like a tornado (see Figure P5.29). Use comet3 instead of plot3 to create the graph.
204 Chapter 5 Plotting
Three-Dimensional Surface and Contour Plots
5.30 Create x and y vectors from 5 to 5 with a spacing of 0.5. Use the mesh-grid function to map x and y onto two new two-dimensional matrices called X and Y. Use your new matrices to calculate vector Z, with magnitude
Z sin 1 2X 2 Y 22
(a) Use the mesh plotting function to create a three-dimensional plot of Z.
(b) Use the surf plotting function to create a three-dimensional plot of Z. Compare the results you obtain with a single input ( Z) with those obtained with inputs for all three dimensions (X, Y, Z).
(c) Modify your surface plot with interpolated shading. Try using different colormaps.
(d) Generate a contour plot of Z.
(e) Generate a combination surface and contour plot of Z.