ClearAll["Global`*"]

Example Problem 7-8-13a

In class, we determined that a[0] = (1/2), and that a[n] = 0. We also found b[n]=((4 (((-1))^n))/(n π)).

f1[x_]=2.0-x;
xmax=2;(*change this to see what happens beyond the "allowed" range  *)
xmin=-xmax;L=2;    
L2=4; (* Also, I redid this problem on paper using -4<x<+4... *)
               (* I'm not showing the work here, just the results... *)
terms=20;     (*we can try more/fewer terms to make comparisons *)
a0=4.0;a[n_]=0.0;b[n_]=((4 (((-1))^n))/(n π));               (* "regular answer" *)
a20=4.0;a2[n_]=0.0;b2[n_]=((8 (((-1))^n))/(n π));                      (* i.e., for -4<x<+4...*)
y[x_]=(a0/2)+(∑_(n=1)^terms)a[n]*Cos[((n π x)/L)]+(∑_(n=1)^terms)b[n]*Sin[((n π x)/L)];   (* "regular answer" *)
 y2[x_]=(a20/2)+(∑_(n=1)^terms)a2[n]*Cos[((n π x)/L2)]+(∑_(n=1)^terms)b2[n]*Sin[((n π x)/L2)];     (* i.e., for -4<x<+4...*)

plot1=Plot[y[x],{x,-xmax,xmax},PlotStyle->Directive[RGBColor[1,0,0],AbsoluteThickness[4]]];
plot2=Plot[f1[x],{x,-xmax,xmax},PlotStyle->Directive[RGBColor[0,0,1],AbsoluteThickness[2]]];
plot3=Plot[y2[x],{x,-xmax,xmax},PlotStyle->Directive[RGBColor[.75,.5,0],AbsoluteThickness[4]]];
Show[plot1,plot2,plot3,ImageSize->{600,300},AspectRatio->Full,PlotRange->{{-xmax,xmax},{-3,6}}]
(* blue =Actual function, red = using ℓ = 2, brown = using ℓ = 4*)