0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ベクトル和を矢印で可視化する

Posted at

はじめに

訳あってベクトル和を可視化したくなりました.
検索してもやりたいことが出てこなかったので備忘録として残します.

やりたいこと

image.png

こんな感じでベクトル和を各ベクトルと一緒に表示したいです.

方法

入力 U, V はベクトルの成分表示とします.
始点の座標を表す配列 X, Ystarting_point() で取得します.
numpy の cumsum 関数は累積和を求める関数で便利です.

import matplotlib.pyplot as plt

def starting_point(U,V,x_start=0,y_start=0):
    # 始点なのでU,Vの最後は不要
    U2 = np.concatenate([[x_start], U[:-1]])
    V2 = np.concatenate([[y_start], V[:-1]])
    X = np.cumsum(U2)
    Y = np.cumsum(V2)
    return X, Y

def vector_sum(X,Y,U,V):
    x = X[0]
    y = Y[0]
    u = np.sum(U)
    v = np.sum(V)

    return x, y, u, v

U = np.array([1,2,-2,2])
V = np.array([1,3,5,1])
X, Y = starting_point(U, V, 1, 0.5)
x, y, u, v = vector_sum(X,Y,U,V)

plt.quiver(X,Y,U,V, angles='xy', scale_units='xy', scale=1)
plt.quiver(x,y,u,v,color=["red"], angles='xy', scale_units='xy', scale=1)
plt.xlim([-1,5])
plt.ylim([-1,12])
plt.show()

下図が得られます.

image.png

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?