python - 如何在极坐标半圆管内用直角坐标画曲线?

标签 python math matplotlib polar-coordinates

import numpy as np
import matplotlib.pylab as plt

def tube():
    theta = np.linspace(0, np.pi/2, 30)

    x = np.cos(theta)
    y = np.sin(theta)
    z = x*0.8
    w = y*0.8

    plt.plot(z,w)
    plt.plot(x,y)
    plt.axis("equal")
    plt.show()

print plt.figure(1);tube()

enter image description here

def euler():
    A, B, a = 40, 10, 2

    t  = 10  # time
    dt = 1e-3 # interval

    nbpt = int(t/dt)

    n = 1
    s = 1. # sign of the derivative, initially chosen
    y = [0]*nbpt # result

    while n < nbpt:
        yp2 = B - A*y[n-1]**a
        if yp2 < 0:
            s = -s
            n -= 1 # recalculating the previous value
        else:
            y[n] = y[n-1] + dt*s*np.sqrt(yp2)
            n += 1

    plt.plot(np.linspace(0,t,nbpt),y)
    plt.show()

print plt.figure(2);euler()

enter image description here

我想在用tube()制作的 pipe 中绘制用euler()制作的曲线。我想我必须从笛卡尔坐标转到极坐标,但是有没有办法用 Python 使这个过程更容易?

最佳答案

有很多方法可以做到这一点,因为这个问题并没有完全确定您正在寻找什么转换。但是,假设只要生成的曲线在管的边界线之间振荡,任何变换都会进行,您可以使用:

def polarmap(x, y):
    # normalize x and y from 0 to 1
    x = (x-x.min())/(x.max()-x.min())
    y = (y-y.min())/(y.max()-y.min())

    # make theta go from 0 to pi/2
    theta = np.pi*x/2

    # make r go from 0.8 to 1.0 (the min and max tube radius)
    r = 0.2*y + 0.8

    # convert polar to cartesian
    x = r*np.cos(theta)
    y = r*np.sin(theta)
    plt.plot(x, y)
<小时/>

例如,

import numpy as np
import matplotlib.pylab as plt


def tube():
    theta = np.linspace(0, np.pi/2, 30)

    x = np.cos(theta)
    y = np.sin(theta)
    z = x*0.8
    w = y*0.8

    plt.plot(z,w)
    plt.plot(x,y)

def euler():
    A, B, a = 40, 10, 2

    t  = 10  # time
    dt = 1e-3 # interval

    nbpt = int(t/dt)

    n = 1
    s = 1. # sign of the derivative, initially chosen
    y = [0]*nbpt # result

    while n < nbpt:
        yp2 = B - A*y[n-1]**a
        if yp2 < 0:
            s = -s
            n -= 1 # recalculating the previous value
        else:
            y[n] = y[n-1] + dt*s*np.sqrt(yp2)
            n += 1

    x = np.linspace(0,t,nbpt)
    y = np.array(y)
    return x, y

def polarmap(x, y):
    # normalize x and y from 0 to 1
    x = (x-x.min())/(x.max()-x.min())
    y = (y-y.min())/(y.max()-y.min())

    # make theta go from 0 to pi/2
    theta = np.pi*x/2

    # make r go from 0.8 to 1.0 (the min and max tube radius)
    r = 0.2*y + 0.8

    # convert polar to cartesian
    x = r*np.cos(theta)
    y = r*np.sin(theta)
    plt.plot(x, y)

fig, ax = plt.subplots()
tube()
x, y = euler()
polarmap(x, y)
plt.axis("equal")
plt.show()

产生 enter image description here

<小时/>

请注意,在 polarmap 中,第一步是标准化 xy,因此 它们的范围都在 0 到 1 之间。您可以将它们视为相等的参数 立足点。如果您在将两个参数传递给 polarmap 之前交换这两个参数,例如:

x, y = euler()
x, y = y, x    # swap x and y
polarmap(x, y)

然后你就得到了

enter image description here

关于python - 如何在极坐标半圆管内用直角坐标画曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009252/

相关文章:

python - 获取刚在Python中启动的程序的pid

Python多线程访问同一个文件

python - cygwin 运行命令而不是 cmd python

python - 如何将积分放入 python/matplotlib 的函数中

math - 对称级别索引算法(替代 float )的好处是什么?

iphone - iOS——实现复数

python - 额外的 x-ticks 不会随着 pandas 情节而消失

python - matplotlib seaborn 长行名影响其他子图的轴

python - 多次显示绘图窗口

python - 在单元测试中模拟对象时避免输入类型警告?