Question: MAT 343 Laboratory 2 Linear transformations and computer animation in M ATLAB Bruno D. Welfert School of Mathematical & Statistical Sciences Goals In this laboratory

MAT 343 Laboratory 2 Linear transformations and computer animation in M ATLAB Bruno D. Welfert School of Mathematical & Statistical Sciences Goals In this laboratory session you learn how to Plot in M ATLAB Implement linear transformations in M ATLAB (rotation, reflection, scaling) Perform simple computer animation in M ATLAB Use homogeneous coordinates to create more advanced computer animations Create AVI movie in M ATLAB 2013 B. Welfert MAT 343 Laboratory 2 2/18 Plotting in M ATLAB Use plot to graph data/functions x = 0 : 0 . 2 : 1 0 ; % 51 v alues 0 , 0 . 2 , 0 . 4 , 0 . 6 , . . . , 1 0 . 0 f = @( x ) x . * s i n ( x ) . / ( 1 + x ) ; % i n l i n e f u n c t i o n % note . * and . / f o r componentwise * and / y = f ( x ) ; % evaluate f at l i s t x p l o t ( x , y ) ; % graph y data vs x data p l o t ( x , y , ' o ' ) ; % use marker o w i t h o u t c o n n e c t i n g p o i n t s z = s i n ( x ) ; % e v a l u a t e s i n a t x v alues p l o t ( x , y , ' o ' , x , z ) ; % superpose 2 p l o t s T = [ x ; y ] ; % 2x51 m a t r i x w i t h x i n row 1 and y i n row 2 p l o t ( T ( 1 , : ) , T ( 2 , : ) ) ; % p l o t 2nd row o f T vs 1 s t row It is also possible to do more sophisticated types of plots in MATLAB, including polar coordinates, three-dimensional surfaces, contour plots, . . . 2013 B. Welfert MAT 343 Laboratory 2 3/18 Example 1. Plot the triangle with vertices (0.5, 1), (0, 1), (0.5, 1) [ 0.5 , 0 , 0 . 5 ] ; % x data [ 1 , 1 , 1]; % y data [ x ; y ] ; % 2x3 m a t r i x w i t h x , y c o o r d i n a t e s [ T , T ( : , 1 ) ] ; % repeat 1 s t p o i n t to close t r i a n g l e p l o t ( T ( 1 , : ) , T ( 2 , : ) , ' l i n e w i d t h ' , 3 ) ; % draw t r i a n g l e % t i s handle t o g r a p h i c s o b j e c t a x i s equal ; % guarantees 11 aspect r a t i o a x i s ( [ 2 , 2 , 2 , 2 ] ) ; % v i s u a l i z a t i o n window g r i d on ; % add r e f e r e n c e g r i d p r i n t depsc t r i a n g l e . eps ; % o r djpeg90 t r i a n g l e . j p g x y T T t = = = = = 2013 B. Welfert MAT 343 Laboratory 2 4/18 2D rotations, reflections, scaling The matrix of a rotation of angle in the plane is \u0014 \u0015 cos sin A= sin cos The matrix of a reflection in the plane w.r.t. line y = x tan 2 is \u0014 \u0015 cos sin A= sin cos The matrix of a scaling in the plane by a factor sx in the x -direction and sy in the y -direction is \u0015 \u0014 s A= x sy \u0015 \u0014 x x xN simply multiply A and T To apply A to T = 1 2 y1 y2 yN AT = A*T; The matrix AT contains the coordinates of the transformed points 2013 B. Welfert MAT 343 Laboratory 2 5/18 Example 2. Rotate the triangle from Example 1 counterclockwise by 45o h o l d on ; % superpose on c u r r e n t p l o t ang = p i / 4 ; % r o t a t i o n angle o f 45 degrees c o u n t e r c l o c k w i s e A = [ cos ( ang ) , s i n ( ang ) ; s i n ( ang ) , cos ( ang ) ] ; % r o t a t i o n m a t r i x AT = A * T ; % a p p l y r o t a t i o n t o t r i a n g l e t 2 = p l o t ( AT ( 1 , : ) , AT ( 2 , : ) , ' r ' , ' l i n e w i d t h ' , 3 ) ; % r o t a t e d t r i a n g l e i n red % t 2 i s a handle t o t h e graph o f t h e r o t a t e d t r i a n g l e hold o f f ; % release p l o t 2013 B. Welfert MAT 343 Laboratory 2 6/18 Example 3. Reflect the triangle from Example 1 along the line y = x d e l e t e t 2 ; % d e l e t e r o t a t e d t r i a n g l e from Example 2 h o l d on ; ang = p i / 2 ; % t a n ( ang / 2 ) = 1 = s l o p e o f l i n e => ang /2= p i / 4 A = [ cos ( ang ) , s i n ( ang ) ; s i n ( ang ) , cos ( ang ) ] ; % r e f l e c t i o n AT = A * T ; % a p p l y r e f l e c t i o n t o t r i a n g l e t 3 = p l o t ( AT ( 1 , : ) , AT ( 2 , : ) , 'm ' , ' l i n e w i d t h ' , 3 ) ; % i n magenta hold o f f ; 2013 B. Welfert MAT 343 Laboratory 2 7/18 Example 4. Scale the triangle from Example 1 by sx = 2 and sy = .5 delete t3 ; h o l d on ; sx = 2 ; sy = . 5 ; % s c a l i n g f a c t o r s i n x and y d i r e c t i o n s A = [ sx , 0 ; 0 , sy ] ; % s c a l i n g m a t r i x AT = A * T ; % a p p l y s c a l i n g t o t r i a n g l e t 4 = p l o t ( AT ( 1 , : ) , AT ( 2 , : ) , ' k ' , ' l i n e w i d t h ' , 3 ) ; % s c a l e d i n b l a c k hold o f f ; 2013 B. Welfert MAT 343 Laboratory 2 8/18 Combinations of Transformations Transformations can be combined by successively applying each one A A 1 2 T A1 T A2 A1 T The resulting transformation is represented by A = A2 A1 Note that the first transformation appears on the right of the product of the matrix representations of the individual transformations 2013 B. Welfert MAT 343 Laboratory 2 9/18 Example 5. Scale the triangle from Example 1 horizontally by a factor 2, then rotate it by an angle = 45o . delete t4 ; h o l d on ; sx = 2 ; sy = 1 ; A1 = [ sx , 0 ; 0 , sy ] ; % s c a l i n g ang = p i / 4 ; A2 = [ cos ( ang ) , s i n ( ang ) ; s i n ( ang ) , cos ( ang ) ] ; % r o t AT = A2 * A1 * T ; % a p p l y A1 then A2 t o t r i a n g l e t 5 = p l o t ( AT ( 1 , : ) , AT ( 2 , : ) , ' k ' , ' l i n e w i d t h ' , 3 ) ; hold o f f ; 2013 B. Welfert MAT 343 Laboratory 2 10/18 Homogeneous coordinates What about rotations or scalings about a point other than the origin? What about reflections about lines not passing through the origin? What about translations? \u0014 \u0015 x x extend Homogeneous coordinates y y 1 The matrix of a standard transformations in homogeneous coordinates is Rotation Reflection Scaling Translation cos sin 0 cos sin 0 sx 0 0 1 0 tx sin cos 0 , sin cos 0 , 0 sy 0 , 0 1 ty 0 0 1 0 0 1 0 0 1 0 0 1 For example, to implement a rotation of angle 2 about (1, 0): (0, 1) x y x 1 Rot( ) 1y T (1,0) T (1,0) y y 2 x 1 x 1 1 1 =T (1,0) 1 1 1 b b (0, 1) 2013 B. Welfert MAT 343 Laboratory 2 11/18 Example 6. Rotate the triangle from Example 1 counterclockwise by an angle = 90o about (1, 0). delete t5 ; h o l d on ; tx = 1; ty = 0; A1 = [ 1 , 0 , t x ; 0 , 1 , t y ; 0 , 0 , 1 ] ; % t r a n s l a t i o n ang = p i / 2 ; A2 = [ cos ( ang) , s i n ( ang ) , 0 ; s i n ( ang ) , cos ( ang ) , 0 ; 0 , 0 , 1 ] ; % r o t a t i o n AT = A1 * ( A2 * ( A1\\T ) ) ; % (1 ,0) >(0 ,0) , r o t a t e , (0 ,0) >(1 ,0) t 6 = p l o t ( AT ( 1 , : ) , AT ( 2 , : ) , ' k ' , ' l i n e w i d t h ' , 3 ) ; hold o f f ; 2013 B. Welfert MAT 343 Laboratory 2 12/18 Animation/AVI movie a v i = V i d e o W r i t e r ( ' t r i a n g l e . a v i ' ) ; a v i . Q u a l i t y = 5 0 ; open ( a v i ) ; w r i t e V i d e o ( a v i , getframe ) ; N = 4 0 ; % number o f frames f o r each t r a n s f o r m a t i o n t x = 1 /N; t y = 0 ; A1 = [ 1 , 0 , t x ; 0 , 1 , t y ; 0 , 0 , 1 ] ; % s m a l l t r a n s l a t i o n ang = ( p i / 2 ) / N ; A2 = [ cos ( ang ) , s i n ( ang ) , 0 ; s i n ( ang ) , cos ( ang ) , 0 ; 0 , 0 , 1 ] ; % small r o t a t i o n f o r i = 1 :N T = A1\\T ; % (1 ,0) >(0 ,0) i n N s te p s s e t ( t , ' xdata ' ,T ( 1 , : ) , ' ydata ' , T ( 2 , : ) ) ; w r i t e V i d e o ( a v i , getframe ) ; pause ( 0 . 1 ) ; end f o r i = 1 :N T = A2 * T ; % r o t a t e 90 degrees i n N s te p s s e t ( t , ' xdata ' ,T ( 1 , : ) , ' ydata ' , T ( 2 , : ) ) ; w r i t e V i d e o ( a v i , getframe ) ; pause ( 0 . 1 ) ; end f o r i = 1 :N T = A1 * T ; % (0 ,0) >(1 ,0) i n N s te p s s e t ( t , ' xdata ' ,T ( 1 , : ) , ' ydata ' , T ( 2 , : ) ) ; w r i t e V i d e o ( a v i , getframe ) ; pause ( 0 . 1 ) ; end close ( a vi ) ; 2013 B. Welfert MAT 343 Laboratory 2 13/18 Instructions for the problems For each of the following problems: 1 Create an m-file to store the M ATLAB commands 2 Copy and paste the m-file into a text document 3 For Problems 1, 2, 3, include in the text document the pictures produced by M ATLAB. Resize and crop the pictures so that they do not take up too much space 4 If a question requires written answers, include them in the text file in the appropriate location 5 Make sure you clearly label and separate all the questions 6 For Problem 4 you do not need to include any picture. Instead include the submission data and time of your AVI movie 2013 B. Welfert MAT 343 Laboratory 2 14/18 Problem 1 Let f (x ) = x 2 + sin \u0012 \u0013 1 x a) Plot f for 0 < x 1. Use an appropriate window in both x and y directions b) Superpose a plot of the derivative of f f (x + h) f (x ) with h = 108 . h c) Plot g for 0 < x 1. Compare to b) Let g(x ) = d) What happens if instead h = 1015 ? 2013 B. Welfert MAT 343 Laboratory 2 15/18 Problem 2 Consider the original triangle T . Perform the following transformations: a) Rotate T by an angle of 180o b) Reflect T about the line y = x 3 c) Compose the transformations a) then b) d) Compose the transformations b) then a). Compare to c) e) Translate T by one unit in the positive x direction, then rotate T by an angle of 90o about (1, 0) f) Determine and implement a transformation which brings back T to its original position from the result of e) 2013 B. Welfert MAT 343 Laboratory 2 16/18 Problem 3 Consider the square S with vertices at (0, 0), (1, 0), (1, 1), and (0, 1). Perform the following transformations: a) Rotate S counterclockwise by an angle of 30o about (0, 0) b) Translate S horizontally by one unit to the right c) Rotate S counterclockwise by an angle of 60o about (1, 0) d) Compose the transformations a) then b) then c) e) Identify the transformation from d) and implement it directly with a single matrix 2013 B. Welfert MAT 343 Laboratory 2 17/18 Problem 4 Create an AVI movie PB4your name.avi implementing the transformations from Problem 3d) using N = 40 steps for each of the 2 rotations and the translation. Use a M ATLAB command like title ( ' my name') to include your name as a title of the figure used in the video Submit the resulting AVI file on Blackboard 2013 B. Welfert MAT 343 Laboratory 2 18/18

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!