2017-02-02 21 views
1
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() 

私はtube()で作られたチューブにeuler()で作られた曲線を描きたいです。私はデカルト座標から極座標に移動しなければならないと思いますが、とにかくPythonでプロセスを簡単にするためにそこにはありますか?

答えて

1

質問には、あなたが探している変換を完全に把握していないため、多くの方法があります。しかし、任意の変換をと仮定すると結果として得られる曲線は、チューブの境界線の間を振動する限り行います、あなたが使用できます。たとえば

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) 

enter image description hereを生み出す

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


最初のステップは、xyの両方を正規化することでした。したがって、 は両方とも0から1までの範囲であることを示しています。これらは、 脚注のパラメータと考えることができます。あなたが例えば、polarmapに渡す前に2つのパラメータを交換する場合:

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

あなたは

enter image description here

+0

を得るありがとうございます!他にどんな方法で話していますか? –

+0

'euler()'によって返される最初の点は '(0,0)'です。この点がどこで管の内側(または上)にマッピングされるべきかは明確ではない。上で選択した '(0.9、0)'や '(1,0)'(シータの位相シフトあり)、 '(0.8,0)'、 '' 0,1 ) 'や'(0,0.8) 'などがあります。無限の可能性があります。 'theta'(これは異常な方法でカーブを伸ばしたり伸ばすことができる)や' r'の非線形変換に加えて、可能性は無限です。 – unutbu

+0

わかりました。ありがとうございました :) –

関連する問題