Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Piecewise functions

12 views
Skip to first unread message

Venkateswara Rao Ayyadevara

unread,
Sep 30, 1997, 3:00:00 AM9/30/97
to

Hi !

I would like to know how to represent piecewise linear functions in
Mathematica. I tried searching on-line help but could not find any info
regarding this.

Thanks for your help

Venkat


Stephen P Luttrell

unread,
Oct 2, 1997, 3:00:00 AM10/2/97
to

Venkateswara Rao Ayyadevara <va...@andrew.cmu.edu> wrote in article
<60s2hp$6...@smc.vnet.net>...

> I would like to know how to represent piecewise linear functions in
> Mathematica. I tried searching on-line help but could not find any info
> regarding this.

Here is an example notebook showing two different ways of defining a
piecewise linear function (Mathematica 3):

Notebook[{
Cell["Piecewise linear function using Calculus`DiracDelta`:", "Text"],

Cell[BoxData[
\(<< Calculus`DiracDelta`\)], "Input"],

Cell[BoxData[
\(f[x_] := \(-x\)\ UnitStep[\(-x\)] + x\ UnitStep[x]\)], "Input"],

Cell[BoxData[
\(\(Plot[f[x], {x, \(-1\), 1}]; \)\)], "Input"],

Cell["Alternatively you can do it this way:", "Text"],

Cell[BoxData[{
\(g[x_] := \(-x\) /; x \[LessEqual] 0\),
\(g[x_] := x /; x \[GreaterEqual] 0\)}], "Input"],

Cell[BoxData[
\(\(Plot[g[x], {x, \(-1\), 1}]; \)\)], "Input"]
},
FrontEndVersion->"Microsoft Windows 3.0",
ScreenRectangle->{{0, 1024}, {0, 712}},
WindowSize->{496, 604},
WindowMargins->{{0, Automatic}, {Automatic, 0}}
]


--
Stephen P Luttrell
lutt...@signal.dra.hmg.gb
Adaptive Systems Theory 01684-894046 (phone)
Room EX21, DERA 01684-894384 (fax)

Malvern, Worcs, WR14 3PS, U.K.
http://www.dra.hmg.gb/cis5pip/Welcome.html

William F. Campbell

unread,
Oct 2, 1997, 3:00:00 AM10/2/97
to

Venkateswara Rao Ayyadevara wrote:

> Hi !


>
> I would like to know how to represent piecewise linear functions in
> Mathematica. I tried searching on-line help but could not find any
> info
> regarding this.
>

> Thanks for your help
>
> Venkat

I do this all the time, albeit in an inelegant fashion. Here's an
example:

myabs[x_ /; x<0] := -x
myabs[x_ /; x>=0] := x

This trivially generalizes to more complex functions -- just specify the
valid domain using the /; conditional operator. However, suppose one
has a spline fit on, say 100 or 1000 subdomains. How can I avoid typing
100 or 1000 separate function definitions?

--
Bill Campbell Correlation is not cause.


0 new messages