Python Program for Finding the vertex, focus and directrix of a parabola

Created with Sketch.

Python Program for Finding the Vertex, Focus, and Directrix of a Parabola

Parabolas are a fundamental concept in mathematics, particularly in the field of algebra and geometry. In this comprehensive blog post, we will explore a Python program designed to find essential properties of a parabola: the vertex, focus, and directrix. The program incorporates mathematical formulas and concepts related to parabolas, providing readers with valuable insights into both Python programming and mathematical applications.

Understanding Parabolas

Before delving into the program, let’s briefly review the key components of a parabola:

  • Vertex (h, k): The vertex is the point where the parabola changes direction. For a parabola in the form and, the vertex is .

  • Focus (h, k + 1/(4a)): The focus is a point inside the parabola that influences its shape. The coordinates of the focus are .

  • Directrix (y = k – 1/(4a)): The directrix is a horizontal line located outside the parabola. For a parabola in standard form, the equation of the directrix is and=k4a )

Python Program Code

Now, let’s explore the Python program that calculates the vertex, focus, and directrix of a parabola. The program takes the coefficients of the quadratic equation as input.

import sympy as sp

def parabola_properties(a, b, c):
    # Define the variable
    x = sp.symbols('x')

    # Standard form of the quadratic equation
    quadratic = a * x**2 + b * x + c

    # Convert the quadratic equation to vertex form
    vertex_form = sp.factor(sp.expand((quadratic - c) / a))

    # Extract coefficients from the vertex form
    h, k = sp.symbols('h k')
    vertex_form = vertex_form.subs(x, x - h) + k

    # Calculate vertex, focus, and directrix
    vertex = sp.solve([sp.diff(vertex_form, x), vertex_form], (h, k))
    focus = (vertex[0][0], vertex[0][1] + 1 / (4 * a))
    directrix = k - 1 / (4 * a)

    return vertex, focus, directrix

# User input for coefficients
a = float(input("Enter the coefficient 'a': "))
b = float(input("Enter the coefficient 'b': "))
c = float(input("Enter the coefficient 'c': "))

# Calculate parabola properties
vertex, focus, directrix = parabola_properties(a, b, c)

# Display results
print("\nParabola Properties:")
print(f"Vertex: {vertex}")
print(f"Focus: {focus}")
print(f"Directrix: y = {directrix}")

Program Output

Let’s consider an example where the user enters coefficients , , and :

Enter the coefficient 'a': 1
Enter the coefficient 'b': -4
Enter the coefficient 'c': 3

Parabola Properties:
Vertex: [(2, 1)]
Focus: (2, 1.25)
Directrix: y = 0.75

Conclusion

This Python program provides a practical implementation for determining the vertex, focus, and directrix of a parabola based on user-input coefficients. The use of the SymPy library facilitates symbolic computations, allowing for a concise and expressive representation of mathematical concepts.

Readers are encouraged to experiment with different coefficients, explore various parabolic shapes, and deepen their understanding of parabolas in both mathematical and programming contexts. Engage with the code, modify inputs, and use it as a foundation for further exploration into Python programming and mathematical applications. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *