MathGroup Archive 2011

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

Search the Archive

Re: Protect a variable against being used as an iterator

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116145] Re: Protect a variable against being used as an iterator
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Thu, 3 Feb 2011 05:31:21 -0500 (EST)

Your "exp" is inefficient, computing NDSolve all over again each time Plot  
chooses a new t, even though sol[a] has not changed.

That's true for both plots.

To eliminate the error only (ignoring the inefficiency) is simple enough:

Clear[sol, exp]
tmin = 0;
tmax = 1;
sol[a_] :=
  Block[{t}, NDSolve[{y'[t] == a y[t], y[0] == 1}, y, {t, tmin, tmax}]
   ]
exp[a_, t_] := y[t] /. sol[a]

Plot[exp[2, t], {t, tmin, tmax}] // Timing//Column

0.063649

(Module will also do, in place of Block.)

One way to speed things up is with memoization (dynamic programming):

Clear[sol]
sol[a_] :=
  sol[a] = Block[{t},
    NDSolve[{y'[t] == a y[t], y[0] == 1}, y, {t, tmin, tmax}]
    ]

Plot[exp[2, t], {t, tmin, tmax}] // Timing // Column

0.003212

For a simple differential equation like this one, we can solve for all a  
at once:

Clear[y, t]
y[a_, t_] = y[t] /. First@DSolve[{y'[t] == a y[t], y[0] == 1}, y, t]

E^(a t)

Plot[y[2, t], {t, tmin, tmax}] // Timing // Column

0.001759

These differences in timing are not large, but for another problem they  
could be.

Bobby

On Wed, 02 Feb 2011 05:09:05 -0600, Guido Walter Pettinari  
<coccoinomane at gmail.com> wrote:

> Dear Mathematica group,
>
> I would like to ask you a simple question that, if answered, would
> make my life much easier :-)
>
> Does anybody know how to trigger a warning/error message whenever a
> particular symbol is used as an iterator? By iterator, I mean the
> second argument of functions like Plot, Table, Sum, i.e. the "i" in
> Table [ i^2,  {i, 10} ]
>
> I am looking for this feature since I get errors/unmeaning results
> whenever I use as an iterator a variable already defined in some other
> part of the code (which I may have forgot of).
> E.g., this happens with NDSolve.  Take the following example:
>
> tmin = 0;
> tmax = 1;
> sol[a_] := NDSolve [ {y'[t] == a y[t], y[0] == 1},  y,  {t, tmin,
> tmax} ]
> exp[a_, t_] := y[t] /. sol[a]
>
> The following Plot command does not work:
> Plot[exp[2, t], {t, tmin, tmax}]
> while changing the iterator name works:
> Plot[exp[2, x], {x, tmin, tmax}]
>
> I know that (i) the first Plot command does not work because Plot
> (like Table and Sum) has the HoldAll attribute, and (ii) using the
> Evaluate function on exp[2, t] solves the problem.
>
> However, say that I publish a package that uses a variable in the same
> way I use "t" in the above example.  How does the user of the package
> know that she should not use that variable to iterate?  It would be
> nice if she gets a warning message whenever she tries to do so.
>
> I guess that a workaround would be to use a Unique[] symbol, either as
> an iterator or as, say, the NDSolve independent variable.... do you
> think this is doable?
>
> Thank you very much!
>
> Regards,
>
> Guido
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: Mathematica 8 Home Edition
  • Next by Date: Re: MousePosition with PlotLegends
  • Previous by thread: Re: How to export a large Grid/Table on a single PDF page?
  • Next by thread: Excel XLS Import slowdown in version 8