E5 Lab 8

Mo' MATLAB; Mostly Matrices, …Mostly

Upon completion of the lab you should be able to use MATLAB to

At the end of the lab you will turn in three files.  One is a MATLAB script (.m file) with code in several cells; each cell will be the solution to a different part of the lab (marked by magenta arrows: ).  The other two files are function files "pickShape.m" and "rotTrans.m" described in the lab directions below.  See the end of this web page for directions on what, precisely, needs to be turned in.

Feel free to consult with each other but don't ask for (or provide) completed solutions.

Getting extra help:

If you find that you would like extra help, the following resources are available (in no particular order):

Multidimensional Arrays

Load the file shapes.mat to your computer so that you can see it in the MATLAB directory.   Go to the "Current Directory" window of MATLAB and navigate to that directory (if the window is docked, you might have to undock it, browse to the proper directory, then re-dock it).  At the MATLAB prompt enter

>>load shapes

This will load a variable "shapes" this is a 3x5x4 matrix.  In other words the matrix has 3 rows, 5 columns and 4 "layers."  Display the variable:

>> shapes
shapes(:,:,1) =
0 1 1 0 0
0 1 0 0 0
1 0 0 0 0

shapes(:,:,2) =
0 0 2 3 0
0 1 1 0 0
0 1 0 0 0
shapes(:,:,3) =
0 0 1.0000 1.0000 0
0 1.5858 0.5858 0 0
0 0 1.0000 0 0

shapes(:,:,4) =
0 0.7071 0.4142 1.8284 2.8284
0 0.7071 1.0000 1.0000 0
0 1.0000 1.0000 0 0

Each "layer" of the matrix represents a shape.  For instance consider the top layer:

The top row, [0 1 1 0 0], is a set of x values, the second row, [0 1 0 0 0], is a set of y values and the first three elements of the third row is a color, [1 0 0] (red).

If you want to examine the shape you can make a patch object:

>> myRedTriangle=patch([0 1 1 0 0],[0 1 0 0 0],[1 0 0]);
>> axis([-2 5 -2 5],'square'); grid on;

The first argument of the "patch" command is a vector of "x" values, the second is a vector of "y" values, and the third is a three element vector defining an "r,g,b" color (the first element is the amount of red (from 0 to 1), the second element is for green, and the third element is for red). The (x,y) pairs define the vertices of a "patch" ("patch" is MATLAB terminology for a 2D shape). Note that even through the patch has 5 "x" and "y" values in (x,y) pairs, the first, fourth and fifth (x,y) pairs are all (0,0).  Therefore there are only 3 distinct (x,y) pairs, and these describe the triangle shown. THe color has [r g b] = [ 1 0 0], so it is pure red, with no green or blue. Make sure you understand the connection between the "x" array, the "y" array and the shape that is displayed, otherwise the rest of the lab will be very difficult.

MATLAB Cell 1:  Go to the MATLAB command window and choose File→New→Script.  In this script adapt the code below so that the red triangle is displayed by extracting the appropriate sections of the "shapes" array.  Replace the question marks in the code as necessary and add comments to explain. You may want to refer to Lecture 5 to see how you can work with just a part of a matrix. It is also covered in your text.

axis([-2 5 -2 5],'square');  grid on;

Recall that the beginning of a "cell" is a line of code that starts with "%% " (two percent marks and a space - the space is important). You might want to add code at the top of the cell like the following

disp(' ');  disp('Cell 1');

This will print a line in the MATLAB command window identifying the cell. This will be useful later when you run the code when it has multiple cells.


Write a function called "pickShape" that takes two input arguments, the "shape" matrix, and a number, "n",  from one to four that specifies the shape.  It should have three output arguments, "xShape", "yShape", and "cShape" that represent the x values, y values and the color of the chosen shape.  The first line of the function describes the function syntax:

function [xShape, yShape, cShape] = pickShape( shape, n )

A template function is given in the file "pickShape.m".  Download it into your directory and complete it. It should only take 3 lines, one for each output variable.

Now you should be able to get all of the information to define shape 1:

>>> [x1,y1,c1]=pickShape(shapes,1)
x1 =
     0     1     1     0     0

y1 =
     0     1     0     0     0

c1 =
     1     0     0
>> shape1=patch(x1,y1,c1);  axis([-2 5 -2 5],'square');  grid on;

Repeat for shapes 2, 3, and 4 (by replacing the "1" in the second argument call of the "pickShape" function.  You may want to clear the axes ("cla") between plots.

Save your "pickShape" function to SwatFiles or somewhere you can access it later.

Multiple Plots

Use MATLAB's "subplot" function to put all four shapes on the screen at the same time, each in its own set of axes as shown below.  The subplots should be labeled "Shape 1", "Shape 2", ...  To learn how "subplot" works, look at documentation (>> doc subplot) or use your textbook.  A for...loop can be helpful here (look for any code that looks repetitive and try to figure out how to use a for loop).

MATLAB Cell 2:  Write MATLAB code that shows all four shapes simultaneously, each in its own plot, using MatLab's "subplot" function, and your "pickShape" function. Add this code to the MATLAB script you started previously.

Recall that the beginning of a "cell" is a line of code that starts with "%% " (two percent marks and a space - the space is important).

Coordinate Transformations

A brief review of coordinate transformations from class:

Starting with a point (x,y) you can rotate it about the origin by an angle θ and translate it in the x and y directions by tx, and ty, resulting in a new point (x',y') by doing a matrix multiplication by the transformation matrix, T.

In this example θ is approximately 15°, this rotates the point around the origin by that amount.

After the point is rotated around the origin by θ, it is translated to the left (i.e., tx is negative) and upwards (ty is positive).

Note that the [x;y] vector needs to be augmented with another row with a "1" at the bottom in order for the transformation to work properly.

You can similarly transform multiple points (x1,y1), (x2,y2), … (xn,yn) by forming them into a matrix before multiplying.

Write a function called "rotTrans" that has five input variables:

and two output variables:

The first line of the function should look like:

function [xp yp] = rotTrans( x_in, y_in, theta, tx, ty )

 It should be called with the syntax

[xp yp] = rotTrans(x, y, theta, tx, ty)

Remember: before you do the transformation you need to create an array whose first row is the x vector, second row is the y vector and third row is all ones (check out MATLAB's "ones" function: >> doc ones).

To test your function try the following and make sure you get the correct results:

>> x=1;  % x value of point
>> y=1;  % y value of point
>> % rotate the point (1,1) by 90 degrees, result should be (-1,1)
>> [xp,yp]=rotTrans(x,y,90,0,0)
xp =
yp =

>>  % translate (1,1) by tx=-1, ty=2, result should be (0,3)
>> [xp,yp]=rotTrans(1,1,0,-1,2) 
xp =
yp =

>> % rotate (1,1) by 90 degrees then translate by tx=-1, ty=2.  Result is (-2,3)
>> [xp,yp]=rotTrans(x, y, 90, -1, 2)
xp =
yp =

Save your "rotTrans" function to SwatFiles or somewhere you can access it later.

MATLAB Cell 3:  Verify that your "rotTrans" function works for individual points by including the code shown below (use the code below but fill in the question marks).

disp(' '); disp('Cell 3'); 
% If we take the point (x,y)=(2,-2) and rotate it 180 degrees,
% we get the point (?,?).  If we then translate it by 
% by tx=1, ty=-1.5 we should get the point (x',y')=(?,?).
% This is verified below.

[xp,yp] = rotTrans(2,-2,180,1,-1.5)

MATLAB Cell 4:  Verify that your "rotTrans" function works for multiple points by creating a cell with the code shown below, but with added comments.

disp(' '); disp('Cell 4'); 

x = [3 2];
y = [1 -2];

[xp,yp] = rotTrans(x,y,180,1,-1.5)

Solving the T-Puzzle

For each of the four pieces of the T-puzzle figure out how much each shape has to be translated and rotated to form the solution.

The Pieces

The solution:

MATLAB Cell 5:  Write MATLAB code that displays necessary translations and rotations using the code below (fill in question marks and add comments).  You should figure out the necessary rotations and translations by examining the original shapes.  MATLAB is doing no calculations here - this code is just to ensure that you know the correct transformations to make to the shapes to get them in the proper place.

thetas = [? ? ? ?];
txs = [? ? ? ?];
tys = [? ? ? ?];

% The for...loop repeats the commands in the loop (before the "end") statement four
% times.  First with i=1, then i=2, 3, 4.
for i=1:4,
   disp(['Shape ' num2str(i) ' rotated by theta = ' num2str(thetas(i))])
   disp(['and translated by [tx ty] = ' num2str([txs(i) tys(i)]) '.'])
   disp(' ')

Save this information.  You will be using it next week.

MATLAB Cell 6:  Write MATLAB code that does the following:

Hints/techniques to consider while debugging your script:

To turn in

(via moodle)

For this lab you are to turn in 3 files.  Turn in one MATLAB script with multiple cells; each cell is devoted to one of the assigned tasks (magenta arrows, ).  Turn in two function files "pickShape.m" and "rotTrans.m."  There is no need to turn in a "published" Word document.  Grading of each cell and function is on a ten point scale: