1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include <iostream> #include <cmath> #include <iomanip> // For std::fixed and std::setprecision const double G = 6.67430e-11; // Gravitational constant // Structure to represent a celestial body struct Body { double mass; // Mass of the body (in kg) double x; // X position (in meters) double y; // Y position (in meters) double vx; // Velocity in X direction (in meters/second) double vy; // Velocity in Y direction (in meters/second) }; // Function to update positions and velocities using simple Euler integration void updateBody(Body& body, double ax, double ay, double dt) { body.vx += ax * dt; body.vy += ay * dt; body.x += body.vx * dt; body.y += body.vy * dt; } // Function to compute the gravitational acceleration on a body due to another body void computeGravitationalAcceleration(const Body& body1, const Body& body2, double& ax, double& ay) { double dx = body2.x - body1.x; double dy = body2.y - body1.y; double distance = std::sqrt(dx * dx + dy * dy); double force = G * body1.mass * body2.mass / (distance * distance); double ax_temp = force * dx / (body1.mass * distance); double ay_temp = force * dy / (body1.mass * distance); ax = ax_temp; ay = ay_temp; } int main() { Body planet1, planet2; double dt; // Time step for simulation int steps; // Number of simulation steps // Input initial conditions std::cout << "Enter the mass of the first planet (kg): "; std::cin >> planet1.mass; std::cout << "Enter the initial x position of the first planet (m): "; std::cin >> planet1.x; std::cout << "Enter the initial y position of the first planet (m): "; std::cin >> planet1.y; std::cout << "Enter the initial velocity in x direction of the first planet (m/s): "; std::cin >> planet1.vx; std::cout << "Enter the initial velocity in y direction of the first planet (m/s): "; std::cin >> planet1.vy; std::cout << "Enter the mass of the second planet (kg): "; std::cin >> planet2.mass; std::cout << "Enter the initial x position of the second planet (m): "; std::cin >> planet2.x; std::cout << "Enter the initial y position of the second planet (m): "; std::cin >> planet2.y; std::cout << "Enter the initial velocity in x direction of the second planet (m/s): "; std::cin >> planet2.vx; std::cout << "Enter the initial velocity in y direction of the second planet (m/s): "; std::cin >> planet2.vy; std::cout << "Enter the time step for the simulation (s): "; std::cin >> dt; std::cout << "Enter the number of simulation steps: "; std::cin >> steps; // Simulation loop std::cout << "Step\tX1\tY1\tX2\tY2" << std::endl; for (int step = 0; step <= steps; ++step) { double ax1 = 0, ay1 = 0, ax2 = 0, ay2 = 0; // Compute gravitational acceleration on both planets computeGravitationalAcceleration(planet1, planet2, ax1, ay1); computeGravitationalAcceleration(planet2, planet1, ax2, ay2); // Update positions and velocities of both planets updateBody(planet1, ax1, ay1, dt); updateBody(planet2, ax2, ay2, dt); // Output positions of both planets std::cout << step << "\t" << std::fixed << std::setprecision(2) << planet1.x << "\t" << std::fixed << std::setprecision(2) << planet1.y << "\t" << std::fixed << std::setprecision(2) << planet2.x << "\t" << std::fixed << std::setprecision(2) << planet2.y << std::endl; } return 0; } |
Explanation:
- Headers and Constants:
- Includes
iostream
for input and output,cmath
for mathematical functions, andiomanip
for formatting. - Defines the gravitational constant
G
.
- Includes
- Body Structure:
- Represents a celestial body with mass, position (x, y), and velocity (vx, vy).
- updateBody Function:
- Updates the position and velocity of a body using simple Euler integration based on acceleration (
ax
,ay
) and time step (dt
).
- Updates the position and velocity of a body using simple Euler integration based on acceleration (
- computeGravitationalAcceleration Function:
- Computes the gravitational acceleration exerted on one body by another based on their masses, positions, and the gravitational constant.
- main Function:
- Prompts the user to input the masses, initial positions, and velocities of two planets.
- Reads the time step and number of simulation steps.
- Runs a simulation loop where it computes gravitational forces, updates positions and velocities, and prints the positions of both planets at each step.