3.16 HW
- Sim #1 updates 2 variables by a random amount at each iteration (runs for 100 iterations)
- Sim #2 an object falling under the influence of gravity
- Example of simulation
import random
# Define the number of iterations
num_iterations = 100
# Set the initial values of the variables
value_1 = 0
value_2 = 0
# Define the update function
def update_values():
global value_1
global value_2
value_1 += random.randint(-1, 1)
value_2 += random.randint(-1, 1)
# Run the simulation for the specified number of iterations
for i in range(num_iterations):
update_values()
print(f"Iteration {i}: value_1 = {value_1}, value_2 = {value_2}")
import matplotlib.pyplot as plt
# Define the initial conditions
x = 0
y = 0
vx = 0
vy = 0
g = 9.8
# Set the time step and number of iterations
dt = 0.1
num_iterations = 100
# Define the update function
def update_position():
global x, y, vx, vy
x += vx * dt
y += vy * dt
vy -= g * dt
# Run the simulation for the specified number of iterations
for i in range(num_iterations):
update_position()
# Plot the results
plt.plot(x, y)
plt.show()