gpt4 book ai didi

python - 参数函数求和

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:49 25 4
gpt4 key购买 nike

我正在处理这样的参数函数:

Parametric function

理想情况下,我想对重复的 x 轴求和,如示例所示。就是说,对于x~4.75,我看到函数可以是0.04、0.06或者0.16,我想在0.06+0.04+0.16 = 0.26的总和上加点。我需要对每个点都这样做,以便我可以构造一个函数,该函数是参数函数的一种“投影”。有人知道如何在 Python 中做到这一点吗?

最佳答案

看例子:

import numpy as np
import matplotlib.pyplot as plt

# set x, y
x = np.arange(-3.,3.,.1)
N = x.size
x[10:13] = x[10]
y = x ** 3 + np.random.rand(N)

# plot curve
fig, ax = plt.subplots()
plt.plot(x,y,'b-')
curve = ax.lines[0]

# get data of plotted curve
xvalues = curve.get_xdata()
yvalues = curve.get_ydata()

# get y for given x
indexes = np. where(xvalues == x[10])
# test print
print xvalues[indexes]
print yvalues[indexes]
print "Sum of y(x) = ",np.sum(yvalues[indexes]) , " where x = ", x[10]

# define markers
xm = []
ym = []

for x1 in x:
indexes = np.where(xvalues == x1)
print x1, yvalues[indexes]
if len(yvalues[indexes]) > 1:
xm += [xvalues[indexes],]
ym += [np.sum(yvalues[indexes]),]

plt.plot(xm, ym, linestyle = 'None', marker='o', color='g')

plt.show()

测试输出:

x: [-2. -2. -2.]
y: [-7.0936372 -7.42647923 -7.56571131]
Sum of y(x) = -22.0858277351 where x = -2.0

enter image description here

关于python - 参数函数求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37856680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com