Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

1D Wave Equation Solver - Leapfrog Method

Note: This was a college project for a numerical methods course. It's a learning exercise, so don't take it too seriously! 🎓

Overview

This project implements a numerical solver for the 1D wave equation using the leapfrog method (also known as the Verlet method). The implementation solves the wave equation with fixed boundary conditions and compares the numerical solution against the analytical solution.

The Wave Equation

The code solves the 1D wave equation:

∂²u/∂t² = ∂²u/∂x²

With:

  • Boundary conditions: u(0,t) = u(1,t) = 0 (fixed endpoints)
  • Initial condition: u(x,0) = sin(πx)
  • Analytical solution: u(x,t) = cos(πt) × sin(πx)

Files

  • onda.c - Main wave simulation program

    • Simulates wave propagation over 1000 time steps
    • Outputs numerical solution, analytical solution, and error to onda.txt
  • leap1derro.c - Error analysis program

    • Tests the numerical method with varying time steps
    • Analyzes convergence and stability
    • Outputs error metrics to erro.txt

Compilation

Compile the programs using GCC:

# Compile the main wave simulation
gcc -o onda onda.c -lm

# Compile the error analysis program
gcc -o leap1derro leap1derro.c -lm

Note: The -lm flag is required to link the math library.

Usage

Run the wave simulation:

./onda

This generates onda.txt with columns:

  1. Spatial index (j)
  2. Numerical solution u(x,t)
  3. Analytical solution
  4. Error (difference between numerical and analytical)

Run the error analysis:

./leap1derro

This generates erro.txt with:

  1. Time step (k)
  2. Average error

Numerical Method

The leapfrog method is a second-order accurate, explicit time-stepping scheme:

u[n+1] = 2*u[n] - u[n-1] + k²*(u[j+1] - 2*u[j] + u[j-1])

Where:

  • u[n] is the solution at time step n
  • k is the time step size
  • j is the spatial index

Parameters

  • Spatial points: 50 (jmax = 50)
  • Time step (k): 1.0 (for onda.c), variable (for leap1derro.c)
  • Simulation time: 1000 time units (onda.c), 10 time units (leap1derro.c)

Visualizing Results

The output files (onda.txt and erro.txt) can be visualized using tools like:

  • gnuplot
  • MATLAB/Octave
  • Python (matplotlib, numpy)

Example gnuplot commands:

# Plot wave evolution
plot 'onda.txt' using 1:2 with lines title 'Numerical'

# Plot error vs time step
plot 'erro.txt' using 1:2 with points title 'Error'

Stability

The leapfrog method has a stability condition (CFL condition):

k ≤ h

Where h is the spatial step size (h = 1/(jmax-1) ≈ 0.02). The current configuration uses k=1.0 in onda.c, which may not satisfy the stability condition. For stable results, consider reducing the time step.

Known Issues

  • The time step k=1.0 in onda.c may be too large for stability
  • No command-line argument parsing (parameters are hardcoded)
  • Limited error handling for file I/O operations

License

This project is licensed under the terms specified in the LICENSE file.

Contributing

Since this is a completed college project, contributions are not being actively sought. However, feel free to fork and experiment!

References

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages