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 92 93 94 |
#include <iostream> #include <cmath> #include <vector> #include <iomanip> class NewtonsCradleSimulator { private: double gravity; // Acceleration due to gravity double damping; // Damping factor to simulate energy loss double mass; // Mass of each ball double length; // Length of the string double angle; // Initial angle from vertical double angularVelocity; // Initial angular velocity struct Ball { double angle; double angularVelocity; }; std::vector<Ball> balls; public: // Constructor NewtonsCradleSimulator(double gravity, double damping, double mass, double length, double angle, double angularVelocity, int numBalls) : gravity(gravity), damping(damping), mass(mass), length(length), angle(angle), angularVelocity(angularVelocity) { balls.resize(numBalls, {angle, angularVelocity}); } // Function to simulate the motion of the cradle void simulate(double totalTime, double timeStep) { int steps = static_cast<int>(totalTime / timeStep); std::cout << "Time\tBall Angles\n"; std::cout << "---------------------\n"; for (int step = 0; step <= steps; ++step) { double time = step * timeStep; updateMotion(timeStep); printState(time); } } private: // Function to update the motion of the balls void updateMotion(double timeStep) { for (auto& ball : balls) { double angularAcceleration = -gravity / length * std::sin(ball.angle) - damping * ball.angularVelocity; ball.angularVelocity += angularAcceleration * timeStep; ball.angle += ball.angularVelocity * timeStep; } } // Function to print the current state of the simulation void printState(double time) const { std::cout << std::fixed << std::setprecision(2) << time << "\t"; for (const auto& ball : balls) { std::cout << ball.angle << " "; } std::cout << "\n"; } }; int main() { double gravity, damping, mass, length, angle, angularVelocity, totalTime, timeStep; int numBalls; // Input parameters from the user std::cout << "Enter the acceleration due to gravity (m/s^2): "; std::cin >> gravity; std::cout << "Enter the damping factor: "; std::cin >> damping; std::cout << "Enter the mass of each ball (kg): "; std::cin >> mass; std::cout << "Enter the length of the string (m): "; std::cin >> length; std::cout << "Enter the initial angle from vertical (radians): "; std::cin >> angle; std::cout << "Enter the initial angular velocity (rad/s): "; std::cin >> angularVelocity; std::cout << "Enter the number of balls: "; std::cin >> numBalls; std::cout << "Enter the total time for the simulation (s): "; std::cin >> totalTime; std::cout << "Enter the time step for the simulation (s): "; std::cin >> timeStep; // Create a NewtonsCradleSimulator object NewtonsCradleSimulator simulator(gravity, damping, mass, length, angle, angularVelocity, numBalls); // Run the simulation simulator.simulate(totalTime, timeStep); return 0; } |