Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts &...

26
Lecture 4: Matrix Transformations, MatLab Scripts & Functions, Serial Communication Professor Carr Everbach Course web page: http://www.swarthmore.edu/NatSci//Classceverba1/e5/E5Index.html

Transcript of Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts &...

Page 1: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Lecture4:MatrixTransformations,MatLabScripts&Functions,SerialCommunication

ProfessorCarrEverbach

Coursewebpage:http://www.swarthmore.edu/NatSci//Classceverba1/e5/E5Index.html

Page 2: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

E5… Comments on Response Readings  Thursday 9/23: Matlab I is due.  Thursday 9/30: MatLab II lab is due.

 Monday (7:00-10:00), Wednesday (7:00-10:00) Wizards available in Trotter 201.

  This week in lab: make things out of stuff. If time, start assembling servos into robotic arm.

Page 3: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Also… Possibly helpful sections of Pratap:

  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.

Page 4: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

From Elektronik (a Swedish Engineering Magazine)

Page 5: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Rotationofapoint(algebraic)(rotationaroundtheorigin)

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

x = lcosφ;y = lsinφ

x'= lcos(θ + φ);y'= lsin(θ + φ)Trig identities:

cos(θ + φ) = cosφ cosθ − sinφ sinθ

sin(θ + φ) = cosφ sinθ + sinφ cosθ

x'= lcos(θ + φ)= lcosφ cosθ − lsinφ sinθ= x cosθ − y sinθ

y'= lsin(θ + φ)= lcosφ sinθ + lsinφ cosθ= x sinθ + y cosθ

Page 6: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Rotationofapoint(2x2matrix)abouttheorigin

x'= x cosθ − y sinθy'= x sinθ + y cosθ

x 'y '

=

cosθ −sinθsinθ cosθ

xy

cosθ −sinθsinθ cosθ

= RotationMatrix = R

Where:

Page 7: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Translationofapoint(3x3)

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

How can we express this with matrices?

Page 8: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Rotationredefined(3x3)

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

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θ 0sinθ cosθ 00 0 1

= RotationMatrix = R

x 'y '1

=

cosθ −sinθ 0sinθ cosθ 00 0 1

xy1

= R •

xy1

Page 9: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

CombinedRotationandTranslation

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

x 'y '1

=

1 0 tx0 1 ty0 0 1

cosθ −sinθ 0sinθ cosθ 00 0 1

xy1

= M •R •

xy1

We can combine a rotation (first) with a translation (second).

x 'y '1

=

cosθ −sinθ txsinθ cosθ ty0 0 1

xy1

Page 10: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

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

If the points define a shape, we can rotate and translate that shape with a single matrix multiplication.

x1' x2

' x3'

y1' y2

' y3'

1 1 1...xn'

yn'

1

=

cosθ −sinθ txsinθ cosθ ty0 0 1

x1 x2 x3y1 y2 y31 1 1

...xnyn1

Page 11: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Othertransformations…

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

Scaling

Shearing

Page 12: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Composite transformations (e.g., rotation about an arbitrary point (x1,y1)): •  Translate to origin •  Rotate •  Translate back

Evenmoretransformations…

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

Reflection

Page 13: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

ControlofflowinMatLab  So far we have used MatLab as a fancy calculator   The real power comes when we can have MatLab:

  Perform operations many times consecutively:   for… loop

 Make decisions:   if…then…else…   if…then…elseif…then…elseif… … …else…   while… loop

Page 14: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Thefor…loop(1)

Example: for i=1:10, disp(i) end

Output: 1 2 3 4 5 6 7 8 9 10

Syntax: for variable=start:finish, …commands… end

Page 15: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Thefor…loop(2)

Example: for i=1:4:10, disp(i) end

Output: 1 5 9

Syntax: for variable=start:increment:finish, …commands… end

Page 16: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

MakingDecisionsinMatlab:if Syntax: if expression, … statements … end

Example: if a>b, disp('a is greater than b'); end

if (a>b) & (a>0), disp('a>b and a is positive'); end

expression has to evaluate to TRUE or FALSE

Page 17: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Logicalexpressions

a>b TRUE when a is greater than b, otherwise FALSE

expression has to evaluate to TRUE or FALSE

a<b TRUE when a is less than b, otherwise FALSE

a>=b TRUE when a is greater than or equal to b, otherwise FALSE

a==b TRUE when a is equal to b, otherwise FALSE

a~=b TRUE when a is not equal to b, otherwise FALSE

(a>0) & (b>0) TRUE when a is greater than 0 and b is greater than zero, otherwise FALSE

(a>0) | (b>0) TRUE when a is greater than 0 or b is greater than zero, otherwise FALSE

(a>0) | ~(b>0)TRUE when a is greater than 0 or b is not greater than zero, otherwise FALSE

Be careful if a or b are matrices, surprising results can occur!

Note: this list is not exhaustive, check you text for more.

Page 18: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

MakingDecisions:if … else Syntax: if expression, … statements … else … statements end

Example: if a>b, disp('a is greater than b'); else disp('a is less than or equal to b'); end

Page 19: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

MakingDecisions:if … elseif … else Syntax: if expression1, … statements … elseif expression2, … statements … else … statements … end

Example: if a>b, disp('a is greater than b'); elseif a<b, disp('a is less than b'); else disp('a is equal to b'); end

Page 20: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

MakingDecisions:while… Syntax: while expression, … statements … end

Example: i=1; while i<10, disp(i); i=i+4; end

Output: 1 5 9 Note: this is equivalent to the for…loop used earlier

Page 21: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

Functions For our purposes a function is a special MatLab

file that does a calculation(s) and returns a result   It is different from a script file:

  it returns a result   it can’t change other variables that you are using

Page 22: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

AnatomyofaFunction

function p = perim(xvals,yvals)

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

xd=diff(x1); %Calculate differences yd=diff(y1);

sqrd = xd.^2+yd.^2; %Square them sqrtSqd=sqrt(sqrd); %Find distances

p=sum(sqrtSqd); %Find perimeter

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

Calculations

Output variable (must be the same variable name as is in function declaration)

Page 23: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

UsingFunctionsNow 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”).

Page 24: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

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

Page 25: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

SerialCommunicationinMatlab Matlabcommunicatesthroughtheserialportviafprintfandfscanfcommandswritingtoa“serialportobject.”

  s=serial(‘COM1’);%createsaserialportobject  set(s,‘Baudrate’,38400);%setsbaudrateofCOM1  fprintf(s,‘#0P1500’);%writestextinsinglequotestoCOM1serialportoutput

  a=fscanf(s);%readstextfromCOM1serialportandputstheresultinarray‘a’

Page 26: Lecture 4: Matrix Transformations, MatLab Scripts ... · Matrix Transformations, MatLab Scripts & Functions, ... How can we express this with matrices? ... Note: this list is not

MatlabDemo…