Re: Piecewise defined functions

*To*: mathgroup at smc.vnet.net*Subject*: [mg13135] Re: Piecewise defined functions*From*: "Allan Hayes" <hay at haystack.demon.cc.uk>*Date*: Mon, 13 Jul 1998 07:41:50 -0400*References*: <6mqep0$3gn@smc.vnet.net> <6n4rd1$o0n@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Tobias Oed wrote in message <6n4rd1$o0n at smc.vnet.net>... >Edward Neuman wrote: > >> I'm working with the piecewise defined functions with a large number of >> subintervals. They can be defined using the UnitStep function. >> Manipulations >> (e.g., plotting, integration, etc.) with a function having a large >> number of the UnitStep factors is often a time consuming job. Is it >> possible to force Mathematica to create a sequence of rules like this: >> >> g[x_/;x1 <= x <x2] := Expr1 >> g[x_/;x2 <= x <x3] := Expr2 >> .... >> >> where the lists {x1, x2, ... } and {Expr1, Expr2, ... } are supplied by >> a user? Any help would be greatly appreciated. >> >> Regards, >> Edward Neuman > >This works > >In[1]:= x={x0,x1,x2,x3,x4} > >Out[1]= {x0, x1, x2, x3, x4} > >In[2]:= e={e1,e2,e3,e4} > >Out[2]= {e1, e2, e3, e4} > >In[3]:= xx=Thread[{Drop[x,-1],Rest[x],e}] > >Out[3]= {{x0, x1, e1}, {x1, x2, e2}, {x2, x3, e3}, {x3, x4, e4}} > >In[4]:= Scan[((g[x_ /; (#1<=x<=#2)]:=#3)&[Apply[Sequence,#]])&,xx] > >In[5]:= ??g >Global`g > >g[x_ /; x0 <= x <= x1] := e1 > >g[x_ /; x1 <= x <= x2] := e2 > >g[x_ /; x2 <= x <= x3] := e3 > >g[x_ /; x3 <= x <= x4] := e4 > >Tobias > Another way, using Partition and the listabiliy of Set: x={x0, x1, x2, x3, x4}; e={e1,e2,e3,e4}; Evaluate[Partition[x,2,1,g[x_/;#1<=x<=#2]&]] = e; Test: ?g Global`g g[x_ /; x0 <= x <= x1] = e1 g[x_ /; x1 <= x <= x2] = e2 g[x_ /; x2 <= x <= x3] = e3 g[x_ /; x3 <= x <= x4] = e4 ------------------------------------------------------------- Allan Hayes Training and Consulting Leicester UK http://www.haystack.demon.co.uk hay at haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44(0)116 271 8642