繁体   English   中英

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

[英]How to draw a curve in Cartesian coordinates in a semicircle tube in 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()

在此处输入图片说明

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

在此处输入图片说明

我想在用tube()制成的tube()绘制用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()

这产生在此处输入图片说明


请注意,在polarmap ,第一步是对xy进行归一化,使它们的范围都在 0 到 1 之间。您可以将它们视为平等的参数。 如果在将它们传递给polarmap之前交换两个参数,例如:

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

然后你得到

在此处输入图片说明

暂无
暂无

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

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