       Re: piecewise function

• To: mathgroup at smc.vnet.net
• Subject: [mg109200] Re: piecewise function
• From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
• Date: Fri, 16 Apr 2010 05:53:41 -0400 (EDT)

```Hi,

to answer your question (which is maybe not the answer you want):
Your definition for s[x] makes only sense when x is a numerical value
in the interval [a,b]. For all other values it gives 0.
PiecewiseExpand on your s, no matter what value x has, makes never
sense, since you can only expand expression that contain If-statements
or nested Piecewise's. Your s[x] gives a number. What should be
expanded? The PiecewiseExpand never sees your definition, only the
result of the evaluation.

Please, (please!), never ever use "N" as variable. N is a function. Look
it up in the documentation center. This is bad style.

I don't now exactly what you want but
say you have a list of the y-values your piecewise-defined function
should have inside the interval [a,b]:

data = Table[Sin[x], {x, 0, Pi, Pi/15.}]

then you could define a function which makes a Piecewise function for
you:

makePiecewise[values : {__?NumericQ}, a_?NumericQ, b_?NumericQ,
var_Symbol] := Piecewise[
Transpose[{values,
(#1 <= var <= #2) & @@@
Partition[Table[x, {x, a, b, (b - a)/Length[values]}], 2, 1]}],
0]

func=makePiecewise[data, 0, Pi, x]
Plot[func,{x,0,Pi}]

Cheers
Patrick

On Wed, 2010-04-14 at 23:14 -0400, mircea wrote:
> I want a define a piecewise constant function on an interval [a,b].
> What I did is:
>
> s[x_] := Module[{out}, out = 0;
>    For[i = 1, i <= N, i++,
>     If[mesh[[i]] <= x < mesh[[i + 1]],
>      out = (mesh[[i]] + mesh[[i + 1]])/2; Break[]]]; out];
>
> where:
> mesh={x_1,x_2,..,x_{N+1} }, a=x_1<x_2<..<x_{N+1}=b
>
> However
>
> PiecewiseExpand /@ s[x]
>
> gives me 0.....
>