Lab 2

[PDF]

ECE 201: Introduction to Electrical Engineering II
Prof. B.-P. Paris
Lab 2: Plotting in MATLAB

Solve the MATLAB problems below. To generate a report of your work start a diary file called LAB2 by typing diary at the command prompt. All your MATLAB commands as well as the answers you get will be captured in the diary file.

However, the plots you produce are not automatically captured in your diary file! However, it is very easy to incorporate a plot in MS Word or similar. Select Edit| Copy Figure from the menu of a figure window; that will copy the figure to the system clipboard. Then you can paste the figure directly into a Word document.

  1. Be sure that the workspace has been cleared of all variables.
  2. Be sure to close all figure windows before starting. Display the text Instruction #2 is finished. in the command window.
  3. Generate a row vector called X that has a minimum of 0 and a maximum of 1 incremented by 0.1.
  4. Generate a row vector of the same length as X called Y. Have Y represent the function X + 1. Plot X versus Y in figure window 1. Be sure to plot the independent variable X horizontally. The title of your plot must display y = x+1. Your plot must use a solid blue line blue. Be sure to label both axes; consider using a grid to make your plot more readable.
  5. Display the text The Linear Function X+1 at point (0.2,1.9) in Figure 1.
  6. Use the axis command, to zoom in on the plot in Figure 1. Show only the range from X=0.2 through to X=0.4. You should know the Y values at these points.
  7. Generate a row vector called X1 of length 500 that represents the exponential function (exp(x)) from -4 to 0.
  8. Generate a row vector called X2 of length 500 that represents the exponential function from 0 to 4.
  9. Generate row vector X3 from X1 and X2 put end-to-end (length will equal 1000).
  10. Plot X3 in Figure 2 and use green dots (i.e., ’.’) to indicate the data points.
  11. Plot a horizontal red line of thickness 1 across the full length of the X3 data. The line should be positioned so that it intersects with the X3 data. (You may choose where the intersection takes place as long as it does.)
  12. Store the length of X3 in the variable named LengthofX. Use LengthofX in the title for figure 2 to read ’The length of X3 is : ’. Use the value of LengthofX to fill in the blank in the expression.
  13. Generate four subplot (2 plots high by 2 plots wide) as follows.

    , These subplots are to appear in figure window 3.

  14. Generate a figure (in figure window 4) with four sublots (4 high by 1 wide) each showing a sinusoid with the following parameters.
    1. The first sinusoid is to be shown in the top figure. Its paramters are:
      • Amplitude = 1,
      • Phase = 0,
      • Frequency= 3 Hz,
      • Time ranges from 0 to 1 seconds; there are 100 samples.
      • Use a solid red line.
      • In this and all following plots, the title should indicate the amplitude, frequency, and phase.
      • In this and all following plots, the xlabel should indicate that time is the independent variable and its units.
    2. The second sinusoid is to be plotted in the second subplot. It uses the same parameters as above, except that the phase equals π∕2. Use a blue, “dash-dotted” line.
    3. The third sinusoid is to be plotted in the third subplot. It uses the same parameters as above, except that the amplitude equals 3. Use a green, dotted line.
    4. The final sinusoid is to be plotted in the bottom subplot. It uses the same parameters as above, except that the frequency is doubled to 6 Hz. Use a black line and mark samples with ’o’s.

Stop the diary facility. Using a word processor, open the diary file LAB2, clean up any errors or text you don’t want displayed, annotate your work, and insert the figures you produced in the appropriate places, then print a copy and turn it in no later than one week after you did this lab. Don’t forget to add your name, your section number, and the date to the top of LAB1A when you are inside the word processor.