Re: piecewise continuous linear function
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1180] Re: piecewise continuous linear function
- From: Elliot Weinberg <weinberg at math.uiuc.edu>
- Date: Sat, 20 May 1995 05:13:32 -0400
...Message-Id: <9505170729.AA09392 at christensen.cybernetics.net.>
...From: REECE_D <REECE+_D%A1%Electromagnetic_Sciences at mcimail.com>
...Subject: [mg1136] Piecewise function generator
...Content-Length: 416
...To: mathgroup at christensen.cybernetics.net
... I want to generate a piecewise continuous linear function by giving a
...function, call it F, a list of {x,y} values defining the corners of the
...function. I can't seem to do it. Does anyone out there have a quick way
...of doing it?
...}
... Thanks in advance,
... Daryl
This can be done with a one-liner. The resulting function even
plots correctly, although it would be faster to put together
a graphics object that drew the line segments directly.
Mathematica 2.2 for Solaris
Copyright 1988-93 Wolfram Research, Inc.
In[1]:= pieceWise[list_][x_]:=
Apply[Which,Flatten[Map[{#[[1,1]]<=x<=#[[2,1]],Fit[#,{1,x},x]}&,
Partition[list,2,1]
]
]
]
In[2]:= example={{1,2},{2,5},{3,0},{4,2}};
In[3]:= Map[pieceWise[example],{1.5,2.5,3.5}]
Out[3]= {3.5, 2.5, 1.}
ecw