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 |
#include <iostream> #include <cmath> #include <iomanip> // Constants const double PI = 3.141592653589793; const double ORBIT_RADIUS = 1000.0; // Radius of the orbit in kilometers const double ORBIT_PERIOD = 3600.0; // Orbit period in seconds (e.g., 1 hour) // Function to calculate the satellite's position based on time void calculatePosition(double time, double& x, double& y) { double angularVelocity = 2 * PI / ORBIT_PERIOD; double angle = angularVelocity * time; x = ORBIT_RADIUS * std::cos(angle); y = ORBIT_RADIUS * std::sin(angle); } int main() { // Simulation parameters double startTime = 0.0; // Start time in seconds double endTime = 7200.0; // End time in seconds (e.g., 2 hours) double timeStep = 600.0; // Time step in seconds (e.g., 10 minutes) std::cout << "Time (s)\tX Position (km)\tY Position (km)\n"; std::cout << "----------------------------------------------\n"; // Run the simulation for (double time = startTime; time <= endTime; time += timeStep) { double x, y; calculatePosition(time, x, y); std::cout << std::fixed << std::setprecision(2) << time << "\t\t" << std::fixed << std::setprecision(2) << x << "\t\t" << std::fixed << std::setprecision(2) << y << "\n"; } return 0; } |
Explanation
- Constants:
- PI: Mathematical constant π.
- ORBIT_RADIUS: Radius of the satellite’s orbit around the planet in kilometers.
- ORBIT_PERIOD: Time taken for one complete orbit in seconds (e.g., 1 hour).
Advertisement - Function
calculatePosition(double time, double& x, double& y)
:- Purpose: Calculates the satellite’s position in its orbit at a given time.
- Parameters:
time
: Current time in seconds.x
andy
: Output parameters for the satellite’s position coordinates.
- Implementation:
- Angular Velocity: Calculated as
2 * PI / ORBIT_PERIOD
, representing the rate of angular change. - Angle Calculation: Angle of the satellite’s position is
angularVelocity * time
. - Position Calculation: Uses trigonometric functions to determine the satellite’s position in a circular orbit.
- Angular Velocity: Calculated as
Advertisement - Main Function:
- Simulation Parameters:
startTime
: Beginning of the simulation in seconds.endTime
: End of the simulation in seconds.timeStep
: Increment in time for each step of the simulation.
- Simulation Loop:
- Position Calculation: Calls
calculatePosition
to determine the satellite’s position at each time step. - Result Display: Outputs the time and satellite’s position in the orbit.
Advertisement - Position Calculation: Calls
- Simulation Parameters:
Usage
- Satellite Orbit Simulation: Models the position of a satellite in a circular orbit around a planet.
- Position Calculation: Calculates and displays the satellite’s position over time based on circular motion principles.