       Re: trying to evaluate a piecewise function

• To: mathgroup at smc.vnet.net
• Subject: [mg2171] Re: trying to evaluate a piecewise function
• From: sherod at boussinesq.Colorado.EDU (Scott Herod)
• Date: Thu, 12 Oct 1995 01:57:41 -0400
• Organization: University of Colorado at Boulder

```In article <44mhaf\$etu at ralph.vnet.net>, Alberto.Meroni at th.u-psud.fr (Alberto.MERONI) writes:
|> I am trying to solve this problem:
|> I have a list of expressions {expr1,expr2,...exprN} each
|> valid in a subdomain specified as a list {l1<x<=u1,l2<x<=u2..
|> ,lN<x<=uN} and I would like to have a function which evaluate a plot
|> this piecewise object.

[snip]

|> Thank you very much
|> 		Alberto Meroni
|> 		ameroni at psisun.u-psud.fr

I did this in some code I wrote to compute estimates of solutions
to differential-delay equations.  Essentially I just wrapped a Which
around everything.  There was a note in the _Mathematica Journal_
suggesting that one use functions with conditionals on them but my
quick Timing test suggested that Which was just as fast.  Anyway here
is a little code.  I wrote each step out but you would probably want to
combine all of the "spam" steps into one line.

--------------------------------------------
Mathematica 2.2 for Solaris
License valid through 28 Nov 1995.
-- Open Look graphics initialized --

In:= a = {1 <= t < 2, 5 <= t < 7, 2 <= t < 5}

Out= {1 <= t < 2, 5 <= t < 7, 2 <= t < 5}

In:= b = {Sin[t], Cos[t], t}

Out= {Sin[t], Cos[t], t}

In:= spam = {a,b}

Out= {{1 <= t < 2, 5 <= t < 7, 2 <= t < 5}, {Sin[t], Cos[t], t}}

In:= spam = Transpose[spam]

Out= {{1 <= t < 2, Sin[t]}, {5 <= t < 7, Cos[t]}, {2 <= t < 5, t}}

In:= spam = Flatten[spam]

Out= {1 <= t < 2, Sin[t], 5 <= t < 7, Cos[t], 2 <= t < 5, t}

In:= g[t_] := Apply[Which,spam]

In:= Plot[g[t],{t,1,7}]

Plot::plnr: CompiledFunction[{t}, g[t], -CompiledCode-][t]
is not a machine-size real number at t = 7..

Out= -Graphics-
-----------------------------------------------

Plot complained because g[t] is not defined at 7.  The graph worked though.

BTW, I would be happy to send anyone the delay equation code.  It still
needs some work but takes input which looks like that of NDSolve and returns
numerical approximations to the solutions of systems of multiple delay
differential equations.

Scott A. Herod
Applied Mathematics