This commit is contained in:
louiscklaw
2025-01-31 21:40:42 +08:00
parent 9a15bbea05
commit 04a4edce00
8 changed files with 308 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
#!/usr/bin/env python
import os,sys
from pprint import pprint
from math import sin, pi
import random
# ## Instructions (for reference):
# 1. Initialize the height of the particle to 100 meters.
h = 100
# 2. Use a while loop to simulate the motion of the particle until it hits the ground.
# 3. Inside the while loop, use an if-else statement to check if the particle has hit the ground.
# 4. Update the height of the particle using the equation of motion: h = hinitial 1/2 * g * (t)2.
# 5. Print the height of the particle at each time step.
# 6. Use a for loop to iterate through the time steps.
# h = h - 1/2 * g * t^2
# 1. Initialize the height from which the particle is released.
t_step = float(input("Enter the time step in seconds: "))
h_current = float(input("Enter the height in meters: "))
h_initial = h_current
t_current = 0
while (h_current >= 0):
# 4. Print the height of the particle at each time step until it hits the ground.
print(f'Time: {t_current:.1f} s, Height: {h_current:.2f} m')
# 2. Use a loop to update the position of the particle at each time step.
t_current += t_step
h_current = h_initial - (1/2 * 9.8 * (t_current**2))
# 3. Use an if-else statement to check if the particle has hit the ground.
if (h_current < 0):
print(f'Time: {(t_current):.1f} s, Height: {0:.2f} m')
print(f'has hit the ground')
break

View File

@@ -0,0 +1,119 @@
# Question 04:
Simulating the Motion of a Particle in a Gravitational Field
## Scenario:
Imagine a particle is released from a certain height above the ground and is subject to gravitational acceleration. The particle falls freely under gravity until it hits the ground. For simplicity, assume that air resistance is negligible, and the only force acting on the particle is gravity.
## Problem Statement:
You are required to write a program that simulates the motion of the particle.
1. Initialize the height from which the particle is released.
2. Use a loop to update the position of the particle at each time step.
3. Use an if-else statement to check if the particle has hit the ground.
4. Print the height of the particle at each time step until it hits the ground.
## Assumptions:
- Gravitational acceleration (g) is 9.8 m/s².
- The time step (Δt) for the simulation is 0.1 seconds and t is the current time.
- The initial height (h) from which the particle is released is 100 meters.
- Assume velocity and mass are ZERO (for ease of programming)
## Instructions (for reference):
1. Initialize the height of the particle to 100 meters.
2. Use a while loop to simulate the motion of the particle until it hits the ground.
3. Inside the while loop, use an if-else statement to check if the particle has hit the ground.
4. Update the height of the particle using the equation of motion: h = hinitial 1/2 _ g _ (t)2.
5. Print the height of the particle at each time step.
6. Use a for loop to iterate through the time steps.
Deliverables:
- A well-commented code that simulates the motion of the particle.
- A brief explanation of how the code works.
### Sample Output 1:
```
Enter the time step in seconds: 0.1
Enter the height in meters: 1
Time: 0.0 s, Height: 1.00 m
Time: 0.1 s, Height: 0.95 m
Time: 0.2 s, Height: 0.80 m
Time: 0.3 s, Height: 0.56 m
Time: 0.4 s, Height: 0.22 m
Time: 0.5 s, Height: 0.00 m
The particle has hit the ground.
```
### Sample Output 2:
```
Enter the time step in seconds: 0.2
Enter the height in meters: 2
Time: 0.0 s, Height: 2.00 m
Time: 0.2 s, Height: 1.80 m
Time: 0.4 s, Height: 1.22 m
Time: 0.6 s, Height: 0.24 m
Time: 0.8 s, Height: 0.00 m
The particle has hit the ground.
```
### Sample Output 3:
```
Enter the time step in seconds: 0.1
Enter the height in meters: 100
Time: 0.0 s, Height: 100.0 m
Time: 0.1 s, Height: 99.95 m
Time: 0.2 s, Height: 99.80 m
Time: 0.3 s, Height: 99.56 m
Time: 0.4 s, Height: 99.22 m
Time: 0.5 s, Height: 98.78 m
Time: 0.6 s, Height: 98.24 m
Time: 0.7 s, Height: 97.60 m
Time: 0.8 s, Height: 96.86 m
Time: 0.9 s, Height: 96.03 m
Time: 1.0 s, Height: 95.10 m
Time: 1.1 s, Height: 94.07 m
Time: 1.2 s, Height: 92.94 m
Time: 1.3 s, Height: 91.72 m
Time: 1.4 s, Height: 90.40 m
Time: 1.5 s, Height: 88.97 m
Time: 1.6 s, Height: 87.46 m
Time: 1.7 s, Height: 85.84 m
Time: 1.8 s, Height: 84.12 m
Time: 1.9 s, Height: 82.31 m
Time: 2.0 s, Height: 80.40 m
Time: 2.1 s, Height: 78.39 m
Time: 2.2 s, Height: 76.28 m
Time: 2.3 s, Height: 74.08 m
Time: 2.4 s, Height: 71.78 m
Time: 2.5 s, Height: 69.37 m
Time: 2.6 s, Height: 66.88 m
Time: 2.7 s, Height: 64.28 m
Time: 2.8 s, Height: 61.58 m
Time: 2.9 s, Height: 58.79 m
Time: 3.0 s, Height: 55.90 m
Time: 3.1 s, Height: 52.91 m
Time: 3.2 s, Height: 49.82 m
Time: 3.3 s, Height: 46.64 m
Time: 3.4 s, Height: 43.36 m
Time: 3.5 s, Height: 39.97 m
Time: 3.6 s, Height: 36.50 m
Time: 3.7 s, Height: 32.92 m
Time: 3.8 s, Height: 29.24 m
Time: 3.9 s, Height: 25.47 m
Time: 4.0 s, Height: 21.60 m
Time: 4.1 s, Height: 17.63 m
Time: 4.2 s, Height: 13.56 m
Time: 4.3 s, Height: 9.40 m
Time: 4.4 s, Height: 5.14 m
Time: 4.5 s, Height: 0.77 m
Time: 4.6 s, Height: 0.00 m
The particle has hit the ground.
```