Re: Variable number of intervals
- To: mathgroup at smc.vnet.net
- Subject: [mg40463] Re: [mg40437] Variable number of intervals
- From: Dr Bob <majort at cox-internet.com>
- Date: Sun, 6 Apr 2003 04:36:07 -0400 (EDT)
- References: <200304050900.EAA16237@smc.vnet.net> <oprm5yzxpgkcjuc2@smtp.cox-internet.com> <3E8F1D9C.8090104@netscape.net>
- Reply-to: majort at cox-internet.com
- Sender: owner-wri-mathgroup at wolfram.com
Here's a fairly general method. ClearAll[f, interval, g] f[n_, i_, x_] := Sin[n*i*x] interval[n_Integer, i_Integer, x_] := interval[n, i] = (i - 1)/n â?¤ x < i/n ClearAll[g] g[n_, x_] := Block[{Which}, Which[Sequence @@ Flatten@({interval[n, #, x], f[ n, #, x]} & /@ Range[0, n - 1]), True, 0]] g[12, x] Timing@Plot[g[12, x], {x, 0, 1}] {0.266 Second, â??Graphicsâ??} This plots and evaluates faster: ClearAll[g] g[n_] := g[n] = Function[{x}, Evaluate@Block[{Which}, Which[Sequence @@ Flatten@({interval[n, #, x], f[ n, #, x]} & /@ Range[0, n - 1]), True, 0]]] g[12][x] Timing@Plot[g[12][x], {x, 0, 1}] {0.047 Second, â??Graphicsâ??} In both cases, ", True, 0" can be deleted if x will always be in one of the intervals. Bobby On Sat, 05 Apr 2003 13:17:00 -0500, Vadim Nagornyi <vnagornyi at netscape.net> wrote: > Dear Bobby, > thank you very much for this nice and elegant solution. > Unfortunately, the problem that I posted is a simpification of what I > really need. I simplified it intentionally in hope to get an idea form > responses and than extent it to the real case. > Looks like I have oversimplified it.... > In the real case, the function is not constant on the intervals. Instead, > it is definied by some other function that depends on x , on the n, and > on the interval sequentional number, for example Sin[x n i]. If you know > how to solve the problem in this more general case, I would greatly > appreciate your help. > Regards, > Vadim. > > > majort at cox-internet.com wrote: > >> g[x_, n_Integer] := Sum[UnitStep[x - k/n]/n, {k, 0, n - 1}] >> >> or (simpler, more efficient, and not limited to the unit interval) >> >> g[x_, n_Integer] := Ceiling[n x]/n >> >> The first is right-continuous while the second is left-continuous. >> >> The function is useful for creating various square waves: >> >> Plot[g[x, 24] - g[x, 12], {x, 0, 1}] >> Plot[g[x, 12] - g[x, 24] + g[x, 48] - g[x, 96], {x, 0, 1}] >> >> Bobby >> >> On Sat, 5 Apr 2003 04:00:25 -0500 (EST), Vadim Nagornyi >> <vnagornyi at netscape.net> wrote: >> >>> Hello, >>> here is the function that grows on unit interval in 12 steps: >>> >>> n=12; >>> Map[(g[x_]:=#/n/;(#-1)/n<=x<=#/n)&,Range[n]]; >>> Plot[g[x], {x,0,1}] >>> >>> Now, changing n in the first line we can get different number of >>> steps. >>> Instead, I would like to make n the function parameter: g[x_,n_] and >>> define it when plotting, like >>> >>> Plot[{g[x,3],g[x,7],g[x,12]}, {x,0,1}] >>> >>> How to to this? >>> Thanks. >>> Vadim. >>> >>> >> >> >> > -- majort at cox-internet.com Bobby R. Treat
- References:
- Variable number of intervals
- From: vnagornyi@netscape.net (Vadim Nagornyi)
- Variable number of intervals