f[x_]:=1/(1+x^2);
pts={-2,-1,0,1,2};

prod=Product[(x-pts[[i]]),{i,1,Length[pts]}]

num=prod/(x-pts)

den=Table[num[[i]]/.{x->pts[[i]]},{i,1,Length[pts]}]

lag=num/den

Plot[lag,{x,-2,2},PlotStyle->Thick]

Plot[{lag*f[pts],f[x]},{x,-2,2}]

poly=Total[lag*f[pts]]

Plot[{poly,f[x]},{x,-2,2}]

f[x_]:=1/(1+x^2);
a=-1;
b=2;
n=10;
pts=Table[a+i*(b-a)/n,{i,0,n}]

prod=Product[(x-pts[[i]]),{i,1,Length[pts]}]

num=prod/(x-pts)

den=Table[num[[i]]/.{x->pts[[i]]},{i,1,Length[pts]}]

lag=num/den

Plot[lag,{x,a,b},PlotStyle->Thick]

Plot[{lag*f[pts],f[x]},{x,a,b}]

poly=Total[lag*f[pts]]

Plot[{poly,f[x]},{x,a,b}]