Re: square wave function
- To: mathgroup at smc.vnet.net
- Subject: [mg31682] Re: square wave function
- From: "David W. Cantrell" <DWCantrell at sigmaxi.org>
- Date: Sat, 24 Nov 2001 16:43:53 -0500 (EST)
- References: <9tlb73$pep$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
peterangelo at mindspring.com (Peter Dimitriou) wrote: > Clear[square] > square[x_] := 1 /; 0<x<1 > square[x_] := -1 /; -1<x<0 > > Plot[square[x], {x,-1,1}] > > This defines and plots a square wave of period 2 between -1 and 1. > The question is how do I define this for all x (-inf<x<inf, with > period 2) in Mathematica? The Only hint I get is to consider using > the Mod function. Scratched my head long enough anyone out there > willing to help? Here's a cute solution, but please don't take it too seriously (because it's not computationally efficient). You will surely get better answers. If you're unconcerned about how the function is defined at integer values, you can use either 2*Floor[Sin[Pi*x]]+1 or 2*Ceiling[Sin[Pi*x]]-1. If you want, as I would, that the function be 0 at integer values, you can use Ceiling[Sin[Pi*x]]+Floor[Sin[Pi*x]]. However, if you insist that the function be undefined at integer values, then of course the above do not work. Cheers, David