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 Copyright 1988-93 Wolfram Research, Inc. License valid through 28 Nov 1995. -- Open Look graphics initialized -- In[1]:= a = {1 <= t < 2, 5 <= t < 7, 2 <= t < 5} Out[1]= {1 <= t < 2, 5 <= t < 7, 2 <= t < 5} In[2]:= b = {Sin[t], Cos[t], t} Out[2]= {Sin[t], Cos[t], t} In[3]:= spam = {a,b} Out[3]= {{1 <= t < 2, 5 <= t < 7, 2 <= t < 5}, {Sin[t], Cos[t], t}} In[4]:= spam = Transpose[spam] Out[4]= {{1 <= t < 2, Sin[t]}, {5 <= t < 7, Cos[t]}, {2 <= t < 5, t}} In[5]:= spam = Flatten[spam] Out[5]= {1 <= t < 2, Sin[t], 5 <= t < 7, Cos[t], 2 <= t < 5, t} In[6]:= g[t_] := Apply[Which,spam] In[7]:= 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[7]= -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 University of Colorado, Boulder (sherod at newton.colorado.edu)