Fourier Sine Series Examples

Remove["Global`*"]

Make things easy on ourselves, and just set the length of the string to unity

L=1;

Define a shape that is the string plucked at the center

pluck=Piecewise[{{2x/L,x<L/2},{2(1-x/L),x>L/2}},{x,0,L}];

Plot it

Plot[pluck,{x,0,L},PlotRange->{0,1},PlotStyle->Black]

Calculate the expansion coefficients for a truncated set of values of “n”

nMax=10;
nVals=Range[1,nMax];
AVals=(2/L)Integrate[pluck Sin[nVals Pi x/L],{x,0,L}]

Form the Fourier Sine expansion and plot it on top of the function

terms=AVals Sin[nVals Pi x/L]

u=Total[terms];
Plot[{pluck,u},{x,0,L},PlotStyle->{Black,Red}]

Now do the same for a square pulse in the center

square=Piecewise[{{0,x<L/3},{1,x>L/3&&x<2L/3},{0,x>2L/3}},{x,0,L}];
Plot[square,{x,0,L},PlotStyle->Black]

nMax=10;
nVals=Range[1,nMax];
AVals=(2/L)Integrate[square Sin[nVals Pi x/L],{x,0,L}]

terms=AVals Sin[nVals Pi x/L];
u=Total[terms];
Plot[{square,u},{x,0,L},PlotStyle->{Black,Red}]