参数函数和

2024-04-16 10:01:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我处理的是这样一个参数函数:

Parametric function

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


Tags: 函数示例参数情况投影理想
1条回答
网友
1楼 · 发布于 2024-04-16 10:01:57

看看这个例子:

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

相关问题 更多 >