# Category: Differential Equations
# Name: Fourier series animation (Gibbs phenomenon)
#
# Animation where a discontinuity appears and disappears to exhibit
# Gibbs phenomenon
#

# The computation can be slow, so be patient when running...

# The function (h is a parameter)
function F(x) = (
    while x < -pi do increment x by 2*pi;
    while x > pi do increment x by -2*pi;
  
    if x < 0 then 2*h*(x+pi/2)/pi else 2-h
);
  
  
LinePlotWindow=[-pi*1.1,pi*1.1,-3,3];
LinePlotDrawLegends=false;

# For faster animation, precompute,
print("Precomputing Fourier Series...");
hvals = [0.0:0.1:2.0,2.0:-0.1:0.0];
for n=1 to elements(hvals) do (
    h = hvals@(n);
    printn (n + "/" + elements(hvals) + "...");
    fs@(n) = NumericalFourierSeriesFunction (F, pi, 10);
);
print("Done... Starting animation...");

PlotWindowPresent(); # Make sure the window is raised

for k = 1 to 10 do (
    for n = 1 to elements(hvals) do (
        h = hvals@(n);
        LinePlot(F, fs@(n))
    )
)
