Search code examples
pythonnumpymatplotlibvectorrotational-matrices

Apply rotation matrix to vector + plot it


I have created a vector (v) and would like to perform the rotMatrix function on it. I cannot figure out how to call the function rotMatrix with a degree of 30 on the vector (v). I am also plotting the vectors.

Here is my code:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("white")
import math

def rotMatrix(angle):
    return np.array([[np.cos(np.degrees(angle)), np.arcsin(np.degrees(angle))], [np.sin(np.degrees(angle)), np.cos(np.degrees(angle))]])

v = np.array([3,7])
v30 = rotMatrix(np.degrees(30)).dot(v)

plt.arrow(0,0,v[0],v[1], head_width=0.8, head_length=0.8)
plt.arrow(0,0,v30[0],v30[1],head_width=0.8, head_length=0.8)
plt.axis([-5,5,0,10])
plt.show()

Solution

  • In your rotMatrix function you have used the arcsin() function. You want to use -sin() You should also convert your degrees value to radians

    return np.array([[np.cos(np.radians(angle)), 
                           -np.sin(np.radians(angle))],
                     [np.sin(np.radians(angle)),
                            np.cos(np.radians(angle))]])
    

    Or slightly improve efficiently and readability by

    c = np.cos(np.radians(angle))
    s = np.sin(np.radians(angle))
    return np.array([[c, -s], [s, c]])
    

    and the call with

    rotMatrix(30).dot(v)
    

    -sin and arcsin are very different.