CSE4421 Lab 01

Thu Jan 11 12:30-14:20, 14:30-16:20
Due: Thu Jan 25 before 23:59

Introduction

This lab has 2 parts. This first part is an introduction to Matlab programming. The lab session will consist of an instructor led guided tutorial on using Matlab and learning to program in the Matlab programming language. Students already familiar with Matlab may choose to not attend this lab session; however, such students should review or learn how to use cell mode in Matlab to produce HTML reports:

  1. Cell mode
  2. Mark-up in cell mode

The second part of the lab consists of a series of written questions concerning the lecture material.

Written questions

The written questions are here

Matlab tutorial

The tutorial starts by entering some basic commands into the Matlab interpreter. We then move onto creating a script and demonstrating how commands can be run directly from the script, and how to generate an HTML report from the script.

Here is a sample script that illustrates some basic Matlab functionality.

%% Lab 1 Creating a script and publishing a report
% This is the script created during the lab demonstration.
% The demonstration showed you how to create script,
% add comments to the script, use cell mode in a script,
% and publish a script to an HTML report.


%% Question 1
% The radius of the earth is approximately 6.37 * 10^6 meters.
% (a) What is the circumference of the earth in kilometers?
% (b) What is the surface area of the earth in square kilometers?
% (C) What is the volume of the earth in cubic meters?

%% Question 1a

% radius of the earth in meters
radius = 6.37e6;

% circumference in kilometers
circum = 2 * pi * radius / 1000


%% Question 1b

% surface area in square kilometers
surfaceArea = 4 * pi * (radius / 1000)^2


%% Question 1c

% volume in cubic meters
volume = 4 / 3 * pi * radius^3


%% Question 2
% Plot the function y = x^2 for values
% x = -5:0.1:5
% Label the x and y axes.

x = -5:0.1:5;
y = x.^2;
plot(x, y)
xlabel('x');
ylabel('y');

The report produced by publishing the script can be viewed here.

The second part of the tutorial guides the students through parts of the programming exercise for this lab.

Programming exercise

Geometric Transformations in 2D

A geometric transformation in 2D is a function that maps a 2D point $p = \begin{bmatrix}x \\ y\end{bmatrix}$ to a new 2D point $q = \begin{bmatrix}x' \\ y'\end{bmatrix}$.

A translation moves all points a constant distance in a given direction. A translation of a point can be accomplished using vector addition:
$$\begin{bmatrix}x' \\ y'\end{bmatrix} = \begin{bmatrix}x \\ y\end{bmatrix} + \begin{bmatrix}t_x \\ t_y\end{bmatrix}$$

A vertical shear is a translation along the y axis by an amount that increases linearly with the x component of the point; this transformation will transform a square into a rhomboid (a parallelogram with unequal sides and angles not equal to 90 degrees). A vertical shear by a non-zero scalar amount m can be accomplished using matrix multiplication:
$$\begin{bmatrix}x' \\ y'\end{bmatrix} = \begin{bmatrix}1 & 0 \\ m & 1\end{bmatrix} \begin{bmatrix} x \\ y\end{bmatrix}$$

A rotation about the origin spins a point counterclockwise around the origin through an angle $\theta$. A rotation can be accomplished using matrix multiplication:
$$\begin{bmatrix}x' \\ y'\end{bmatrix} = \begin{bmatrix}\cos\theta & -\sin\theta \\ \sin\theta & \cos\theta\end{bmatrix} \begin{bmatrix} x \\ y\end{bmatrix}$$

1. Implement Matlab functions that perform translation, vertical shear, and rotation in 2D. Your functions must have the following signatures:

function Q = translate2(P, t)
function Q = vshear2(P, m)
function Q = rotate2(P, theta)

In each function, P should be a 2 x n matrix of n 2D column vectors representing the points to be transformed, and Q should be a 2 x n matrix of n 2D column vectors representing the output transformed points. For translate2, t is the 2D translation vector. For vshear2, m is the shear amount. For rotate2, theta is the angle in degrees.

A working version of rotate2 might look like:

function Q = rotate2(P, theta)
% ROTATE2 Rotate points about the origin.
%   Q = rotate2(P, theta) rotates the points P about the origin by
%   an angle theta specified in degrees. P is a 2 x n array of points.

R = [cosd(theta) -sind(theta);
     sind(theta)  cosd(theta)];
Q = R * P
end

2. Write a Matlab script (that will be published as HTML) that uses the three functions you created in Step 1 to transform two objects that are represented as points.

Start by creating a section (cell) in your script with the title Original Shapes. The first object should be a square represented by its corner points
$$\begin{bmatrix} 1 \\ 1 \end{bmatrix}, \begin{bmatrix} -1 \\ 1 \end{bmatrix}, \begin{bmatrix} -1 \\ -1 \end{bmatrix}, \begin{bmatrix} 1 \\ -1 \end{bmatrix}$$

The second object should be a circle of radius 1 centered at the point $\begin{bmatrix} 5 \\ 0 \end{bmatrix}$ and represented using points on the circumference of the circle every 10 degrees. Plot the closed outlines of both the square and circle in black using the plot command; both shapes should appear on the same plot (use the hold on command). The square should look like a square (not a rectangle) and the circle should like a circle (not an ellipse); use the axis equal command.

Create a new section (cell) in the script with the title Translation. Next, translate the points of the square and circle using your translate2 function by the vector t = [-3; -3]. Plot the translated square and circle in red on the same plot as the original square and circle (the plot should show the original shapes in black and the transformed shapes in red).

Make sure to use the axis equal command when plotting the shapes in Steps 3-6.

The script below illustrates Steps 1 and 2.

%% Original Shapes

% points on the square; the first point is repeated to close the
% square when it is plotted
Psquare = [1 -1 -1  1 1;
           1  1 -1 -1 1];

% points on the circle generated using a loop
% it's faster to preallocate space for the points than to
% repeatedly append to an array in Matlab
Pcircle = zeros(2, 37);
for i = 1:37
    ang = (i-1) * 10;
    px = cosd(ang) + 5;
    py = sind(ang);
    Pcircle(:, i) = [px; py];
end

% points on the circle generated using a vector angles
% most functions in Matlab are vectorized (i.e., they will work
% for array inputs)
ang = 0:10:360;
Pcircle = [cosd(ang) + 5;
           sind(ang)];

plot(Psquare(1, :), Psquare(2, :), 'k');
hold on
plot(Pcircle(1, :), Pcircle(2, :), 'k');
axis equal

%% Translation

% translation vector
t = [-3; -3];

% translated square
Qsquare = translate2(Psquare, t);

% translated circle
Qcircle = translate2(Pcircle, t);

plot(Qsquare(1, :), Qsquare(2, :), 'r');
plot(Qcircle(1, :), Qcircle(2, :), 'r');
axis equal

3. Create a new section (cell) in the script with the title Vertical Shear. Use your vshear2 function with a shear amount m = 3 to transform the original shapes. In a new figure, plot the original and transformed shapes in black and red, respectively.

4. Create a new section (cell) in the script with the title Rotation. Use your rotate2 function with an angle theta = 120 degrees to transform the original shapes. In a new figure, plot the original and transformed shapes in black and red, respectively.

5. Create a new section (cell) in the script with the title Translation Followed by Rotation. Translate the original shapes first, and then rotate the translated shapes. Use the same translation and rotation parameters as in Steps 2 and 4. In a new figure, plot the original and final transformed shapes in black and red, respectively.

6. Create a new section (cell) in the script with the title Rotation Followed by Translation. Rotate the original shapes first, and then translate the rotated shapes. Use the same translation and rotation parameters as in Steps 2 and 4. In a new figure, plot the original and final transformed shapes in black and red, respectively.

7. Publish your Matlab script to an HTML file.