Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute Parabola using python

def parabola(h, k, xCoordinates):

h is the x coordinate where the parabola touches the x axis and k is the y coordinate where the parabola intersects the y axis and xCoordinates is a list of x coordinates along the major axis. The function returns a list of y coordinates using the equation shown below. There will be one y coordinate for each x coordinate in the list of x coordinates.

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

I know how to work in python as i already compute the area ,

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

But the question above is hurting me because its pure mathmatics , i just want little bit help


1 Answers

You can use the ** power operator to square values:

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

where ** exponentiation has a higher precedence than multiplication or division.

So for a series of x coordinates, that'd be:

def parabola(h, k, xCoordinates):
    return [(k / h ** 2) * (x - h) ** 2 for x in xCoordinates]
like image 184
Martijn Pieters Avatar answered Apr 17 '26 13:04

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!