fork(2) download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. // Constants
  7. const double PI = 3.14159265358979323846;
  8.  
  9. // Function to calculate a sine wave at position x and time t
  10. double sineWave(double A, double k, double omega, double phi, double x, double t) {
  11. return A * sin(k * x - omega * t + phi);
  12. }
  13.  
  14. int main() {
  15. // Parameters for wave 1
  16. double A1 = 1.0; // Amplitude of wave 1
  17. double k1 = 2 * PI / 10; // Wave number for wave 1 (wavelength = 10 units)
  18. double omega1 = 2 * PI / 5; // Angular frequency for wave 1 (period = 5 units)
  19. double phi1 = 0; // Phase of wave 1
  20.  
  21. // Parameters for wave 2
  22. double A2 = 0.8; // Amplitude of wave 2
  23. double k2 = 2 * PI / 12; // Wave number for wave 2 (wavelength = 12 units)
  24. double omega2 = 2 * PI / 6; // Angular frequency for wave 2 (period = 6 units)
  25. double phi2 = PI / 4; // Phase of wave 2
  26.  
  27. // Space and time parameters
  28. double t = 0; // Time (fixed for simplicity)
  29. double x_start = 0; // Starting position
  30. double x_end = 50; // Ending position
  31. int num_points = 1000; // Number of data points
  32.  
  33. std::vector<double> x_vals(num_points); // Positions
  34. std::vector<double> y_vals(num_points); // Resulting wave values
  35.  
  36. // Create a file to store the data (for plotting)
  37. std::ofstream outputFile("wave_superposition.txt");
  38.  
  39. // Generate x values and calculate corresponding wave values
  40. double dx = (x_end - x_start) / (num_points - 1); // Step size for x
  41. for (int i = 0; i < num_points; ++i) {
  42. double x = x_start + i * dx; // Current position
  43. x_vals[i] = x;
  44.  
  45. // Calculate individual waves
  46. double y1 = sineWave(A1, k1, omega1, phi1, x, t);
  47. double y2 = sineWave(A2, k2, omega2, phi2, x, t);
  48.  
  49. // Superimpose the waves
  50. y_vals[i] = y1 + y2;
  51.  
  52. // Write x and y values to the file for later plotting
  53. outputFile << x << "\t" << y_vals[i] << "\n";
  54. }
  55.  
  56. outputFile.close();
  57. std::cout << "Wave superposition data saved to wave_superposition.txt" << std::endl;
  58.  
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Wave superposition data saved to wave_superposition.txt