Python 如何在极坐标系下的半圆管中绘制笛卡尔坐标系下的曲线?

Python 如何在极坐标系下的半圆管中绘制笛卡尔坐标系下的曲线?,python,math,matplotlib,polar-coordinates,Python,Math,Matplotlib,Polar Coordinates,def euler(): A、 B,A=40,10,2 t=10次 dt=1e-3#间隔 nbpt=int(t/dt) n=1 s=1.#初始选择的导数符号 y=[0]*nbpt#结果 当n

def euler():
A、 B,A=40,10,2
t=10次
dt=1e-3#间隔
nbpt=int(t/dt)
n=1
s=1.#初始选择的导数符号
y=[0]*nbpt#结果
当n


我想在使用
tube()
制作的管中绘制使用
euler()
制作的曲线。我想我必须从笛卡尔坐标转换到极坐标,但是有没有办法让Python的转换过程更简单呢?

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

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

比如说,

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)
然后你得到


谢谢!还有什么其他方法在讨论呢?
euler()
返回的第一点是
(0,0)
。现在还不清楚这个点应该映射到管道内部(或管道上)的确切位置。它应该像我上面选择的那样转到
(0.9,0)
,或者
(1,0)
(可能,在θ中有相移),或者
(0.8,0)
,或者
(0,1)
或者
(0,0.8)
,等等。有无限的可能性。在
theta
r
中添加非线性变换(这可能会以不寻常的方式扭曲或拉长曲线)和
r,可能性是无穷的。好吧,我明白了。再次感谢:)
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()
x, y = euler()
x, y = y, x    # swap x and y
polarmap(x, y)