Lab 16

Introduction to Plotting

CS211 Lab Policy:

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.

  1. Begin your program by clearing the command window and displaying your name and "Lab 16".

Plotting a single line graph

  1. 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 (.^)?
     
  2. 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

  1. Add code that plots the following three rapidly-growing functions on a single graph:
     

    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.
     

  2. Annotate your plot with:
  3. Save your plot as a JPEG image file named lab16.jpg.
     
  4. 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

  1. 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.
     
  2. 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.
     
  3. Plot the quadratic function represented by the input coefficients over the user-specified range of x values.
     
  4. 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 + ', ... ]
     
  5. Wait for the user to press Enter before closing the graph window and continuing. (Use the same code as step 3 above.)
     
  6. Test your program with several different sets of coefficients and ranges of x!

Plotting multiple graphs in a single figure window

  1. Add code that plots the following 4 functions, each in a separate subplot:
  1. Add titles, axis labels, and line attributes as you think appropriate.
     
  2. 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.