Fourier series of a 2step function?



On Sept. 16, Doug Stewart wrote:

>how do you do a fourier series of
>Fx =     0   -10<x<-5
>           -5      -5<x<0
>            5      0<x<5
>            0      5<x<10
>
You can construct and plot this function with
unit_step(x), but integrate (and hence fourie.mac)
choke on it. So do expansion by hand as
follows:

(%i1) (display2d:false, declare(n,integer),
               assume(n>0), facts() );
(%o1) [kind(n,integer), n > 0]
/* here is the function you want to expand */
(%i2) uu(x)  :=  -5*unit_step(x+5) + 10*unit_step(x) - 5*unit_step(x-5)$
(%i3) map( 'uu, [-6,-2,2,6] );
(%o3) [0, -5 , 5, 0]
/* but integrate can't cope */
(%i4) integrate( uu(x), x, -10, 10 );
(%o4) 
'integrate(-5*unit_step(x+5)+10*unit_step(x)-5*unit_step(x-5),x,-10,10)
/* so do it "by hand" */
/* Since we have an odd function, only terms like
   b(n)*sin(n*%pi*x/10) will contribute to the
   expansion, but just for practice find a(n) too */

(%i5) a0 : (1/10)*( integrate( -5, x, -5, 0 ) +
                              integrate( 5, x, 0, 5 )  );
(%o5) 0

(%i6) an : (1/10)*(integrate( -5*cos( n*%pi*x/10 ), x, -5, 0 ) +
                       integrate(5*cos(n*%pi*x/10), x, 0, 5 )  );
(%o6) 0

(%i7) bn : ( (1/10)*(integrate( -5*sin(n*%pi*x/10), x, -5, 0 ) +
             integrate( 5*sin(n*%pi*x/10), x, 0, 5 )  ),
              ratsimp(%%) );
(%o7) -( 10*cos(%pi*n/2) - 10 )/(%pi*n)
/* get a Maxima function using expressions bn  */
(%i8) define( b(n), bn );
(%o8) b(n) := -( 10*cos(%pi*n/2) - 10 )/(%pi*n)

/* what are the first seven coefficients?  */

(%i9) (bnlist : [], for i thru 7 do bnlist : cons( b(i), bnlist ),
                                reverse( bnlist )  );
(%o9) [ 10/%pi,  10/%pi,  10/(3*%pi),  0,
                       2/%pi,  10/(3*%pi),  10/(7*%pi)  ]
/* double check first three  */
(%i10) [ b(1), b(2), b(3) ];
(%o10) [ 10/%pi,  10/%pi,  10/(3*%pi)  ]
/* here is expansion out to n = nmax  */
(%i11) fs(nmax) :=  sum( b(m)*sin(m*%pi*x/10), m, 1, nmax )$
/*  display lowest order (nmax = 1) and next higher (nmax = 2) */
(%i12) [ fs(1), fs(2) ];
(%o12) [ 10*sin(%pi*x/10)/%pi,
                     10*sin(%pi*x/5)/%pi + 10*sin(%pi*x/10)/%pi ]
/* let's make some plots  : I am particularly enamored with
   my own plotting  package qdraw.mac, available on my
        webpage. Use your own favorite. */

(%i13) ( load(draw), load(qdraw)  )$
" qdraw(...), qdensity(...), syntax: type qdraw()\; "

/*plot of uu(x)  */

(%i14) qdraw( xr(-15, 15), yr(-10, 10),
                            ex( uu(x), x, -10, 10 ) )$

/* plot of uu(x) with fs(1) and fs(2) approximations: pretty crude */
(%i15) qdraw(  xr(-15, 15), yr(-10, 10),
                            ex( [uu(x), fs(1), fs(2) ], x, -10, 10 ) )$

/* fs(11) does pretty well as a rough approximation  */
(%i16) qdraw( xr(-15, 15), yr(-10, 10),
                    ex(  [ uu(x), fs(11) ], x, -10, 10 ) )$

Ted Woollett
http://www.csulb.edu/~woollett
(or Maxima docs, English tutorials,
  Maxima by Example )