#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>

// Constants
const double PI = 3.14159265358979323846;

// Function to calculate a sine wave at position x and time t
double sineWave(double A, double k, double omega, double phi, double x, double t) {
    return A * sin(k * x - omega * t + phi);
}

int main() {
    // Parameters for wave 1
    double A1 = 1.0;      // Amplitude of wave 1
    double k1 = 2 * PI / 10; // Wave number for wave 1 (wavelength = 10 units)
    double omega1 = 2 * PI / 5; // Angular frequency for wave 1 (period = 5 units)
    double phi1 = 0;      // Phase of wave 1

    // Parameters for wave 2
    double A2 = 0.8;      // Amplitude of wave 2
    double k2 = 2 * PI / 12; // Wave number for wave 2 (wavelength = 12 units)
    double omega2 = 2 * PI / 6; // Angular frequency for wave 2 (period = 6 units)
    double phi2 = PI / 4; // Phase of wave 2

    // Space and time parameters
    double t = 0;   // Time (fixed for simplicity)
    double x_start = 0; // Starting position
    double x_end = 50;  // Ending position
    int num_points = 1000; // Number of data points

    std::vector<double> x_vals(num_points);  // Positions
    std::vector<double> y_vals(num_points);  // Resulting wave values

    // Create a file to store the data (for plotting)
    std::ofstream outputFile("wave_superposition.txt");

    // Generate x values and calculate corresponding wave values
    double dx = (x_end - x_start) / (num_points - 1); // Step size for x
    for (int i = 0; i < num_points; ++i) {
        double x = x_start + i * dx;   // Current position
        x_vals[i] = x;

        // Calculate individual waves
        double y1 = sineWave(A1, k1, omega1, phi1, x, t);
        double y2 = sineWave(A2, k2, omega2, phi2, x, t);

        // Superimpose the waves
        y_vals[i] = y1 + y2;

        // Write x and y values to the file for later plotting
        outputFile << x << "\t" << y_vals[i] << "\n";
    }

    outputFile.close();
    std::cout << "Wave superposition data saved to wave_superposition.txt" << std::endl;

    return 0;
}
