将旋转矩阵应用于向量 + 绘制它

Apply rotation matrix to vector + plot it

我创建了一个向量 (v) 并想对其执行 rotMatrix 函数。我无法弄清楚如何在向量 (v) 上调用度数为 30 的函数 rotMatrix。我也在绘制矢量。

这是我的代码:

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()

在您的 rotMatrix 函数中,您使用了 arcsin() 函数。您想使用 -sin() 您还应该将度数转换为弧度

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

或通过

稍微提高效率和可读性
c = np.cos(np.radians(angle))
s = np.sin(np.radians(angle))
return np.array([[c, -s], [s, c]])

以及与

的通话
rotMatrix(30).dot(v)

-sin 和 arcsin 非常不同。

如有疑问,请在交互式 Python/numpy 会话中尝试计算。

In [23]: 30/180*np.pi       # do it yourself convsion - easy
Out[23]: 0.5235987755982988
In [24]: np.radians(30)     # degrees to radians - samething
Out[24]: 0.52359877559829882
In [25]: np.sin(np.radians(30))    # sin(30deg)
Out[25]: 0.49999999999999994

enter image description here 将 numpy 导入为 np 将 matplotlib.pyplot 导入为 plt

A = np.array([[3],[-3]])

n = np.linspace(0,2*np.pi,100)

def rotate_me(A,n):
  fig, (ax1, ax2) = plt.subplots(nrows=1, ncols = 2, figsize=(18, 16))

  buf1 = []
  buf11 = []
  buf12 = []
  buf13 = []
  buf1p = []
  buf2 = []
#   t = []

  for theta in n:

    x=2
    x1=3
    x2=2
    x3=3
#     xp=3
#     theta = 1/p
    origin = [0],[0]
    R = np.array([[x*np.cos(theta),-np.sin(theta)],
                 [np.sin(theta),np.cos(theta)]])
    R1 = np.array([[np.cos(theta),-np.sin(theta)],
             [np.sin(theta),np.cos(theta)*x1]])
    R2 = np.array([[np.cos(theta),-np.sin(theta)],
             [x2*np.sin(theta),np.cos(theta)]])
    R3 = np.array([[np.cos(theta),-np.sin(theta)*x3],
             [np.sin(theta),np.cos(theta)]])
    Rp = np.array([[np.cos(theta),-np.sin(theta)],
             [np.sin(theta),np.cos(theta)]])

    V = R.dot(A)
    V1 = R1.dot(A)
    V2 = R2.dot(A)
    V3 = R3.dot(A)
    Vp = Rp.dot(A)

    buf1.append(np.linalg.norm(V))
    buf11.append(np.linalg.norm(V1))
    buf12.append(np.linalg.norm(V2))
    buf13.append(np.linalg.norm(V3))
    buf1p.append(np.linalg.norm(Vp))
#     buf2.append(np.linalg.norm(A))

    ax1.quiver(*origin,V[0,:],V[1,:], scale=21,color ='r',label='cos')
    ax1.quiver(*origin,V1[0,:],V1[1,:], scale=21,color ='g',label='cos')
    ax1.quiver(*origin,V2[0,:],V2[1,:], scale=21,color ='m',label='sin')
    ax1.quiver(*origin,V3[0,:],V3[1,:], scale=21,color ='b',label='-sin')
    ax1.quiver(*origin,Vp[0,:],Vp[1,:], scale=21,color ='k',label='pure')
#     print(theta)
#     ax1.legend()
  ax2.plot(n,buf1,color ='r')
  ax2.plot(n,buf11,color ='g')
  ax2.plot(n,buf12,color ='m')
  ax2.plot(n,buf13,color ='b')
  ax2.plot(n,buf1p,color ='k')
#   ax2.plot(n,buf2,color ='b')

  ax2.set_xlabel('angle')
  ax2.set_ylabel('magnitude')


  plt.show()
#   return buf1,buf2

rotate_me(A,n)