Math 111H: MATLAB Assignment 1

Plotting functions with MATLAB
 
 

Due Date: October 11, 2007


 
 


The example below shows how MATLAB is used to plot simple functions. To begin, click on the Matlab icon on your desktop to open a Matlab screen. Click on File -> New -> m-file to create a new file. Cut-and-paste, or type in, the code below. Save as a .m file, e.g. plotcode.m. Now, at the >> prompt in the Command window, type the file name plotcode. The program will run and the plots should appear. Consult the MATLAB TA's if you have any questions.
 

 
   

x = [-0.25: 0.001: 0.25];

y = sin(10 .* x);

z = cos(10 .* x);

w = (y + z) ./ 2;

plot(x,y)

hold on

plot (x, z, ':')

plot (x, w, '*')

grid on

hold off
 

 
   

In the first statement, x is an array of numbers from -0.25 to 0.25 in steps of 0.001. The ';' suppresses display of the array. The first plot command creates the graph of sin 10 x (note the "." in ".*" and "./" for mpultiplication and division, respectively). The hold on command allows a second graph to be overlaid on the first (instead of replacing it). The ':' in the second plot command causes the graph of cos 10 x to be dotted, to distinguish it from the solid graph of sin 10 x. The grid on command is optional; it puts the horizontal and vertical grid lines on the graph.
 

After running the above example, do the following to hand in. For this assignment, you should hand in at most 5 pages, consisting of four plots and your code.

    Use MATLAB to plot 

        f

    Pay special attention to the values of the function at integer points.

    Next, consider f(x) = 2*x^6 + 4*x^5 + 3*x^3 - 9*x^2 + 2. First, plot f(x) on the interval [-5, 5]. How many roots does this polynomial apparently have? Then, plot the same function on the intervals [-3,0] and [0, 2]. What is your answer now? Explain your observations.