Home:ALL Converter>plot Fourier expansion with gnuplot

plot Fourier expansion with gnuplot

Ask Time:2019-02-11T19:15:49         Author:NBur

Json Formatter

After computing the Fourier coefficients of my function, I'd like to plot the first terms of the serie. However, I can't get the correct result…

It's not a matter of wrong coefficients as it plots correctly https://www.desmos.com/calculator/dh84khkc1o With the gnuplot code below

set terminal pngcairo
set output 'Fourier.png'
set samples 2000;

aa = -pi/2;
bb = pi/2;
repete(x) = (x-(bb-aa)*floor((x-aa)/(bb-aa)));
ff(x) = (-pi/2<x) && (x<0) ? x-cos(x)+1 : ((0<=x) && (x<pi/2)) ? x+cos(x)-1: 0;
fourier(k, x) = ((1-pi/2)*((-1)**k)+1/(4*k**2-1)) * sin(2*k*x) / k;

plot ff(repete(x)), 2/pi*sum [k=1:50] fourier(k,x)

I've got the discontinuities, but the “cos” part become a straight line.

enter image description here

Author:NBur,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54629366/plot-fourier-expansion-with-gnuplot
yy