Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Piecewise bug in Mathematica 8.01?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123136] Re: Piecewise bug in Mathematica 8.01?
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Thu, 24 Nov 2011 06:57:17 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201111231209.HAA15055@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

(a) These statements do nothing:

list3 = {};
list4 = {};
j = 0;

You set the first two variables again, so those statements are wasted, and  
j in the third line is not the same as j in your Table because of scoping  
considerations.

(b) If asked why Piecewise cannot handle more than 45000 clauses, I think  
immediately, "Wow. SURELY we have better ways to write this code."

(c) And we do (in this case at least). Your piecewise function is  
equivalent to this very simple one:

Clear[f]
f[x_Real] := f@Ceiling@x
f[x_Integer] := f[x] = RandomReal[{0.01, 0.03}]
Plot[f@x, {x, 0, 40}]
?f

This is also gorgeous:

Plot[f@x, {x, 0, 45000}]

That DOES store 45000 pieces of random noise, sadly... but it does so  
behind the scenes with no fuss or bother, and it stores only the values  
needed for what you actually do with f.

To see that storage, try

?f

again after the second plot.

(d) That didn't take account of the step-size "dt", but no problem; it can  
be handled this way:

Clear[f, g]
dt = .5;
f[x_] := g@Ceiling[x/dt]
g[x_Integer] := g[x] = RandomReal[{0.01, 0.03}]
Plot[f@x, {x, 0, 10}]
?g

(e) We can also use Interpolation, as in

dt = .5;
maxt = 10;
list3 = RandomReal[{0.01, 0.03}, 1 + Floor[maxt/dt]];
h = Interpolation[list3, InterpolationOrder -> 0][Floor[#/dt] + 1] &;
Plot[h@x, {x, 0, 10}]

Bobby

On Wed, 23 Nov 2011 06:09:11 -0600, Dario <dariopassos at gmail.com> wrote:

> Hello everyone
>
> I'm currently simulating some turbulent processes that incorporate a
> large number of random steps.
> I used Mathematica's 8.01 Piecewise to produce a function that has
> different (random) values at different times. This function is then
> used to calculate some differential equations.
> Through some debugging I figured out that there is something wrong
> with Mathematica's Piecewise when a large number of intervals is used.
> Here is the isolated piece of code that I use to generate the random
> values function.
>
> ******************************************************************
> list3 = {};
> list4 = {};
> j = 0;
> Maxt = 40000;
> dt = 1;
> list3 = Table[RandomReal[{0.01, 0.03}], {i, 0, Maxt, dt}];
> list4 = Table[j < t <= j + dt, {j, 0, Maxt, dt}];
> anoise = Thread[{list3, list4}];
> a = Piecewise[anoise];
> Plot[a, {t, 0, Maxt}]
> *****************************************************************
>
> The problem I detected has to do with the number of intervals that
> Piecewise uses, in this specific case is defined by Maxt.
> If Maxt is bigger then 45000 then Mathematica crashes and all values
> in memory are lost.
> I've tried this in different PCs with different OS (Windows 7 and
> Linux) and the same thing happens.
> I've also tried to play around with $MaxPiecewiseCases but still no
> luck.
> Does someone has any idea what might be causing this?
>
> Best Regards
> D=E1rio Passos
>


-- 
DrMajorBob at yahoo.com



  • Prev by Date: Re: special character size
  • Next by Date: Re: FindShortestTour Function- Error
  • Previous by thread: Re: Piecewise bug in Mathematica 8.01?
  • Next by thread: Re: Piecewise bug in Mathematica 8.01?