0% found this document useful (0 votes)
68 views

Lecture 6: More Matlab Functions, Transformations: Professor Erik Cheever Course Web Page

This document provides an overview of the lecture topics on functions and transformations in MatLab. The key points are: 1) Functions allow repeating code for multiple cases without rewriting. Functions take in inputs, perform calculations, and return outputs. 2) Coordinate transformations like rotation and translation can be represented using matrices. Rotation matrices rotate points around the origin, while translation matrices shift points along an axis. 3) Multiple transformations can be combined by multiplying the corresponding matrices. This allows rotating and translating points in one operation.

Uploaded by

Aldo Rod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Lecture 6: More Matlab Functions, Transformations: Professor Erik Cheever Course Web Page

This document provides an overview of the lecture topics on functions and transformations in MatLab. The key points are: 1) Functions allow repeating code for multiple cases without rewriting. Functions take in inputs, perform calculations, and return outputs. 2) Coordinate transformations like rotation and translation can be represented using matrices. Rotation matrices rotate points around the origin, while translation matrices shift points along an axis. 3) Multiple transformations can be combined by multiplying the corresponding matrices. This allows rotating and translating points in one operation.

Uploaded by

Aldo Rod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture 6: More MatLab; Functions, Transformations

Professor Erik Cheever

Course web page:


http://www.swarthmore.edu/NatSci/echeeve1/Class/e5/E5Index.html
Remember…
 Thursday 10/8: Bridge report is due.
 Thursday 10/8: First MatLab lab is due.

 Tuesday (8:30-10:00), Wednesday (8:00-10:00)


Wizards available in Hicks 211/213.

 Start thinking about your project (last 3 weeks of


class). Some ideas are at “Projects” link on left side
of course web page.
The big picture…
 Today:
 Matlab questions?
 MatLab functions.
 Graphical Transformations.
 This week in lab: functions and Graphical
Transformations.
 1st week after break: “Control of Flow” in MatLab.
 Following three weeks: design, build, and program a
physical robotic arm (using MatLab, functions,
transformations…).

 Last three weeks of class – individual projects. Some


ideas on “Projects” link on left of course web page.
Also…
 Skim (i.e., be familiar with what is there):
 Sections 3.2 and 3.6,
 Sections 4.2 and 4.4.1, and
 Section 6.2

 If you are anxious about programming, you may


want to try tutorials 2.5 and 2.6.
From Elektronik
(a Swedish Engineering Magazine)
… MatLab demo…
&
… MatLab questions…
Functions
Sometimes we would like to repeat a set of
commands repeatedly.

An example:
Suppose we want to find the perimeter of a shape
defined by a “patch.”

Recall – distance from (x1,y1) to (x2,y2) is


2 2
d x1 x2 y1 y2
Finding the perimeter d x1 x2
2
y1 y2
2

To find the perimeter we:


 find the difference between consecutive points
in x and in y,
 square each difference in x and in y,
 add the square for corresponding x and y
differences.
 take the square root of each of these to find the
distance between consecutive points, and
 sum these distances.
Finding perimeter (1) 3
>> %Define shape (a parallelogram)
2
>> x = [0 1 3 2]; y=[0 1 1 0];
>> myShape=patch(x,y,[0 0 1]); 1

>> axis([-3 3 -3 3],'square'); grid on 0

-1
>> %Difference between x values
>> xd=diff(x) -2

xd = 1 2 -1 -3
-3 -2 -1 0 1 2 3

This is no good because it leaves off difference between last


point and first point.
>> x1=[x x(1)]
x1 = 0 1 3 2 0

>> xd=diff(x1)
xd = 1 2 -1 -2
Finding perimeter (2) d x1 x2
2
y1 y2
2

>>>> x1=[x x(1)]; %Make new arrays


>> y1=[y y(1)]
y1 = 0 1 1 0 0

>> xd=diff(x1) %Find difference between consecutive points


>> yd=diff(y1)
yd = 1 0 -1 0

>> sqrd = xd.^2 + yd.^2 %Square difference and add (x^2+y^2)


sqrd = 2 4 2 4

>> sqrtSqd=sqrt(sqrd) %Get length of each side


sqrtSqd = 1.4142 2.0000 1.4142 2.0000

>> perim=sum(sqrtSqd) %Get perimeter


perim = 6.8284
Problems with this approach
 If we want to calculate the perimeter of 10
different shapes, we need to enter the code 10
different times.
 Even if we use a script file, we need to make 10
copies of the code
 If we find a mistake in the code, we need to
change the code every place it appears.

 We can fix all of these issues by defining a


“function”
Functions
 For our purposes a function is a special MatLab
file that does a calculation(s) and returns a
result(s)
 It is different from a script file:
 it returns a result
 it can’t change other variables that you are using
Anatomy of a Function
Keyword “function” tells us this is a function file (called “perim.m”)
Output variable(s) name(s); in this case only one: the variable “p”
Function name; also name of “.m” file Input variables

function p = perim(xvals,yvals)

x1=[xvals xvals(1)];%Form augmented vector


y1=[yvals yvals(1)];

xd=diff(x1); %Calculate differences


yd=diff(y1); Calculations

sqrd = xd.^2+yd.^2; %Square them


sqrtSqd=sqrt(sqrd); %Find distances

p=sum(sqrtSqd); %Find perimeter


Output variable (must be the same variable name as is in
function declaration)
Using Functions
Now we can call function as if it was built in:

>> q=perim(x,y)
q = 6.8284

A trick: if you add comments to the very top of the


function file, they will appear if you ask for
documentation (i.e., “>>doc perim”).
Caveats
 Function name must not be the same as a
variable name
 Function name must have no spaces in it
 Function must be in MatLab directory so MatLab
can find it.
 If you edit a function, you must save the file
before the changes will take effect in subsequent
calls
 If you edit a function, you must save the file
before the changes will take effect in subsequent
calls
Coordinate transformations
 Robot arm: transformation between joint
angles and x, y, z position of laser light
 Next two weeks in lab: we will use coordinate
transformations to move around objects.
From this… … to this
Rotation of a point (algebraic)
(rotation around the origin)

x  cos φ , y  sin φ
x'  cos θ φ , y'  sin θ φ

Trig identities :
cos θ φ cos φ cos θ sin φ sin θ
sin θ φ cos φ sin θ sin φ cos θ

x'  cos θ φ y'  sin θ φ


 cos φ cos θ  sin φ sin θ  cos φ sin θ  sin φ cos θ
x cos θ y sin θ x sin θ y cos θ
Image adapted from: http://www.cs.umu.se/kurser/TDBC07/HT04/handouts/HO-lecture7.pdf
Rotation of a point (2x2 matrix)
x' cos θ x sin θ y
y' sin θ x cos θ y

x' cos θ sin θ x


y' sin θ cos θ y

cos θ sin θ
rotation matrix=R
sin θ cos θ
Translation of a point (3x3)
x' x tx , y' y ty

How can we express this with matrices?

x' 1 0 tx x
y' 0 1 ty y
1 0 0 1 1

1 0 tx
0 1 ty translation matrix=M
0 0 1

Image adapted from: http://www.cs.umu.se/kurser/TDBC07/HT04/handouts/HO-lecture7.pdf


Rotation redefined (3x3)
We can combine a rotation (first)
with a translation (second).
This can be done in one operation if we
redefine the rotation matrix to be 3x3
cos θ sin θ 0
sin θ cos θ 0 rotation matrix=R
0 0 1

x' cos θ x sin θ y


y' sin θ x cos θ y

x' cos θ sin θ 0 x x


y' sin θ cos θ 0 y R y
1 0 0 1 1 1
Image adapted from: http://www.cs.umu.se/kurser/TDBC07/HT04/handouts/HO-lecture7.pdf
Rotation and translation (1)
We can combine a rotation (first)
with a translation (second).
x' x x
y' M R y MR y
1 1 1

x' 1 0 tx cos θ sin θ 0 x


y' 0 1 ty sin θ cos θ 0 y
1 0 0 1 0 0 1 1

x' cos θ sin θ tx x


y' sin θ cos θ ty y
1 0 0 1 1

Image adapted from: http://www.cs.umu.se/kurser/TDBC07/HT04/handouts/HO-lecture7.pdf


Rotation and translation (2)
cos θ sin θ tx
transformation
sin θ cos θ ty =T
matrix
0 0 1

x' cos θ sin θ tx x x


y' sin θ cos θ ty y T y
1 0 0 1 1 1

x' cos θ x sin θ y tx


y' sin θ x cos θ y ty

Image adapted from: http://www.cs.umu.se/kurser/TDBC07/HT04/handouts/HO-lecture7.pdf


Extension to many points
If we have several points (x1,y1), (x2,y2), (x3,y3), … (xn,yn), we can still
use the transformation matrix.

x1' x'2 x'3 xn' cos θ sin θ tx x1 x2 x3 xn


y1' y'2 y'3  yn' sin θ cos θ ty y1 y2 y3  yn
1 1 1 1 0 0 1 1 1 1 1

x1' cos θ x1 sin θ y1 tx


'
etc 
y 1 sin θ x1 cos θ y1 ty

If the points define a shape, we can rotate and translate that shape with a
single matrix multiplication.
… MatLab demo…
Other transformations…
(that we won’t be using)

Scaling

Shearing

Images from: http://www.cs.umu.se/kurser/TDBC07/HT04/handouts/HO-lecture7.pdf


Even more transformations…
(that we also won’t be using)

Reflection

Composite transformations (e.g., rotation about an arbitrary point (x1,y1)):


• Translate to origin
• Rotate
• Translate back

Images from: http://www.cs.umu.se/kurser/TDBC07/HT04/handouts/HO-lecture7.pdf

You might also like