4

我有一个功能:

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 数组为该函数创建相同的图?

布莱恩

4

1 回答 1

3

我不太清楚你在问什么,但它只是想要你的“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()

在此处输入图像描述

于 2013-05-03T17:14:20.010 回答