Created
November 14, 2025 23:04
-
-
Save graysonhead/224810ebb92fe532444b56a45899d4eb to your computer and use it in GitHub Desktop.
Car Acceleration Integrator Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| python | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| def acceleration_curve(velocity): | |
| """ | |
| Realistic car acceleration that decreases with velocity. | |
| Models: engine power limits, air resistance, gearing | |
| """ | |
| max_accel = 10.0 # m/s^2 at low speed | |
| drag_coefficient = 0.05 | |
| # Acceleration decreases as velocity increases | |
| return max_accel * np.exp(-drag_coefficient * velocity) | |
| def simulate_car(dt, total_time): | |
| """Simulate car with given timestep""" | |
| time = 0 | |
| velocity = 0 | |
| position = 0 | |
| times = [0] | |
| velocities = [0] | |
| positions = [0] | |
| while time < total_time: | |
| # Get current acceleration | |
| accel = acceleration_curve(velocity) | |
| # Euler integration | |
| velocity += accel * dt | |
| position += velocity * dt | |
| time += dt | |
| times.append(time) | |
| velocities.append(velocity) | |
| positions.append(position) | |
| return times, velocities, positions | |
| # Simulate two cars | |
| total_time = 10.0 # seconds | |
| # Car A: updates every 1 second | |
| times_A, vels_A, pos_A = simulate_car(dt=1.0, total_time=total_time) | |
| # Car B: updates every 2 seconds | |
| times_B, vels_B, pos_B = simulate_car(dt=2.0, total_time=total_time) | |
| # Car C: "ground truth" with very small timestep | |
| times_C, vels_C, pos_C = simulate_car(dt=0.01, total_time=total_time) | |
| # Print results | |
| print(f"After {total_time} seconds:") | |
| print(f"Car A (dt=1.0s): Position = {pos_A[-1]:.2f}m, Velocity = {vels_A[-1]:.2f}m/s") | |
| print(f"Car B (dt=2.0s): Position = {pos_B[-1]:.2f}m, Velocity = {vels_B[-1]:.2f}m/s") | |
| print(f"Car C (dt=0.01s, 'truth'): Position = {pos_C[-1]:.2f}m, Velocity = {vels_C[-1]:.2f}m/s") | |
| print(f"\nCar B is ahead of Car A by: {pos_B[-1] - pos_A[-1]:.2f}m") | |
| print(f"Car B's error vs truth: {pos_B[-1] - pos_C[-1]:.2f}m") | |
| print(f"Car A's error vs truth: {pos_A[-1] - pos_C[-1]:.2f}m") | |
| # Plot results | |
| fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8)) | |
| # Position over time | |
| ax1.plot(times_C, pos_C, 'k-', linewidth=2, label='Ground Truth (dt=0.01s)', alpha=0.7) | |
| ax1.plot(times_A, pos_A, 'bo-', label='Car A (dt=1.0s)', markersize=8) | |
| ax1.plot(times_B, pos_B, 'rs-', label='Car B (dt=2.0s)', markersize=8) | |
| ax1.set_xlabel('Time (s)') | |
| ax1.set_ylabel('Position (m)') | |
| ax1.set_title('Drag Race: Position vs Time') | |
| ax1.legend() | |
| ax1.grid(True, alpha=0.3) | |
| # Velocity over time | |
| ax2.plot(times_C, vels_C, 'k-', linewidth=2, label='Ground Truth (dt=0.01s)', alpha=0.7) | |
| ax2.plot(times_A, vels_A, 'bo-', label='Car A (dt=1.0s)', markersize=8) | |
| ax2.plot(times_B, vels_B, 'rs-', label='Car B (dt=2.0s)', markersize=8) | |
| ax2.set_xlabel('Time (s)') | |
| ax2.set_ylabel('Velocity (m/s)') | |
| ax2.set_title('Velocity vs Time') | |
| ax2.legend() | |
| ax2.grid(True, alpha=0.3) | |
| plt.tight_layout() | |
| plt.savefig('euler_drag_race.png', dpi=150, bbox_inches='tight') | |
| print("\nPlot saved as 'euler_drag_race.png'") | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment