Introduction

     When an object falls freely under the influence of gravity, its velocity and position change over time. In this Python code snippet, we explore the dynamics of such motion by calculating and tracking the velocity and position of the falling body.  


Code

    
     
    # Velocity and position of a freely falling body
    # v denotes velocity, x denotes displacement from the point of dropping,
    # t denotes the elapsed time, h is the increment in time (time-step), and
    # timeoffall is the total time for which the body is allowed to fall.
    import math
    
    # Initialize variables
    v = 0.0
    x = 0.0
    t = 0.0
    
    # Input time step and time of fall
    h = float(input("Enter time step: "))
    timeoffall = float(input("Enter time of fall: "))
    
    # Print header for output
    print("Time   Velocity   Position")
    
    # Loop until the elapsed time reaches the specified time of fall
    while (t - timeoffall) < 0.000001:
        # Print current time, velocity, and position
        print(f"{t:.2f}    {v:.2f}       {x:.2f}")
    
        # Update velocity and position
        v = v + (9.8 * h)
        x = x + (v * h)
    
        # Increment elapsed time
        t += h  
       
     

    This code calculates and displays the time, velocity, and position of a falling body at regular time intervals (h) until the specified total time of fall (timeoffall) is reached.

Output


    Enter time step: 0.1
    Enter time of fall: 1
    Time   Velocity   Position
    0.00    0.00       0.00
    0.10    0.98       0.10
    0.20    1.96       0.29
    0.30    2.94       0.59
    0.40    3.92       0.98
    0.50    4.90       1.47
    0.60    5.88       2.06
    0.70    6.86       2.74
    0.80    7.84       3.53
    0.90    8.82       4.41
    1.00    9.80       5.39
       




Explanation

1. Initialization:
  • v: Initial velocity of the falling body (initialized to 0.0).
  • x: Initial displacement from the point of dropping (initialized to 0.0).
  • t: Initial elapsed time (initialized to 0.0).
  • h: Time step (input by the user).
  • timeoffall: Total time for which the body falls (input by the user).
2. Input Time Step and Time of Fall:
  • The user is prompted to input the time step (h) and the total time for which the body falls (timeoffall).
3. Calculation and Output:
  • A while loop iterates until the elapsed time reaches the specified time of fall.
4. Output:
  • The time, velocity, and position of the body at each time step are printed.

Conclusion

     This Python code provides a simple yet effective simulation of a freely falling body's motion. By iteratively updating the velocity and position at regular time intervals, it offers insights into how these parameters evolve over time under the influence of gravity.