Fourier series of a 2step function?



On Sept. 20, Richard Hennessy wrote:

>I noticed
>
>plot2d([if x>0 and x < 10 then x^2
>   elseif x> 10 and x < 20 then x^3
>   else 0], [x,-0,30], [plot_format, gnuplot])$
>
>works in plot2d.
>

I think the if .. then .. elseif .. then .. else construct is the best way 
to
approach piecewise defined functions

For plotting with qdraw it is simpler than using unit_step:

unit height square wave from (1,3)

(%i1) u(x) := if x > 1 and x < 3 then 1 else 0$
(%i4) map( 'u, [0.5, 2, 3.5] );
(%o4)                              [0, 1, 0]
(%i2) (load(draw), load(qdraw))$
               qdraw(...), qdensity(...), syntax: type qdraw();
(%i3) qdraw( yr(-1,2), ex1( u(x), x, 0, 4, lw(5), lc(blue) ) )$


plot of function with two pieces
(%i6) g(x):= if x > 1 and x < 2 then (x-1)
              elseif x > 2 and x < 3 then (6 - 2*x) else 0$
(%i7) qdraw( yr(-1,3), ex1(g(x), x, 0, 4, lw(5), lc(blue) ) )$

but the way draw2d works results in a blue line from (2, 0) to (2, 2) hiding
the discontinuity of the function

so take more care with the plot

(%i9) block (  [ small  : 1.0e-6 ],
            qdraw( yr(-1,3),
              ex1(g(x), x, 0, 2 - small, lw(5), lc(blue) ),
              ex1(g(x), x, 2 + small, 4, lw(5), lc(blue)) ) )$

which shows the discontinuity.

It should not be that difficult to construct pwintegrate( expr,x, x1,x2)
which parses expr into pieces delimited by either (if .. then)
and (elseif ...then), and (else ...) and then creates a sum of
calls to integrate which have the correct subinterval
and function def for that subinterval.

Ted Woollett