From: Sam Takoy on
Hi,

I find that I don't understand a very basic thing about the building
blocks of Mathematica.

When the first command is

s = x + h

I figured that (string?) "s" represents (string?) "x+h".

This theory pans out when one tries

s^2

or

D[s, x]

or

Integrate[s, {x, 0, h}]

and

s /. h -> 1

But then by doesn't this work:

Manipulate[ Plot[s, {x, 0, h}], {h, 0.1, 1}]

Many thanks in advance!

From: Themis Matsoukas on
The control variable (h) is internal to Manipulate but you can link it to an external variable. The following works:

s := x + h;
Manipulate[Plot[s /. h -> H, {x, 0, H}], {H, 0.1, 1}]

Notice the := in the definition of s. If you replace it with =, the above will still work but only as long as h has not been assigned a numerical value. However, if you do assign a numerical value, say h=3, the graph will plot x+3, regardless of the value you pick for H. For this reason it is a good idea to use the delayed assignment (:=) rather than the regular =.

Themis

From: Helen Read on
On 7/20/2010 3:42 AM, Sam Takoy wrote:
> Hi,
>
> I find that I don't understand a very basic thing about the building
> blocks of Mathematica.
>
> When the first command is
>
> s = x + h
>
> I figured that (string?) "s" represents (string?) "x+h".

To make it a string, i.e., a sequence of text characters, you would
enter the following.

s="x+h"

Strings are useful for lots of things, but I don't think a string is
what you intend. What you have instead is an expression.


> But then by doesn't this work:
>
> Manipulate[ Plot[s, {x, 0, h}], {h, 0.1, 1}]
>
> Many thanks in advance!

Manipulate needs to know that s is a function of both x and h.

Clear[s]

s[x_,h_]:=x+h

Also you should put an explicit PlotRange in your Plot, so that
Manipulate doesn't go changing the scale on you.

Try it without a PlotRange:

Manipulate[
Plot[s[x, h], {x, 0, h}]

And see how much better it is with one.

Manipulate[
Plot[s[x, h], {x, 0, h}, PlotRange -> {{0, 1}, {0, 1}}], {h, 0.1, 1}]


Defining functions is a very basic idea in Mathematica, and worth
getting used to.

For example, suppose you want to find the extrema of the function

f(x)= -84 x + 9 x^2 + 4 x^3 + 25

So define it as a function.

f[x_] := -84 x + 9 x^2 + 4 x^3 + 25

Plot[f[x], {x, -10, 10}]

Might want to see the 1st derivative along with it.

Plot[{f[x], f'[x]}, {x, -10, 10}]

Solve[f'[x] == 0, x]

f[-7/2]

f[2]


--
Helen Read
University of Vermont

From: Yanping on
Hi,

I am a newbie.

For your question, if you check the PLOT function,
Plot[f,{X,Subscript[X, min],Subscript[X, max]}]

the f has to be a function of X, while in your case,
s=x+h,

s is NOT a function of x.

If you use
s[x_,h_]:=x+h;
Manipulate[ Plot[s[x,h], {x, 0, h}], {h, 0.1, 1}]
It would work the way you want.

best,
yp

On Jul 20, 3:42 am, Sam Takoy <sam.ta...(a)yahoo.com> wrote:
> Hi,
>
> I find that I don't understand a very basic thing about the building
> blocks of Mathematica.
>
> When the first command is
>
> s = x + h
>
> I figured that (string?) "s" represents (string?) "x+h".
>
> This theory pans out when one tries
>
> s^2
>
> or
>
> D[s, x]
>
> or
>
> Integrate[s, {x, 0, h}]
>
> and
>
> s /. h -> 1
>
> But then by doesn't this work:
>
> Manipulate[ Plot[s, {x, 0, h}], {h, 0.1, 1}]
>
> Many thanks in advance!


From: dr DanW on
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

 |  Next  |  Last
Pages: 1 2 3
Prev: Animated Gif
Next: What's wrong with this assuming?