MathGroup Archive 2010

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

Search the Archive

Re: Very very basic question about Mathematica expressions

Thanks to all who responded!

May I belabor this point a little. I understand how to make Manipulate 
work and I understand functions. (I am a product of Scheme from with 
Mathematica seems to have borrowed a few ideas.)

My question is much more formal: What are the building blocks of 
Mathematica, the formal language. When you say

s = x+h

what is s?

Is it an "expression"? Does s represent x+h wherever it appears 
(assuming x and h are unassigned at the time of s=x+h)? Apparently not 
always: yes in s/.h->5, but not in Manipulate.

So here, then is my "model" of Mathematica:
In s = x+h, s is an "Expression"
In s[x_, h_]:=x+h, s is a "Function"

Manipulate expects a "Function" so that answers my question.

Then what is

s[h_] := x + h?  Is it an "Expression" or a "Function" of h with a 
parameter x?

Would then Manipulate[Plot[s[h], {x, 0, 1}, PlotRange -> {0, 1}], {h, 0, 
1}] work? (The answer is yes.) So apparently, Plot is happy with an 
"Expression", but Manipulate wants a "Function"? Why?

Also, in Manipulate[Plot[x+h, {x, 0, 1}, PlotRange -> {0, 1}], {h, 0, 
1}], x+h is no longer an "Expression", but is once again a "Function", 
because of the context? Even though it's inside Plot which is happy with 
an "Expression"?

A personal note: I guess I'm a little frustrated that after a few months 
of working with Mathematica, I still have to try things before I know 
whether they'll work or not. I'm used to having a clear picture of the 
grammar of the language that I'm working with, but I'm struggling here.

On 7/21/2010 7:14 AM, dr DanW wrote:
> I ran into this problem yesterday.  I don't know exactly why it
> happens, I think it has something to do with the way Manipulate
> localizes variables.  To solve it, I use a trick I found that lets me
> take an expression built up of global symbols and localize the
> symbols. Your trivial example:
> s = x + h
> Make a function out of it.  The Evaluate[] is necessary to evaluate s,
> which replaces it with x+h
> sfnc = Function[{x, h}, Evaluate[s]]
> Now the Manipulate[] works fine
> Manipulate[Plot[sfnc[x, h], {x, 0, h}], {h, 0.1, 1}]
> I find myself using this trick a lot.
> Regards,
> Daniel

  • Prev by Date: Re: Show left hand side
  • Next by Date: Re: Scoping constructs Block, Module, ModuleBlock violate
  • Previous by thread: Re: Very very basic question about Mathematica expressions
  • Next by thread: Re: Very very basic question about Mathematica expressions