2013-05-03 11 views
3

我有一個函數:生成隨機值從創建的函數的陣列被繪製

f = x**0.5*numpy.exp(-x/150) 

我用numpy的和matplot.lib生成的曲線圖˚F作爲x,其中x的函數:

x = np.linspace(0.0,1000.0, num=10.0) 

我在想如何創建一個隨機的x值的數組會創建這個函數使用我第一次創建的x數組相同的情節?

布萊恩

+1

因爲你真正想要的...你是什麼意思我很困惑「使用X數組我首次提出?」 – 2013-05-03 16:46:26

+0

對不起,我的意思是找到另一個數組(z)的值,這樣當我繪製z vs x時,它將看起來像f vs x。 – user1821176 2013-05-03 16:51:01

+0

那麼它不會是一個隨機數組,它將不得不被特別選擇。理論上它不可能是完全相同的,因爲那麼f必須等於z ...爲什麼你要這麼做呢? – 2013-05-03 16:53:18

回答

2

我不太清楚你問什麼,但它是那麼簡單,只需在您的「X」陣列希望非經常間距的點?

如果是這樣,考慮對隨機值數組進行累加求和。

作爲一個簡單的例子:

import numpy as np 
import matplotlib.pyplot as plt 

xmin, xmax, num = 0, 1000, 20 
func = lambda x: np.sqrt(x) * np.exp(-x/150) 

# Generate evenly spaced data... 
x_even = np.linspace(xmin, xmax, num) 

# Generate randomly spaced data... 
x = np.random.random(num).cumsum() 
# Rescale to desired range 
x = (x - x.min())/x.ptp() 
x = (xmax - xmin) * x + xmin 

# Plot the results 
fig, axes = plt.subplots(nrows=2, sharex=True) 
for x, ax in zip([x_even, x_rand], axes): 
    ax.plot(x, func(x), marker='o', mfc='red') 
axes[0].set_title('Evenly Spaced Points') 
axes[1].set_title('Randomly Spaced Points') 
plt.show() 

enter image description here