Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding the vertex, focus and directrix of a parabola in Python Program
In this article, we will learn about finding the vertex, focus and directrix of a parabola using Python. A parabola is a U-shaped curve that can be represented by the standard form equation y = ax² + bx + c.
Understanding Parabola Components
The vertex is the point where the parabola changes direction (minimum or maximum point).
The focus is a point inside the parabola from which distances to any point on the curve have special properties.
The directrix is a horizontal line outside the parabola used to define the curve geometrically.
Mathematical Formulas
For a parabola y = ax² + bx + c:
- Vertex: (-b/(2a), (4ac - b²)/(4a))
- Focus: (-b/(2a), (4ac - b² + 1)/(4a))
- Directrix: y = (4ac - b² - 1)/(4a)
Python Implementation
def find_parabola_properties(a, b, c):
# Calculate vertex coordinates
vertex_x = -b / (2 * a)
vertex_y = (4 * a * c - b * b) / (4 * a)
# Calculate focus coordinates
focus_x = vertex_x
focus_y = vertex_y + 1 / (4 * a)
# Calculate directrix equation
directrix_y = vertex_y - 1 / (4 * a)
print(f"Parabola: y = {a}x² + {b}x + {c}")
print(f"Vertex: ({vertex_x:.4f}, {vertex_y:.4f})")
print(f"Focus: ({focus_x:.4f}, {focus_y:.4f})")
print(f"Directrix: y = {directrix_y:.4f}")
# Example usage
a = 1
b = -4
c = 3
find_parabola_properties(a, b, c)
Parabola: y = 1x² + -4x + 3 Vertex: (2.0000, -1.0000) Focus: (2.0000, -0.7500) Directrix: y = -1.2500
Another Example
Let's try with different coefficients ?
def find_parabola_properties(a, b, c):
vertex_x = -b / (2 * a)
vertex_y = (4 * a * c - b * b) / (4 * a)
focus_x = vertex_x
focus_y = vertex_y + 1 / (4 * a)
directrix_y = vertex_y - 1 / (4 * a)
print(f"Parabola: y = {a}x² + {b}x + {c}")
print(f"Vertex: ({vertex_x:.4f}, {vertex_y:.4f})")
print(f"Focus: ({focus_x:.4f}, {focus_y:.4f})")
print(f"Directrix: y = {directrix_y:.4f}")
# Example with different values
a = 2
b = -8
c = 6
find_parabola_properties(a, b, c)
Parabola: y = 2x² + -8x + 6 Vertex: (2.0000, -2.0000) Focus: (2.0000, -1.8750) Directrix: y = -2.1250
Complete Program with Input Validation
def find_parabola_properties(a, b, c):
if a == 0:
print("Error: 'a' cannot be zero. This would not be a parabola.")
return
# Calculate properties
vertex_x = -b / (2 * a)
vertex_y = (4 * a * c - b * b) / (4 * a)
focus_x = vertex_x
focus_y = vertex_y + 1 / (4 * a)
directrix_y = vertex_y - 1 / (4 * a)
# Display results
print(f"\nParabola equation: y = {a}x² + {b}x + {c}")
print(f"Vertex: ({vertex_x:.4f}, {vertex_y:.4f})")
print(f"Focus: ({focus_x:.4f}, {focus_y:.4f})")
print(f"Directrix: y = {directrix_y:.4f}")
# Determine if parabola opens upward or downward
direction = "upward" if a > 0 else "downward"
print(f"Parabola opens: {direction}")
# Test with multiple examples
examples = [(1, -2, 1), (2, 4, 1), (-1, 2, 3)]
for a, b, c in examples:
find_parabola_properties(a, b, c)
print("-" * 40)
Parabola equation: y = 1x² + -2x + 1 Vertex: (1.0000, 0.0000) Focus: (1.0000, 0.2500) Directrix: y = -0.2500 Parabola opens: upward ---------------------------------------- Parabola equation: y = 2x² + 4x + 1 Vertex: (-1.0000, -1.0000) Focus: (-1.0000, -0.8750) Directrix: y = -1.1250 Parabola opens: upward ---------------------------------------- Parabola equation: y = -1x² + 2x + 3 Vertex: (1.0000, 4.0000) Focus: (1.0000, 3.7500) Directrix: y = 4.2500 Parabola opens: downward ----------------------------------------
Conclusion
This program calculates the vertex, focus, and directrix of a parabola using the standard quadratic equation coefficients. The vertex represents the turning point, while the focus and directrix help define the parabola's geometric properties.
