Re: Very very basic question about Mathematica expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg111166] Re: Very very basic question about Mathematica expressions
- From: Sam Takoy <sam.takoy at yahoo.com>
- Date: Thu, 22 Jul 2010 05:44:18 -0400 (EDT)
- References: <i23k1j$epv$1@smc.vnet.net> <i26kq8$9c8$1@smc.vnet.net>
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
>