0
$\begingroup$

I am new to Mathematica and was trying some new things.

I wanted to rotate a rectangle using a rotation matrix by any angle theta from say o to 2pi.

I could do it using predefined Mathematica functions.

R = Rectangle[{-2.5, -0.5}, {2.5, 0.5}]; Animate[Graphics[GeometricTransformation[R, RotationMatrix[[Theta]]], Axes -> True, PlotRange -> {{-10, 10}, {-10, 10}}], {[Theta], 0, 2 [Pi]}]

But, say I want to write the 2x2 rotation matrix as a function and do it then how would i proceed?

I defined a 2x2 rotation matrix Rot and tried multiplying every coordinate of the rectangle, but I get an error and the rectangle doesn't rotate

I did the following:

Rot[[Theta]] = {{Cos[[Theta]], -Sin[[Theta]]}, {Sin[[Theta]], Cos[[Theta]]}};

R = Rectangle[{-2.5, -0.5}, {2.5, 0.5}]; Animate[Graphics[GeometricTransformation[R, Rot[[Theta]]], Axes -> True, PlotRange -> {{-10, 10}, {-10, 10}}], {[Theta], 0, 2 [Pi]}]

Is there a simple way to do this by just defining a function and then using it?enter image description here

$\endgroup$

closed as off-topic by MarcoB, Sjoerd C. de Vries, user9660, dr.blochwave, Oleksandr R. Nov 11 '15 at 9:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, Sjoerd C. de Vries, Community, dr.blochwave, Oleksandr R.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ 1. Don't use MatrixForm[] here, as it is only intended for output formatting. 2. RotationMatrix[] is built-in. $\endgroup$ – J. M. is away Nov 11 '15 at 4:41
  • $\begingroup$ I don't want to use the inbuilt Mathematica function RotationMatrix[]. But instead define a function for the 2x2 rotation matrix and then use it to rotate the rectangle as shown in the image. Thanks $\endgroup$ – Varun Kulkarni Nov 11 '15 at 4:47
  • $\begingroup$ Then, you can just do With[{θ = π/6}, Graphics[GeometricTransformation[Rectangle[{-2.5, -0.5}, {2.5, 0.5}], {{Cos[θ], -Sin[θ]}, {Sin[θ], Cos[θ]}}]]]. But, it seems you have a more fundamental problem. Execute Remove[Rot], and try using Rot[θ_] := {{Cos[θ], -Sin[θ]}, {Sin[θ], Cos[θ]}}. (Note the difference!) $\endgroup$ – J. M. is away Nov 11 '15 at 4:51
  • $\begingroup$ Thanks, but if I wanted to do something like this: Rot[[Theta]_] := {{Cos[[Theta]], -Sin[[Theta]]}, {Sin[[Theta]], Cos[[Theta]]}}; R = Rectangle[{-2.5, -0.5}, {2.5, 0.5}]; Animate[Show[Graphics[R, Rot[[Theta]]], Axes -> True, PlotRange -> {{-10, 10}, {-10, 10}}], {[Theta], 0, 2 [Pi]}] , Where initially I have defined a function then how do I proceed? It is similar to the image posted initially $\endgroup$ – Varun Kulkarni Nov 11 '15 at 4:56
  • $\begingroup$ It should work. From your screenshot, it seems you used Rot for something different, but forgot to clear it. $\endgroup$ – J. M. is away Nov 11 '15 at 5:05
3
$\begingroup$
Animate[
 Graphics[{Red, 
   GeometricTransformation[Rectangle[{-2.5, -0.5}, {2.5, 0.5}], 
    RotationMatrix[\[Theta]]]},
  Axes -> True, PlotRange -> {{-10, 10}, {-10, 10}}],
 {\[Theta], 0, 2 Pi}]

enter image description here

As per your comment, if you do not want to use the built in RotationMatrix then please see this:

enter image description here

...and this:

enter image description here

$\endgroup$
  • $\begingroup$ Thanks. But instead of using RotationMatrix[[Theta]], I want to define a 2x2 Rotation Matrix say Rot[[Theta]_] := {{Cos[[Theta]], -Sin[[Theta]]}, {Sin[[Theta]], Cos[[Theta]]}}; and then use this function to rotate the rectangle. Also, it would be helpful if I could use Show[] in Animate alongwith PlotRange Thanks $\endgroup$ – Varun Kulkarni Nov 11 '15 at 4:52
  • $\begingroup$ @VarunKulkarni explain why you want to use Show $\endgroup$ – Mike Honeychurch Nov 11 '15 at 4:55
  • $\begingroup$ R = Rectangle[{-2.5, -0.5}, {2.5, 0.5}]; Animate[Graphics[GeometricTransformation[R, RotationMatrix[[Theta]]], Axes -> True, PlotRange -> {{-10, 10}, {-10, 10}}], {[Theta], 0, 2 [Pi]}] This is how I have done , but now I want to define a function for that, and that function is a 2x2 Rotation matrix ....Heres what I am trying Rot[[Theta]_] := {{Cos[[Theta]], -Sin[[Theta]]}, {Sin[[Theta]], Cos[[Theta]]}}; R = Rectangle[{-2.5, -0.5}, {2.5, 0.5}]; Animate[Show[Graphics[R, Rot[[Theta]]], Axes -> True, PlotRange -> {{-10, 10}, {-10, 10}}], {[Theta], 0, 2 [Pi]}] $\endgroup$ – Varun Kulkarni Nov 11 '15 at 4:59
  • 2
    $\begingroup$ @VarunKulkarni before doing any of this you need to learn basic Mathematica syntax. {[Theta], 0, 2 [Pi]} is invalid, Sin[[Theta]] is invalid etc, Show is redundant, and so on $\endgroup$ – Mike Honeychurch Nov 11 '15 at 5:04
  • 1
    $\begingroup$ @MikeHoneychurch the user probably did not type this as it appears; the StackExchange software eats \ if one isn't careful. I think the syntax was most likely valid. $\endgroup$ – Oleksandr R. Nov 11 '15 at 9:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.