MathGroup Archive 2008

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

Search the Archive

Re: Help

  • To: mathgroup at
  • Subject: [mg92362] Re: Help
  • From: Albert Retey <awnl at>
  • Date: Mon, 29 Sep 2008 07:06:46 -0400 (EDT)
  • References: <gbjmbb$3ah$> <> <gbl32v$m2i$>


> Thanks a lot :) .
> I'll go on bothering, feel free not to answer, hehe...
> I'm trying to understand how all this works.
> If I put in a variable an expression, like
> expr = 2*x+65-t;
> then I can't use it in certain situations because mathematica seems to
> expect to "see" the x and t, but in some others I can.
> for example
> expr = 2*x+65-t; Animate[Plot[expr, {x, 0, 4}], {t, 0, 5}];
> doesn't work...
> neither does:
> ran={x, 0, 4}; Plot[Sin[x], ran]
> but
> Clear[expr];expr=2+x;Plot[expr, {x, 0, 4}]
> does work.
> In some of the places where it doesn't work, you seem to be able to sort the
> problem by declaring expr[x_, etc]=etc etc and then using expr[x, etc]...
> but not all the times... in the case of ran, it doesn't work either.
> How exactly does all of this work, or where can I read about it? i can't
> find anything clear in the Help...
> Is there any way to tell mathematica: "ok, heere replace expr for what you
> have stored, imagine instead of expr it says what i read when i type expr
> and press shift+enter"... or how do you think i should manage all this? just
> guessing, testing and changing?

from your second post I guess you are not happy with just having a
solution but want to understand why and how it works. Unfortunatly that
involves some of the more subtle details of the internals of
mathematica. The following tries to give some hints.

There are actually three things you need to understand here: evaluation,
scoping and Dynamic. Both Plot and Animate have the attribute HoldAll:


this means that they both do not evaluate their arguments in the
standard way. You can learn more about the evaluation of expressions in
mathematica here: tutorial/Evaluation (enter this in the documentation
center input field).

Animate (and also Manipulate) also does a kind of scoping, in fact they
need to create variables that live in the frontend, not the kernel, to
do what they are supposed to do. You can see what happens with this:

Animate[{a, Hold[x], x}, {x, 0, 1}]

The x in a and the one that's Animate working with are just not the
same! This is an overview about scoping: guide/ScopingConstructs

the most relevant for Animate is DynamicModule, which is what Animate
and Manipulate create behind the scene. You also might want to look at
the tutorials about Dynamic and Manipulate if you are seeking for a
deeper understanding...

> expr = 2*x+65-t; Animate[Plot[expr, {x, 0, 4}], {t, 0, 5}];

here the t of aninmate is just not the same as the one in expr. There
are various ways to make them match, here is one:

expr = 2*x + 65 - t; Animate[
 Block[{t = tt}, Plot[expr, {x, 0, 4}]], {tt, 0, 5}]

> ran={x, 0, 4}; Plot[Sin[x], ran]

Plot does not evaluate ran, but you can tell it to do so:
ran = {x, 0, 4}; Plot[Sin[x], Evaluate[ran]]

> Clear[expr];expr=2+x;Plot[expr, {x, 0, 4}]

while Plot by using HoldAll sees its arguments unevaluated it still can
evaluate if it wants. This is what happens here. You can construct
something that shows this:

SetAttributes[ptest, HoldAll]

ptest[arg_] := (
   Print["this is the unevaluated argument: ", Hold[arg]];
   Print["this is what it evaluates to: ", arg];

ptest[1 + 1]



  • Prev by Date: Re: ClearAll error message
  • Next by Date: Re: Help
  • Previous by thread: Re: Re: Help
  • Next by thread: Re: Help