# [Maxima] Fourier series of a 2step function?

Richard Hennessy rvh2007 at comcast.net
Sat Sep 20 20:16:13 CDT 2008

```Yes fourier series was a goal, including plotting the series and the original piecewise function, unit_step seemed a
difficult way to me, so I came up with the workaround.  I am not sure of the best way to do this plot.  By the way there
is an alternate syntax for pw(f,x).  So it was not just piecewise continous functions, but just piecewise functions.
That has less use in fourie.

(%i20) f:[[-10,-5,0],[-4,0,5],[0,5,x]];
(%o20)
[[- 10, - 5, 0], [- 4, 0, 5], [0, 5, x]]
(%i21) pw(f,x);
[ If  x  in  [  - 10  ,  - 5  ]  pw(x)  =  0 ]
[                                            ]
(%o21)                                         [ If  x  in  [  - 4   ,   0   ]  pw(x)  =  5 ]
[                                            ]
[ If  x  in  [   0    ,   5   ]  pw(x)  =  x ]

The plotting I am still working on.  Have fun.
Rich

----- Original Message -----
From: "Edwin Woollett" <woollett at charter.net>
To: "Richard Hennessy" <rvh2007 at comcast.net>
Cc: "maxima mailing list" <maxima at math.utexas.edu>
Sent: Saturday, September 20, 2008 6:28 PM
Subject: Re: [Maxima] Fourier series of a 2step function?

On Sept 20, 2008, 2:04 AM(!!), Richard Hennessy wrote:

> It could be better, but it's late.
>
> Rich
> I was working on declaring functions peicewise and I came up with this
> (probably not very good solution).  I have
> defined some functions that allow you to declare a function in sections
> (pw) like you want and integrate (pwdefint and
> pwintegrate) and differentiate (pwdiff).
>
> (%i1) set_display('ascii)\$
> (%i2) pwintegrate(expr,x):=
> block
>    ([__i, __k,__l],
>        if matrixp(expr) then
>            (
>                __l:[],
>                if length(expr[1]) = 11 then
>                (
>                    for __i: 1 thru length(expr) do
> __l:endcons([expr[__i,5],expr[__i,7], integrate(expr[__i,11],
> x)],__l),
>                    pw(__l,x)
>                )
>            )
>        else
>            integrate(expr,x)
>    )\$

> (%i6) f:[-inf, 0, -15, x, -10, sin(x^2)*x*20+cos(3*x)*10, -5, x^2+x+6, -2,
> 10*x, 0, x, 10, 0, inf]\$
>
>
> (%i9) pwdefint(pw(f,x),x,-inf,inf), numer,ratprint=false;
> (%o9)
> 7.250095748755271
>

This is an interesting use of Maxima's matrix tools to deal with
piecewise continuous functions.

I think you get better numerial accuracy if you omit "numer",
(and I don't think "ratprint:false" matters), and let Maxima
do it as it wants and then use float( result ), as in

----------------------------------
(%i9) ans1 : pwdefint(pw(f,x),x,-inf,inf), numer,ratprint=false;
(%o9)                          7.250095748755271
(%i10) ans2 : (pwdefint(pw(f,x),x,-inf,inf) ,float(%%) );
(%o10)                         7.25009572340884
(%i11) (ans1-ans2)/ans2 ;
(%o11)                       3.4960133751591161E-9
---------------------
Also, when I use your  pwdiff(...) function as in
---------------------
(%i12) pwdiff(pw(f,x),x,1);
------------------
I get row after row of Col 1 = [ if ]
[ if ]
[ if ]
etc.
and like wise for all the columns, the only output
of use is Col 11.
------------------
Another way to define piecewise continuous functions
would be using unit_step(x), as in

(%i16) u( x ) := unit_step(x)\$
(%i17) uu( x1, x2 ) := u( x - x1 ) - u( x - x2 )\$
qdraw(...), qdensity(...), syntax: type qdraw();

/* here we plot uu( 1, 2 ) using qdraw  */

(%i19) qdraw( yr( -1, 2 ), xr( -1, 3 ), ex( uu( 1, 2 ), x, 0, 3 )  )\$

/*  which gives a nice unit square wave  */

/* let's define a two element pw function as  */

(%i20) g(x) := x*uu( 0, 1)  +  x^2*uu( 1, 2)\$

/*  and plot it  */

(%i21) qdraw( yr( -1, 5), xr( -1, 3), ex( g(x), x, 0, 3)  )\$

/* which shows func "x" from (0, 1) and "x^2
from (1, 2)  */

Can you work with this definition of a piecewise cont.
function and get the correct integrals and derivatives?

It would be nice if fourie.mac were augmented with
the ability to handle this type of input function.

Ted Woollett

```