Re: Piecewise functions

• To: mathgroup at smc.vnet.net
• Subject: [mg23162] Re: [mg23119] Piecewise functions
• From: Hartmut Wolf <hwolf at debis.com>
• Date: Thu, 20 Apr 2000 23:48:36 -0400 (EDT)
• Organization: debis Systemhaus
• References: <200004190630.CAA07527@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```An Le schrieb:
>
> Can someone send me some notebook and package files to do piecewise
> functions? I can't seem to be able to do it with mathematica.
>
> The only thing i get close to is using the Which[] function, but I can't
> integrate with that function.
>

An Le,

if you have your function defined piecewise, you might define

f[x_]:= f1[x] UnitStep[x-x0] UnitStep[x1-x] +
f2[x] UnitStep[x-x1] UnitStep[x2-x] +
... +
f<<n>>[x] UnitStep[x-x<<n-1>>] UnitStep[x<<n>>-x]

and try.

If f should be piecewise linear then e.g. f1 is

f1[x] ->  y0 + (y1 - y0)/(x1 - x0) (x - x0)

if you have function values {y0, y1, ..., y<<n>>} at {x0, x1, ...,
x<<n>>};
etc.

In this case you can make things more simple; define

n = 9
y = (c = 0; Table[c += Random[Real, {-10, 10}], {n}])

x = Join[{0.}, Sort[Table[Random[Real, {0, 100}], {n-2}]], {100.}]

pts = Transpose[{x, y}]

ListPlot[pts, PlotJoined -> True, PlotRange -> {{0, 100}, All}]

dx = (#2 - #1 &) @@@ Partition[x, 2, 1]
dy = (#2 - #1 &) @@@ Partition[y, 2, 1]

m = dy/dx
PrependTo[m, 0.]

f[\[Xi]_] =
First[y] +
Plus @@ MapThread[(Subtract @@ #1) (#2 - \[Xi]) UnitStep[\[Xi] - #2]
&, \
{Partition[m, 2, 1], Drop[x, -1]}]

Plot[f[x], {x, 0., 100.}]

Kind regards, Hartmut

```

• Prev by Date: A simple programming question.
• Next by Date: output from a package
• Previous by thread: Piecewise functions
• Next by thread: Re: Piecewise functions