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
Python Program for the focal length of a spherical mirror
In this article, we will learn about calculating the focal length of a spherical mirror using Python. We'll explore both concave and convex mirrors with their respective formulas.
Problem Statement
We are given the radius of curvature of a spherical mirror and need to find its focal length.
The focal length is the distance between the center of curvature of the mirror to the principal focus. To determine the focal length of a spherical mirror, we need to know its radius of curvature − the distance from the vertex of the mirror to the center of curvature.
Mathematical Formulas
The focal length depends on the type of spherical mirror ?
- For concave mirror: F = R ÷ 2
- For convex mirror: F = -R ÷ 2
Where F is the focal length and R is the radius of curvature.
Python Implementation
Let's implement functions to calculate focal length for both types of mirrors ?
# Function for spherical concave mirror
def focal_length_concave(radius):
return radius / 2
# Function for spherical convex mirror
def focal_length_convex(radius):
return -(radius / 2)
# Driver code
radius_of_curvature = 30
concave_focal = focal_length_concave(radius_of_curvature)
convex_focal = focal_length_convex(radius_of_curvature)
print(f"Focal length of spherical concave mirror: {concave_focal} units")
print(f"Focal length of spherical convex mirror: {convex_focal} units")
Focal length of spherical concave mirror: 15.0 units Focal length of spherical convex mirror: -15.0 units
Enhanced Version with Input Validation
Here's a more robust version that handles different mirror types and validates input ?
def calculate_focal_length(radius, mirror_type):
"""
Calculate focal length of a spherical mirror
Parameters:
radius (float): Radius of curvature
mirror_type (str): 'concave' or 'convex'
Returns:
float: Focal length
"""
if radius <= 0:
raise ValueError("Radius must be positive")
if mirror_type.lower() == 'concave':
return radius / 2
elif mirror_type.lower() == 'convex':
return -(radius / 2)
else:
raise ValueError("Mirror type must be 'concave' or 'convex'")
# Test with different values
test_cases = [
(20, 'concave'),
(40, 'convex'),
(15, 'concave'),
(25, 'convex')
]
for radius, mirror_type in test_cases:
focal = calculate_focal_length(radius, mirror_type)
print(f"R = {radius} cm, {mirror_type} mirror ? f = {focal} cm")
R = 20 cm, concave mirror ? f = 10.0 cm R = 40 cm, convex mirror ? f = -20.0 cm R = 15 cm, concave mirror ? f = 7.5 cm R = 25 cm, convex mirror ? f = -12.5 cm
Key Points
- Concave mirrors have positive focal lengths (converging)
- Convex mirrors have negative focal lengths (diverging)
- The focal length is always half the radius of curvature
- Units can be in meters, centimeters, or millimeters
Conclusion
Calculating the focal length of spherical mirrors is straightforward using the relationship f = R/2 for concave mirrors and f = -R/2 for convex mirrors. The sign convention helps distinguish between converging and diverging mirrors.
