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

Post on 01-Apr-2018

226 views 2 download

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

Lecture4:MatrixTransformations,MatLabScripts&Functions,SerialCommunication

ProfessorCarrEverbach

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

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.

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.

From Elektronik (a Swedish Engineering Magazine)

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θ

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:

Translationofapoint(3x3)

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

How can we express this with matrices?

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

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

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

Othertransformations…

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

Scaling

Shearing

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

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

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

Thefor…loop(2)

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

Output: 1 5 9

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

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

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.

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

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

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

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

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)

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

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

SerialCommunicationinMatlab Matlabcommunicatesthroughtheserialportviafprintfandfscanfcommandswritingtoa“serialportobject.”

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

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

MatlabDemo…