Matlab assignment
ETME 202-CAE II
Spring 19
Lab3 - Topic 3 lab
Plotting
ETME 202-CAE II
Spring 19
Lab3 - Topic 3 lab
Plotting
SELF-GUIDED LEARNING:
This topic introduces graphics and plotting in MATLAB. This provides you with an understanding of MATLAB as not only a programming language, but also as a useful tool. With it, you are able to create beautiful plots, graphs, and images that can communicate your findings to others (engineers and non-engineers) in a useful and meaningful way. Up until this point, we have been viewing our results by printing numbers to the screen. While this still may be the best way to look at results in some situations, the ability to create visual plots or graphs opens up a whole toolbox of tools that you can use throughout your engineering career.
2D Graphics – we will start with the simplest of plots: line plots.
1. Create an array (use variable name x) and fill it with 40 evenly spaced values from 0 to 2*pi. Use that array to create a line plot of the function y=cos(2*x). Copy down the code here.
2. Give the plot a title (‘Plot of Cosine’). Copy down the code here.
3. Label the x and y axis (‘x’ and ‘y’, respectively). Copy down the code here.
4. Create another array of the function, z=sin(x). Overlay the line plot of z on top of the line plot of y. Color the line of z red and increase its Linewidth to 2. Copy down the code here.
5. Change the x-axis of the plot to range from pi/2 to 3*pi/2. Copy down the code here.
6. What command do you use to open up a new figure window?
7. How do you break a single figure into multiple subplots? How does the numbering work?
8. Describe at least two other 2D plotting functions available in MATLAB and show how they work.
3D Graphics – contour and surface plots
9. One example of a 3D graphic is to plot a scalar function that depends on two variables. For example, you can create a surface plot of the following function:
Complete the steps below:
a. Create an array, x, that ranges from -10 to 10, with a step size of 0.5. Copy down the command here.
b. Create an array, y, that ranges from -10 to 10, with a step size of 0.5. Copy down the command here.
c. Use meshgrid to create 2D arrays (X and Y) from your 1D arrays (x and y). Copy down the command here.
d. Use X and Y to define Z (which is mathematically defined above). Copy down the code here.
e. Create a surface plot of Z (using the function surf). Copy down the code here. What does the plot look like?
f. Create a contour-surface plot of Z (using the function surfc). Copy down the code here. How is this plot different from the one above?
g. Change the view point of the plot to something else. Copy down the command here.
10. Create a regular contour plot of Z and a filled contour plot of Z. Copy down the commands here.
11. Change the color map of one of your plots to something other than the default, ‘jet’. Copy down the command here.
12. What if you wanted to color your contour plot a custom color scheme that ranges from green to blue to brown, is this possible in MATLAB?
Exporting Figures to Images
13. Using the any plot created above, export the figure to an image using both the print commands and the File-> Save As option in the Figure window. Explain how to use both. Test out the image files you create by inserting them into a Word document, what file format did you find is best quality, highest resolution for use in a Word document?
PROGRAMMING LAB PREP
Read through the programming lab for this topic (see the following page). Plan out how you might solve the programming lab problem by following these steps:
0. Read and understand the problem
I. Solve it “human-style” and write down the steps you took (this can be rough).
II. Convert the “human-style” steps into “computer-style” steps. (e.g. human-style: report the answer, computer-style: use disp command).
III. Write out the algorithm/pseudocode. This often times involves putting your computer steps in the proper order and nesting appropriately. You can use short hand, block diagrams, or any kind of format that you prefer. It is not necessary to use perfect syntax, just develop the logic.
IV. Translate your pseudocode to MATLAB syntax to whatever degree you feel would be helpful before you begin typing on the computer.
V. Write down any questions you have or any syntax you are not sure of so you can get help in lab.
Name:_________________________________
Z(x, y) = sin(
√ x2 + y2)√
x2 + y2