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

时间:2016-08-26 23:29:15

标签: python numpy matplotlib vector rotational-matrices

我创建了一个向量(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()

3 个答案:

答案 0 :(得分:1)

在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非常不同。

答案 1 :(得分:0)

如有疑问,请在交互式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

答案 2 :(得分:0)

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)
相关问题