Lab Graphing Functions in MATLAB

Welcome to the LAB component of Math 3046.  Lets assume that you are logged into your personal computer or a Temple University (TU) computer. Now start MATLAB and Microsoft Word (or equivalent word processor. If you are on your own computer you know where the programs are located. If you are on a TU computer you can locate these programs from an icon that may appear on the desk top or if you are running Windows on the desktop click here for directions. (If you are on a MAC you are on your own!)

In Word create a new document. Put your full name and TU ID at the top of the document.   As you work through a lab assignment you should enter all the commands that are listed into MATLAB and work through the ExamplesBut in your Word document you only need to include your responses to the Exercises together with the name of the lab, the number of the Exercise, and your answer. Your answer can a combination of the following four items you are answering. Clearly label any parts to an exercise and use a row of plus signs (++++++++++++++++++) between answers to exercises.

Credits: This design and approach was adapted from work at University of California at San Diego by Bill Helton and a host of contributors.

Icon: Item: Comments:
Commands entered in MATLAB & resulting output  You should copy relevant input and output from MATLAB and paste it into your Word document.  You need only include commands that worked.
Plots & graphs  Include all graphs generated in an exercise unless the problem specifically tells you which/how many to include.
Full sentence response Exercise contains a question that you need to use at least one or two complete sentences to answer.  Even if you're stuck, write down any reasoning or ideas you've had.
Requires work by hand  Do scratch work by hand.  Leave space in your Word document and write your scratch work directly on the assignment to turn in.

Graphing Functions

MATLAB provides several methods for plotting the graphs of functions and other curves. The easiest to use is what we will call EZ plotting, since it uses the command ezplot and its variants. While EZ plotting is easy to use it is not as flexible as several other methods which are available.

Example 1. (EZ plotting)

To plot a function with the ezplot command we first need to form an expression for the function.

  >> ezplot('cos(x)/(1+x^2)')   %Note that the expression is included between single quotes. Result is Figure 1.

     
 Figure 1.       Figure 2. 
Figure 3.

 In Figure 1 the domain was selected automatically and a title appears at the top. MATLAB used the default domain of \(\left[ { - 2\pi ,\,2\pi } \right]\). We can change the domain used by specifying it as part of the ezplot command as shown next. This is shown in Figure 2. It is often the case that our first attempt when we choose the domain is not completely satisfactory. Do not be afraid to redo a plot to make it look better.

>> ezplot('cos(x)/(1+x^2)', [-9,9])      %Result is Figure 2.

>> ezplot('cos(x)/(1+x^2)', [-9,9,-1,1])   %Result is Figure 3.

In Figure 3 the domain was specified as [-9, 9] and the range is [-1, 1]. You may need to make several adjustments to the domain and range to obtain the picture you want. You need not specify a range but then you may not have all of the curve displayed as illustrated in Figure 2. Since another example follows we will delete any current figures in MATLAB using the following command.

 >> close all     %Delete all active figures.

Example 2. (Plotting using fplot.)

To plot a function with the fplot command we first need to form an expression for the function as with ezplot. This time we give the function to plot a name using the inline command. Again the expression must be between single quotes.

>> g = inline('cos(x)/(1+x^2)')  

Now we want to graph g(x) on the interval [-10, 10].  See Figure 4.

>> fplot(g,  [-10, 10])
>>
title('An fplot graph.')

 
Figure 4. 

MATLAB has the result of the previous commands in a figure window. If another ezplot or fplot command is executed then the current figure will be replaced by the new figure. The command figure brings up a new graphics window.  MATLAB will draw on whatever graphics window was most recently in the front.

>> figure
>>
ezplot(g, [-5, 5])

Now there are two active graphics windows, Figures 1 and 2. Commands figure(1) and figure(2) will display the two graphs.

Exercise 1.
(a)
Graph the function \(f(x) = e^{0.1x} \cos (2x)\)  on the interval [0, 5] with ezplot. Include the MATLAB code you used.  Then paste the graph into your Word document.  To copy the graph bring its window to the front (if it is not visible use command shg) and choose the menu option Edit => Copy Figure. Include a sentence describing the \[\mathop {\lim }\limits_{x \to \infty } f(x).\]
(
b) The initial value problem \(y' = 2y - t,\,\,y(0) = - 1\) has solution \(y(t) = \frac{t}{2} - \frac{5}{4}e^{2t} + \frac{1}{4}\). Graph y(t) on the interval [0, 1] using fplot and put a title on the graph that is your name. Include the MATLAB code you used.  Then paste the graph into your Word document.  To copy the graph bring its window to the front (if it is not visible use command shg) and choose the menu option Edit => Copy Figure. Include a sentence describing the \[\mathop {\lim }\limits_{t \to \infty } y(t).\]
(
c) Graph the function \(g(x) = e^{ - 0.1x} \) on the interval [0, 8] with
ezplot. Next use command hold on, which retains the graphics window so we can plot a second function in the same window. Graph  \(f(x) = e^{-0.1x} \sin (2x)\) on the interval [0, 8] and put a title on the graph that is your name. Include the MATLAB code you used.  Then paste the graph into your Word document.  To copy the graph bring its window to the front (if it is not visible use command shg) and choose the menu option Edit => Copy Figure. Once you have things in the word file use the command hold off which releases the graphics screen so that a new plot can replace the current plot. Finally Include a sentence describing the \[\mathop {\lim }\limits_{x \to \infty } f(x).\]  

Plotting using the plot command.

Previously when we used the ezplot or fplot command we constructed an expression for a function and used the expression as input to the commands. The plot command requires we first construct a set of ordered pairs before using the command. Then points corresponding to the ordered pairs can be displayed using various symbols and connected with a variety of solid or dashed lines. So instead of using a function expression the plot command employs a discrete set of points hence there is more flexibility, but at the cost some ease of use. The set of ordered pairs can be constructed as two arrays or vectors of data, say x-values and y-values.

Example 3. (Plotting a set of ordered pairs.)

A set of ordered pairs can be constructed by choosing x-values and the same number of y-values. There need not be a function involved. In MATLAB an array or vector is constructed by including a list of numerical values between square brackets with the entries separated by a space or comma. Each of the following commands includes a variation of the plot command.

>> x = [-3 -1 0 2  7 ], y =[ 2 1 3 5 -1] , plot(x,y)                 %Result is Figure 5. Points connected, but no symbol for a point.

>> figure, x = [-3 -1 0 2  7 ]; y =[ 2 1 3 5 -1] ; plot(x,y,'*')     %Result is Figure 6. Only an asterisk for each point.

>> figure, x = [-3 -1 0 2  7 ]; y =[ 2 1 3 5 -1] ; plot(x,y,'*-')    %Result is Figure 7. Points denoted by an asterisk and connected.

>> figure, x = [-3 -1 0 2  7 ]; y =[ 2 1 3 5 -1] ; plot(x,y,'*-r')   %Result is Figure 8. The same as Figure 7, but in red.

       
Figure 5. Figure 6. Figure 7. Figure 8.

Example 4. (Using plot to graph a function.)

Graph the function \(f(x) = 3x^2 - 2x^3 \) over interval [-1, 3] by using a set of equispaced points from x = -1 to x = 3. To generate the sets of ordered pairs we will illustrate two techniques. To define a vector that contains a large number of equispaced points in [-1, 3] we use MATLAB's colon operator in the form start: increment : finish, where start is -1, finish is 3, and we choose the increment.

>> x = -1:0.25:3     %Increment 0.25

x =

  Columns 1 through 13

   -1.0000   -0.7500   -0.5000   -0.2500         0    0.2500    0.5000    0.7500    1.0000    1.2500    1.5000    1.7500    2.0000

  Columns 14 through 17

    2.2500    2.5000    2.7500    3.0000

>> x = -1:0.1:3    %Increment 0.1

x =

  Columns 1 through 13

   -1.0000   -0.9000   -0.8000   -0.7000   -0.6000   -0.5000   -0.4000   -0.3000   -0.2000   -0.1000         0    0.1000    0.2000

  Columns 14 through 26

    0.3000    0.4000    0.5000    0.6000    0.7000    0.8000    0.9000    1.0000    1.1000    1.2000    1.3000    1.4000    1.5000

  Columns 27 through 39

    1.6000    1.7000    1.8000    1.9000    2.0000    2.1000    2.2000    2.3000    2.4000    2.5000    2.6000    2.7000    2.8000

  Columns 40 through 41

    2.9000    3.0000

For small increments we can use a semicolon after the finish value. To get a smooth graph of f(x) we will use the increment 0.05 which gives 81 values in [-1, 3]. To get the corresponding values of f(x) we employ MATLAB's vector oriented capability. That is, evaluate the function at the vector x instead of each entry. To this end the standard arithmetic operators are replaced by corresponding vector operators. To evaluate f(x) at a single value x the MATLAB command y = 3*x^2-2*x^3 is sufficient. If we precede each arithmetic operator by a period we invoke the vector evaluation. This command is y = 3.*x.^2-2.*x.^3 which generate a set of 81 y-values to use with plot.

>> x = -1:0.05:3; y = 3.*x.^2-2.*x.^3; plot(x,y), title('f(x) = 2x^2 - 3x^3')   %Copy this command and execute in MATLAB.

Inserting vector operators tends to be error prone, so MATLAB has a command that performs the task. The command is named vectorize. For details use help vectorize. Once the function is vectorized we still need to evaluate it so we use the eval command.

Graph \(f(x) = \frac{{4\sin (3x) - 5\cos (x)}}{{e^x }}\) on [0, 5] using 200 equispaced points. To generate the points we use command linspace which has the format linspace(X1, X2, N) to generate N points between values X1 and X2.

>> f='(4*sin(2*x)-5*cos(x))/exp(x)';x=linspace(0,5,200);y=eval(vectorize(f)); plot(x,y,'d:r')  %Copy this command and execute in MATLAB.

The commands used above include

ezplot
fplot
plot
: operator in
start: increment :  finish
linspace
vectorize
eval
shg
hold on
hold off
title

Exercise 2.
(a)
Graph the function \(f(x) = \frac{{e^x }}{{x^2 + 1}}\) on the interval [-3, 3] with plot. Use linspace with N = 50, the vectorize, and eval commands, connect the points, indicate the points with an asterisk and use c for the color; use 'c*-'. Include the MATLAB code you used.  Then paste the graph into your Word document.  To copy the graph bring its window to the front (if it is not visible use command shg) and choose the menu option Edit => Copy Figure.

(b) The initial value problem \(y' = 2y - t,\,\,y(0) = - 1\) has solution \(y(t) = \frac{t}{2} - \frac{5}{4}e^{2t} + \frac{1}{4}\). Graph y(t) on the interval [0, 1] using plot, construct t-values with increment 0.05, use the color red, and put a title on the graph that is your name. Include the MATLAB code you used.  Then paste the graph into your Word document.  To copy the graph bring its window to the front (if it is not visible use command shg) and choose the menu option Edit => Copy Figure.

(c) The height h(t) and horizontal distance x(t) traveled by a ball thrown at angle α with an initial velocity v are given by

\[h(t) = v\,t\sin \left( \alpha \right) - \frac{1}{2}gt^2 ,\,\,x(t) = v\,t\,\cos \left( \alpha \right).\]
We take \(g = 9.81m/s^2 \) and suppose the ball is thrown with velocity \(v = 15m/s\) at an angle of \(\pi /6\) radians. Determine how long the projectile is in flight; call that value tmax. Then use plot to graph a smooth curve for h(t) and x(t) over [0, tmax]. Include the MATLAB code you used.  Then paste the graphs into your Word document.  To copy the graph bring its window to the front (if it is not visible use command shg) and choose the menu option Edit => Copy Figure.

(d) Graph the function \(f(x) = \left| {\cos (x)} \right|\) on interval [0, 2
π] using 500 equispaced points. After you obtain the graph use the command grid in MATLAB. Include the MATLAB code you used.  Then paste the graph into your Word document.  To copy the graph bring its window to the front (if it is not visible use command shg) and choose the menu option Edit => Copy Figure. Explain what the grid command did.

 

Last updated 1/26/2013

David R. Hill Temple University