3
def parabola(h, k, xCoordinates):

h 是抛物线与 x 轴接触的 x 坐标,k 是抛物线与 y 轴相交的 y 坐标,xCoordinates 是沿主轴的 x 坐标列表。该函数使用下面显示的等式返回 y 坐标列表。x 坐标列表中的每个 x 坐标都有一个 y 坐标。

y(x, h, k) = a(x − h)2, where a =k/h2

我知道如何在 python 中工作,因为我已经计算了面积,

def computeArea(y_vals, h):
    i=1
    total=y_vals[0]+y_vals[-1]
    for y in y_vals[1:-1]:
        if i%2 == 0:
            total+=2*y
        else:
            total+=4*y
        i+=1
    return total*(h/3.0)
y_values=[13, 45.3, 12, 1, 476, 0]
interval=1.2
area=computeArea(y_values, interval)
print "The area is", area

但是上面的问题伤害了我,因为它纯粹是数学,我只是想要一点帮助

4

2 回答 2

2

您可以使用**幂运算符对值进行平方:

y = (k / h ** 2) * (x - h) ** 2

其中**求幂的优先级高于乘法或除法。

所以对于一系列x坐标,那就是:

def parabola(h, k, xCoordinates):
    return [(k / h ** 2) * (x - h) ** 2 for x in xCoordinates]
于 2013-03-23T13:50:53.060 回答
2

Martijn Pieters 给出的答案很好。

如果你对这个概念有点困惑,我发现这个例子很容易理解(使用顶点形式方程):

x = range(-10,10)
y = []

a = 2 # this is the positive or negative curvature
h = 0 # horizontal offset: make this term +/- to shift the curve "side to side"
k = 0 # vertical offset: make this term +/- to shift the curve "up to down"

for xi in x:
    y.append(a * (xi + h)** 2 + k)

您可以使用 pylab 绘制它。

于 2013-07-16T22:32:24.333 回答