Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: square wave function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg31688] Re: [mg31663] square wave function
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Sat, 24 Nov 2001 16:44:02 -0500 (EST)
  • References: <200111231046.FAA24071@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Peter Dimitriou wrote:
> 
> To all who responded last time I posted, thank you, I was able to
> resolve that on my own.
> 
> My new Question deals with the following lines of Mathematica code:
> 
> 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?
> 
> Peter

It can be done as you suggest. Below is a slightly more compact form
using Which instead of conditional rules. We handle the basic cases and
then use Mod for the rest.

square[x_] := Which[
	1<=x<2, -1,
	0<=x<1, 1,
	True, square[Mod[x,2.]]]

Plot[square[x], {x,-5,5}]

A still more compact form is as follows.

square2[x_] := -Sign[Mod[x,2.]-1]
Plot[square2[x], {x,-5,5}]


Daniel Lichtblau
Wolfram Research


  • Prev by Date: Re: inverse function computation
  • Next by Date: Re: KroneckerDelta in sum
  • Previous by thread: Re: square wave function
  • Next by thread: RE: square wave function