ClearAll["Global`*"]

Examination of In-Class Example for Fourier Series of Square Wave

In class, we determined that a[0] = (1/2), and that a[n] = 0.
We also discovered that b[n] = ((-2)/(nπ)) if n is odd, and that b[n] = 0 whenever n is even.
So, our total solution could be written as: a[0] + 0 + (∑_(n=1)^∞)(b[n] Sin[n x])

So, the first few non-zero values of b[n] are:  ((-2)/π),((-2)/(3π)),((-2)/(5π)),((-2)/(7π)), etc. These are made using n = 1, 3, 5, and 7.

Determining whether n is even or odd is a pain. We would like to make a function that allows us to directly sum everything using only one count, in one compact form. Consider the following function:
c[n] = ((-2)/((2n-1)π))
The first few non-zero values of this function are:  ((-2)/π),((-2)/(3π)),((-2)/(5π)),((-2)/(7π)), etc. These are made using n = 1,2, 3, and 4, rather than by using 1, 3, 5, 7...
In other words, b[n] and c[n] create the same results, except that the c[n] function lets us count every value of n instead of just using the odd values. If use it, we have to be careful that the values of n in the c[n] function continue to match those in the Sine function.
Also, let’s examine what the nature of “infinity” is in this series. How many terms are needed to give a reasonable approximation of this square wave?

lim=30;
c[n_]=((-2)/((2n-1)π));
y[x_]=(1/2)+(∑_(n=1)^lim)c[n]*Sin[(2 n -1) x];

plot1=Plot[y[x],{x,0,6 π},PlotStyle->Directive[RGBColor[1,0,0],AbsoluteThickness[2]]];
Show[plot1,ImageSize->{600,300},AspectRatio->Full]

How different (or better) is the result if you make a larger approximation of “infinity”?

lim=600;
c[n_]=((-2)/((2n-1)π));
y[x_]=(1/2)+(∑_(n=1)^lim)c[n]*Sin[(2 n -1) x];

plot1=Plot[y[x],{x,0,6 π},PlotStyle->Directive[RGBColor[1,0,0],AbsoluteThickness[2]]];
Show[plot1,ImageSize->{600,300},AspectRatio->Full]

You’ll notice that Mathematica needed a significantly longer time to evaluate this plot. Also, you’ll notice that no matter what you do, even if you could truly use an infinity of terms, you’ll always have those thin little spikes on top of every corner.