Ticker

6/recent/ticker-posts

How to visualize the Fourier series

 

visualize the Fourier series Results 

In this blog, of Physics blog we are going to learn how to visualize the Fourier Series. it is a step-by-step guide for how you can visualize your result on a computer. let's start 

visualize the Fourier series Results


To visualize the Fourier series approximation alongside the original function, you can output the results to a file and use a plotting tool like GNUplot, Matplotlib (Python), or even online graphing tools. Here's a step-by-step guide using Matplotlib:


Step 1: Modify the C++ Program to Save Data

Update your program to write the original function and Fourier approximation values to a file.

Add this to your main function:

cpp
#include <fstream> // Include for file handling int main() { int N = 10; // Number of Fourier terms int numSteps = 1000; // Steps for numerical integration double a0; std::vector<double> an(N), bn(N); // Compute coefficients computeCoefficients(a0, an, bn, N, numSteps); // Open a file to save data std::ofstream file("fourier_data.csv"); file << "x,original,approximation\n"; // Test the Fourier series and write data for (double x = -M_PI; x <= M_PI; x += 0.1) { double approx = fourierSeries(x, a0, an, bn, N); file << x << "," << f(x) << "," << approx << "\n"; } file.close(); std::cout << "Data saved to fourier_data.csv" << std::endl; return 0; }

Step 2: Compile and Run the Program

Compile and run the updated program:

bash
g++ fourier_series.cpp -o fourier_series -lm ./fourier_series

This creates a file named fourier_data.csv with three columns:

  • xx values
  • Original function values
  • Fourier approximation values

Step 3: Visualize Using Python and Matplotlib

Install Matplotlib if you don’t already have it:

bash
pip install matplotlib

Now, create a Python script (e.g., plot_fourier.py) to visualize the data:

python
import matplotlib.pyplot as plt import numpy as np # Load the data data = np.loadtxt("fourier_data.csv", delimiter=",", skiprows=1) x = data[:, 0] original = data[:, 1] approximation = data[:, 2] # Plot the original function and the Fourier approximation plt.figure(figsize=(10, 6)) plt.plot(x, original, label="Original Function", color="blue", linewidth=2) plt.plot(x, approximation, label="Fourier Approximation", color="red", linestyle="--", linewidth=2) # Add labels, legend, and title plt.title("Fourier Series Approximation", fontsize=16) plt.xlabel("x", fontsize=14) plt.ylabel("f(x)", fontsize=14) plt.legend(fontsize=12) plt.grid(True) # Show the plot plt.show()

Step 4: Run the Visualization

Run the Python script:

bash
python plot_fourier.py

This will generate a graph showing the original function and the Fourier series approximation.


Step 5: Optional Enhancements

  • Fine-tune the graph: Adjust line styles, colors, or fonts.
  • Compare different NN: Save Fourier approximations for multiple values of NN and plot them together.
  • Export the graph: Save it as an image using plt.savefig("fourier_plot.png") before plt.show().

By visualizing, you can see how the Fourier series converges to the original function as NN increases

Post a Comment

0 Comments