19) Fourier Series
Fourier Series ( Create Function FOURIER)
> restart;
> plot([[-3,0],[-3,-1],[-1,1],[-1,-1],[1,1],[1,-1],[3,1],[3,0]],t=-3..3,color=red);
> a0:=(f,p)->(1/(2*p))*int(f(t),t=-p..p):
> a:=(k,p)->(1/p)*int(f(t)*cos(k*Pi*t/p),t=-p..p):
> b:=(k,p)->(1/p)*int(f(t)*sin(k*Pi*t/p),t=-p..p):
> FOURIER:=(f,n,p)->a0(f,p)+sum(a(k,p)*cos(k*Pi*t/p),k=1..n)+sum(b(k,p)*sin(k*Pi*t/p),k=1..n):
> f:=x->x;
> FOURIER(f,4,1);
> Fset:={[[-3,0],[-3,-1],[-1,1],[-1,-1],[1,1],[1,-1],[3,1],[3,0]],seq(FOURIER(f,n,1),n=1..4)}:
> plot(Fset,t=-3..3);
Fourier-Legendre Series
Expand f(x)=exp(-4x^2) into a Fourier-Legendre series.
> g:=x->exp(-4*x^2);
> with(orthopoly):
> term:=proc(m)
> (2*m+1)/2*int(g(x)*P(m,x),x=-1..1)*'P(m,x)';
> end:
> PS:=sum('term(m)',m=0..4);
> erf(z):=2/sqrt(Pi)*Int(exp(-t),t=0..z);
> evalf(erf(2));
> plot({PS,g(x)},x=-1..1);