繁体   English   中英

使用极坐标在 Python 中绘制相位图

[英]Plotting phase portraits in Python using polar coordinates

我需要以极坐标形式给出的以下非线性系统的相位图...

\dot{r} = 0.5*(r - r^3)
\点{\theta} = 1

我知道如何在数学中做到这一点......

field1 = {0.5*(r - r^3), 1};
p1 = StreamPlot[Evaluate@TransformedField["Polar" -> "Cartesian", field1, {r, \[Theta]} -> {x, y}], {x, -3, 3}, {y, -3, 3}, Axes -> True, StreamStyle -> Gray, ImageSize -> Large];
Show[p1, AxesLabel->{x,y}, ImageSize -> Large]

在此处输入图像描述

如何在 Python 中使用 pyplot.quiver 来做同样的事情?

只是非常幼稚的实现,但可能会有所帮助......

import numpy as np
import matplotlib.pyplot as plt

def dF(r, theta):
    return 0.5*(r - r**3), 1

X, Y = np.meshgrid(np.linspace(-3.0, 3.0, 30), np.linspace(-3.0, 3.0, 30))
u, v = np.zeros_like(X), np.zeros_like(X)
NI, NJ = X.shape

for i in range(NI):
    for j in range(NJ):
        x, y = X[i, j], Y[i, j]
        r, theta = (x**2 + y**2)**0.5, np.arctan2(y, x)
        fp = dF(r, theta)
        u[i,j] = (r + fp[0]) * np.cos(theta + fp[1]) - x
        v[i,j] = (r + fp[0]) * np.sin(theta + fp[1]) - y

plt.streamplot(X, Y, u, v)
plt.axis('square')
plt.axis([-3, 3, -3, 3])
plt.show()

在此处输入图像描述

更正上一个答案:

  • x=r*cos(theta)得到dx = dr*cos(theta)-r*sin(theta)*dtheta = x*dr/ry*dtheta
  • y=r*sin(theta)得到dy = dr*sin(theta)+r*cos(theta)*dtheta = y*dr/r+x*dtheta
  • 可以使用 numpy 的矢量化操作来避免所有循环
def dF(r, theta):
    return 0.5*r*(1 - r*r), 1+0*theta

X, Y = np.meshgrid(np.linspace(-3.0, 3.0, 30), np.linspace(-3.0, 3.0, 30))
R, Theta = (X**2 + Y**2)**0.5, np.arctan2(Y, X)
dR, dTheta = dF(R, Theta)
C, S = np.cos(Theta), np.sin(Theta)
U, V = dR*C - R*S*dTheta, dR*S+R*C*dTheta

plt.streamplot(X, Y, U, V, color='r', linewidth=0.5, density=1.6)
plt.axis('square')
plt.axis([-3, 3, -3, 3])
plt.show()

这给出了下面的 plot。 使用streamplotdensity选项来增加 plot 线的密度。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM