fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <vector>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. // Constants for the Lorenz system
  10. const double sigma = 10.0;
  11. const double rho = 28.0;
  12. const double beta = 8.0 / 3.0;
  13.  
  14. // Lorenz equations
  15. vector<double> lorenz(double x, double y, double z) {
  16. vector<double> dxdt(3);
  17. dxdt[0] = sigma * (y - x);
  18. dxdt[1] = x * (rho - z) - y;
  19. dxdt[2] = x * y - beta * z;
  20. return dxdt;
  21. }
  22.  
  23. // Euler method to solve the Lorenz system
  24. void euler_method(double x0, double y0, double z0, double dt, double t_max, const string& filename_prefix) {
  25. double t = 0.0;
  26. double x = x0, y = y0, z = z0;
  27. int frame = 0;
  28.  
  29. // Loop through time and output the state to files
  30. while (t < t_max) {
  31. stringstream filename;
  32. filename << filename_prefix << setw(4) << setfill('0') << frame << ".dat"; // Generate frame filename
  33. ofstream file(filename.str());
  34.  
  35. file << x << " " << y << " " << z << endl;
  36. file.close();
  37.  
  38. // Compute the next state using the Lorenz equations
  39. vector<double> dxdt = lorenz(x, y, z);
  40. x += dxdt[0] * dt;
  41. y += dxdt[1] * dt;
  42. z += dxdt[2] * dt;
  43.  
  44. t += dt;
  45. frame++;
  46. }
  47. }
  48.  
  49. int main() {
  50. // Initial conditions
  51. double x0 = 0.0, y0 = 1.0, z0 = 1.05;
  52. double dt = 0.01; // Time step
  53. double t_max = 100.0; // Total time
  54.  
  55. // Run the simulation and generate data for animation
  56. euler_method(x0, y0, z0, dt, t_max, "lorenz_frame_");
  57.  
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0.06s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty