Lab 16
Introduction to Plotting
CS211 Lab Policy:
- This lab exercise will not be graded.
- Submit as much as you have completed before the end of the lab period in
which it is assigned.
- If you do not finish this lab work, it is to your advantage to finish it
outside of class. Please re-submit your finished work to the course web
site.
- You may receive help from anyone in completing this lab.
- You may not submit another student's code as part of your
lab.
Instructions:
You will create one MATLAB program named Lab16
in file Lab16.m for this lab. Your lab16
function should perform the tasks described below.
PLEASE NOTE: Once you have successfully implemented one of the graphs below,
comment out that section of the code while you are working on the remaining
sections. This will speed up your work. After you are finished with the entire
lab, please uncomment any code you have previously commented out.
- Begin your program by clearing the command window and displaying your
name and "Lab 16".
Plotting a single line graph
- Add code that plots the function
y = x*(sin(x)) - cos(x2)
over some reasonable range of x values. Experiment with various ranges of x
values until you find a range that gives a good graph of the function's
behavior. Be sure to try some negative values for x. Also make sure that you
include enough points for the x vector to create a smooth plot.
Question: When you calculate the function's values, should you use the
matrix multiply operator (*) or the
element-by-element multiply operator (.*)?
How about matrix exponentiation (^) or
element-by-element exponentiation (.^)?
- Add the following code to wait for the user to press Enter before
closing the graph window and continuing:
Wait = input('Press Enter to
continue.', 's');
close()
Plotting Three Rapidly Growing Functions
- Add code that plots the following three rapidly-growing
functions on a single graph:
- y = 20x
- y = xx
- y = x! (factorial of x)
Use a different color (e.g., blue, green, red) for each function you
plot. (Use a different linespec
string for each plot line). Your plot should show the relative growth of
these functions for positive values of x. In particular, an
examination of your plot should reveal which of these is the fastest growing
function and the approximate values of x at which one function
overtakes another. Hint: a linear plot will not work for this.
- Annotate your plot with:
- x and y axis labels
- a legend that labels the 3 functions, and
- an appropriate title.
- Save your plot as a JPEG image file named
lab16.jpg.
- Wait for the user to press Enter before closing the graph window and
continuing. (Use the same code as step 3 above.)
Plotting a User-Entered Quadratic Function
- Prompt for and get from a user the 3 coefficient values of a
quadratic equation. That is, get a value for a, b, and c
in the equation ax2 + bx + c = 0. You can
use a single input statement and have the user enter a vector of 3 values,
or you have use 3 separate input statements.
- Prompt for and get from a user the minimum and maximum values of x for
the plot. Ensure the user-entered minimum x value is not greater than the
user-entered maximum x value. You can use a single input statement and have
the user enter a vector of 2 values, or you can use 2 separate input
statements.
- Plot the quadratic function represented by the input coefficients over
the user-specified range of x values.
- Title your plot with the function plotted. For example, if the user
enters -1 for a, -1 for b, and -2 for c, the title of
your plot should be
y = -1x^2 + -1x + -2
Hints: Build up the equation as a string using the
num2str function. You can "glue"
(concatenate) char arrays together by placing a sequence of strings between
square brackets, for example:
['y = ', num2str(a), 'x^2 + ', ...
]
- Wait for the user to press Enter before closing the graph window and
continuing. (Use the same code as step 3 above.)
- Test your program with several different
sets of coefficients and ranges of x!
Plotting multiple graphs in a single figure
window
- Add code that plots the following 4 functions, each in a
separate subplot:
- y = x10
- y = 20x
- y = xx
- y = x! (factorial of x)
- Add titles, axis labels, and line attributes as you think
appropriate.
- Wait for the user to press Enter before closing the graph window. (Use
the same code as step 3 above.)
Turn-in:
Submit your
Lab16.m and
lab16.jpg files.