Return to Existing Everywhere Main Page

Matlab Tutorial Lab 1 Operations on Arrays, Vectors and Matrices

 

Matlab Learning Objectives:

By completing this Lab exercise you should be competent in the following leaming objectives:

  • Understand how to create vectors and matrices using Matlab
  • Know how to use Matlab operators
  • Learn how to diagnose the Matlab error messages for the above operators
  • Use Matlab to plot in 2-D and 3-D

Lab Instructions:

Read the instructions below carefully and follow them as a team. If you have questions on any of them, discuss the issue amongst your team, and if you can't solve it, ask the TA, or even a team nearby.

Passing off the Lab will require that you communicate, help one another, and be responsible for each other's leaming to some extent. Make sure every member of your team knows the material before moving on to each new section. You will need to get the TA's signature for the Lab. The TA may request that one of your team members demonstrate a portion of the Lab activity to ensure you all know the material.

Start Matlab and perform the following steps for each of the five sections:

The workspace and diary should be saved for the following lab. To save the diary enter diary on in the command window before starting. Save to a file when the lab is over.

The file can be reloaded by typing load file. The diary can be loaded by typing type diary. There is a dot after diary.

A. This part of the lab demonstrates how Matlab does multiplication.

Consider the following arrays:

a = 2
b = [3 4]
c = [8;3]
d = [3 2;1 5]
e = [9 7;2 1]
f = eye(3)
g = eye(4)

Observe what happens in the following operations. The result of each operation should be understood including error messages.

  1. a*a
  2. a*b
  3. a*c
  4. a*d
  5. a*e
  6. a*f
  7. a*g
  8. b*a
  9. b*c
  10. b*d

       .
       .
       .
       .
       .
       .

    x.   g*g

B. This part of the lab will help to become familiar with the colon operator.

  1. Use the colon operator ":" to create a vector with values [1 3 5 ... 99] i.e. all the odd integers less than 100 in ascending order. Assign your results to a variable named odd_up. Hint: every odd number is of the form 1 + 2*(n-1) where n = 1, 2, 3, 4, 5, ...

  2. Use the colon operator ":" to create a vector with values [99 97 95... 1] i.e. all the odd integers less than 100 in descending order. Assign your results to a variable named odd-down. Hint: consider 100 - odd_Up

  3. The symbol "pi" in Matlab represents the number 3.14159... . Type in the expression:

    minus_one = cos(pi*odd_up)

    What values do you get and why? -1, every odd multiple of pi is -1

  4. Use the colon ':' and exponental operator '.^' to create a vector plus_minus of length 50 that has the value:

    [1 -1 1 -1 1 -1 ... 1 -1] (25 pairs of values 1 -1)

    (Hint: consider [(-1)^2 (-1)^3 (-1)^4 (-1)^5 ... (-1)^51]
    which can be constructed from [-l -1 -1 ... _J].^[2 3 4 ... 51]
    You can use the built-in function linspace
    linspace(-1,-1,50) creates a vector of 50 values starting at -1 and ending at -1
    Or use the cos function to generate the vector plus_minus. e.g. cos(pi*(O:49));

C. This part aims at giving you basic idea of 2D graphics ability of Matlab

There are two fundamental commands which draw pixels on the screen: plot, and fplot. Through the following example, you should get an idea of what they implement.

  1. Make sure you are in Matlab command window.

  2. Type the following commands:

    1. x = 1: .1: 12;
      y = exp(sin(x)); (twelve values)
      plot (x,y,'g-*'); (that's g dash star)
      hold on;
      What is the range of values is displayed on the x-axis? 0-12
      What does "hold on" do? It holds the current plots and axis properties so that subsequent graphing commands add to the existing graph
      Hint: use the help command or your course book.

    2. fplot('x+sin(x)',[1,10],'r');

      Both graphs should display. What range of values is displayed on the x-axis? 1 - 10

      If you want to see the range of x-values from 1 to 12, click Edit/Axes Properties then find
      the "Limits" property for the x values and change the 10 to a 12.
      Click the zoom in button on the figure window. Click several times on the point of intersection of the two curves. What is the (x,y) value of this point? (1.7, 2.7)

  3. Now try to draw the graphics of the following function using plot and fplot. Write down the codes you've written. To clear the previous figure use the "clf" command. Don't use "hold on" between the next plots:

    y = sin ( exp(x) ) (-10
    CODE1: x=[-10:.1:4] y = sin ( exp(x) ) plot(x,y)
    CODE2: fplot('sin ( exp(x) )', [-10,4],'g')

  4. Try the following command and tell what they do.

    title('2D Graphics'): 2D graphics are displayed above the graph
    xlabel('The X axis'): The x axis is displayed below the horizontal axis
    ylabel('The Y axis'): The y axis is displayed alongside the vertical axis


D. This part of the lab will illustrate the powerful 3D graphics ability of Matlab.

  1. Type following commands:

    close;
    x -1:0.1:2;
    y X;
    [X,Y] = meshgrid(x,y); (remember Matlab is case sensitive)
    Z=sin((X.*X+Y.*Y)./(X.*X+Y.*Y+l)); (don't forget the dot before the *and / operators)
    view(3);
    S = surf(X,Y,Z);

    What is generated in the figure window? A cup, a 3D plot of the sin((x^2 + y^2)/(x^2 + y^2 + 1)) function with red at the top and blue at the bottom

  2. Explain what the dot version of the operators '.*' and './' do and why they are used rather than * and /

    * and / perform scalar and matrix operations while
    .* and ./ perform array operations element-by-element

  3. Try the following commands and see their effects:

    shading interp;
    set(S,'facecolor','r');
    set( S, 'facelighting', 'phong');
    light('Position', [1,1,1],'Style', 'infinite');
    You may type 'help command' to see the usage of a specific command.

  4. Write code to generate the graph of the following function in 3D:

    z = sin(x^2 + y^2) for the intervals -1 < x < 1, -1 < y < 1

    x = -1:0.1:1;
    y = x;
    [X,Y] = meshgrid(x,y);
    z = sin(X.*X+Y.*Y);
    view(3);
    S = surf(X,Y,Z);


In the figure window, click on the Tools command in the menu and select 3D. 3D rotation is now possible. Click and hold down the mouse button on the 3D graph. A wire-frame cube should be visible. Drag the mouse button a little bit and then let go and observe that the graph has changed. Until another tool is selected or Tools/rotate 3D is clicked, the rotation mode is still on.

     >> a=3

     a =

            3

     >> b=[2 3]

     b =

            2     3



   >> e*a

   ans =

         3   9
         6  12

   >> e.*a

   ans =

         3    9
         6   12

   >> f*(e
   ??? f*e
   Error: ")" expected, "end of line" found.

   >> f*e

   ans =

         1    3
         2    4

   >> e*f

   ans =

         1     3
         2     4


Return to Existing Everywhere Main Page