Untitled
unknown
plain_text
a year ago
1.5 kB
7
Indexable
import matplotlib.pyplot as plt
import numpy as np
# Given values
v0 = 30 # initial velocity in m/s
vf = 0 # final velocity in m/s
a = -6 # acceleration in m/s^2
d_total = 315 # total distance in meters
# Step 1: Calculate the distance while decelerating
d_slow = (v0**2) / (2 * abs(a)) # Using v_f^2 = v_0^2 + 2ad
# Step 2: Calculate the time while decelerating
t_slow = v0 / abs(a) # Using v_f = v_0 + at
# Step 3: Calculate the distance at constant velocity
d_constant = d_total - d_slow
# Step 4: Calculate the time at constant velocity
t_constant = d_constant / v0
# Step 5: Total time
t_total = t_slow + t_constant
# Plotting the journey
times = np.linspace(0, t_total, 500)
velocities = np.piecewise(
times,
[times <= t_constant, times > t_constant],
[lambda t: v0, lambda t: v0 + a * (t - t_constant)]
)
distances = np.piecewise(
times,
[times <= t_constant, times > t_constant],
[lambda t: v0 * t, lambda t: v0 * t_constant + v0 * (t - t_constant) + 0.5 * a * (t - t_constant)**2]
)
# Plotting velocity and distance
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(times, velocities, 'g-')
ax2.plot(times, distances, 'b-')
ax1.set_xlabel('Time (s)')
ax1.set_ylabel('Velocity (m/s)', color='g')
ax2.set_ylabel('Distance (m)', color='b')
plt.title("Car's Journey: Velocity and Distance over Time")
plt.show()
(d_slow, t_slow, d_constant, t_constant, t_total)
Editor is loading...
Leave a Comment