Re: Step functions
- To: mathgroup at smc.vnet.net
- Subject: [mg17159] Re: [mg17097] Step functions
- From: BobHanlon at aol.com
- Date: Sun, 18 Apr 1999 00:59:44 -0400
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 4/17/99 9:06:41 AM, alessio.massaro at cern.ch writes:
>Can anyone help me to build a step function out of a series of {x,y} pairs?
>
>For ex.., consider an ordered random {x,y} series like
>
>data=Sort[Table[{Random[],Random[]}, {10}], First[#1]<First[#2]&]
>
>I would like to build a function f[ { {x1, y1}, {x2, y2}, ...}, x ], that
>evaluates to:
>- y[i] if x=x[i]
>- y[i+1] if x[i]<x<x[i+1]
>Here each point {xi, yi} determines f over the left-open interval
>x[i]<x<=x[i+1] of x.
>The right-open interval version would be:
>- y[i] if x=x[i]
>- y[i] if x[i]<x<x[i+1]
>
>Since I do not check the newsgroup regularly, I'll greatly appreciate
>replies sent to my e-mail address as well as the NG itself.
>
Alessio,
You did not specify how you wanted the function to handle the cases of
x < x[1] or x > x[Length[data]], so this is set up to return unevalated in
those cases.
data=Sort[Table[{Random[],Random[]}, {10}]];
Note that the default Sort order does what you want.
f[steps_List, x_?NumericQ] :=
Module[{ss = Select[data, #[[1]] >= x&]},
If[Length[ss] == 1, ss[[1, 1]],
If[ss[[1, 1]] == x, ss[[1, 2]],
ss[[2, 1]]]]] /; steps[[1, 1]] <=x <=
First[Last[steps]];
Plot[Evaluate[f[data, x]], {x, First[First[data]], First[Last[data]]}];
Bob Hanlon