E5 Lab 10

Even More MATLAB; Let's Get Animated

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

At the end of the lab you will turn in two  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 is a function file "getAngs.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):

Getting Started

You may want to be familiar with chapters 3 and 4 of your text (and chapter 5 if you are feeling ambitious, thought this isn't strictly necessary).


Warm up exercise: A for...loop

Load the file shapes.mat into a directory in your user folder.   Also load your "rotTrans.m" and "pickShape.m" function files that you wrote for the last lab in the same folder (or you can use mine, rotTrans.m, pickShape.m).  Save the file,  cell10_1.m (shown below), in the same directory as the other files.

Run the "cell10_1" script.  It should solve the puzzle (your task from last week), but uses arrays to store the patches as well as the necessary rotations and translations.  To make sure you understand it, add detailed comments to explain exactly what the program is doing.  You don't need to worry about the "emode" variable (yet).

MATLAB Cell 1: Start a new script for this lab.  Paste the code for cell10_1.m into "Cell 1" of the new script.  Add comments to the code to indicate that you fully understand how it works..   Make sure the final (solved) image is also shown.

Again with the "for...loop" (this time for animation)

Load the file cell10_2.m into your directory and add code that animates the pieces (one at a time) as they move from their original position (with theta=0, tx=0, and ty=0) to the final position in several (10-20)  steps (or more, if you are ambitious) with a brief pause between steps.  The final animation should look something like the one below (note, this animation was made with an older version of MATLAB, with current versions the overlaps between shapes don't display in the same way)..  You will need to generate a set of linearly spaced angles and translations for the animation, and then iterate through them (using the rotTrans function you wrote previously, or use this link to rotTrans.m file). 

When you are done writing your code, select all of the code in the editor and then go to Text→Smart Indent (this will make your code easier to read by properly indenting all loops).  It is a good idea to do this periodically when writing large chunks of code.  It makes it much more readable.

MATLAB Cell 2:  Include the code that performs the animation.  Make sure it is properly commented and indented.

Writing an "if..." clause

If you watch the animationabove, you will notice that pieces that rotate to a positive theta rotate in a counterclockwise direction, those with a negative theta rotate clockwise. 

Add an "if..." statement to your code such that all angles that are less than zero are converted to the equivalent positive angles (you can do this by adding 360 degrees), so that all parts rotate counterclockwise.  Do not just change the definition of the "theta" variable at the top of the file.

MATLAB Cell 3:  Include the code that rotates all objects counterclockwise.

Yet again with the loops.

Write code in a new cell (cell 4) that animates the pieces so they move as in the animation below (first rotating, then translating in the x direction, then translating in y).  The direction of rotation isn't important. 

MATLAB Cell 4:  Include the code that performs the specified animation.

Fitting data to a line and finding a point on that line.

Consider the code below:

angA=[0.50 0.04 0.25 0.75 0.98]; %angA is a normalized angle from 0 to 1
thetaA=[-4 -41 -18 22 46];       %angle (degrees) 

xlabel('Normalized angle'); 
ylabel('Angle \theta_a (degrees - \circ)'); 
title('Normalized vs. actual of motor A');
axis([0 1 -90 90]); grid on; 

At this point it isn't important what the "angA" and "thetaA" data represent; you will use this in the next lab.  The variable "angA" indicates the normalized angle (between 0 and 1), and "thetaA" is an angle (in degrees).

Paste the code into a new cell (cell 5).  Run it and you should get a plot like the one shown below:

We would like to find a line that accurately fits the data.  MATLAB does this with the polyfit" command. 

Add the following code to the cell to find the slope and intercept of the line that "best" fits the data.

p=polyfit(angA,thetaA,1); % Do a first order (linear) fit of the data 
mA=p(1);                  % slope of fit
bA=p(2);                  % intercept of fit

Now add code that plots the best fit line on the same graph and adds a legend so it looks something like the plot below:

MATLAB Cell 5  Cell 5 should include all of the code that generates the graph shown above.

Using the best fit data to find a pulse width given an angle

In the next lab you will be using two motors whose angles you want to control.  To do this you will control the width of pulses sent to the motor (at this point, don't worry how this is done, or even exactly what it means).  Your final task is to write a function that has two inputs (the angles "thetaA" and "thetaB") and uses the best fit lines to find two outputs (the associated normalized angles "angA" and "angB").  The quantities "mA" and "bA" are the slope and intercept of the plot for angle "thetaA" vs "angA" (as seen above) and "mB" and "bB" are the slope and intercept of a similar plot for "thetaB" vs "angB" (the plot is not shown).

Download the function below, getAngs.m, into your directory.  Complete the code so that you can specify angles thetaA and thetaB and the function returns the appropriate normalized angles.

 You can test the function by entering the following code into a new cell (cell 7):

[angA, angB] = getANgs(-40, 0)

The values for "thetaA" and "thetaBare -20° and 0°, respectively.  The returned pulse counts, "angA" and "angA" should be close to 0.27 and 0.5, respectively.

MATLAB Cell 6:  Include code that calls your "getAngs" function and verify that the result is close to what is expected..


Ah, penny, brown penny, brown penny, I am looped in the loops of her hair. This is an optional "challenge" problem for extra credit.

Write code in a new cell (cell 7) that animates the pieces so they move as in the animation below.    The direction of rotation isn't important.  Think about this - it can be done with a relatively small modification of some of your previous code.

MATLAB Cell 7:  Include the code that performs the specified animation.

To turn in

(via moodle)

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