Message Boards Message Boards

0
|
3511 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Using Manipulate with some integer?

Posted 9 years ago

hi alls i dont really understand why these "Manipulate" do not work :

Clear[n]
g[x_] = Series[x/Sqrt[1 - x^2], {x, 0, n}]
gg[x_] = Normal[g[x]]
Manipulate[gg[x], {n, 1, 5}]
Manipulate[Plot[gg[x], {x, -1, 1}], {n, 1, 5}]

And if i replace the first line with :

  g[x_] = Series[x/Sqrt[1 - x^2], {x, 0, n}]/.n->2

then the g[x_] works, but then n seems to remain fixed to 2 If anyone can clear me...

POSTED BY: Adrien Guyader
4 Replies
Posted 9 years ago

Hi Sander and Henrik thanks, now it works

i have understood that the step for n in the manipulate must be explicit to 1 in order to force n to be an integer

i have understood also that n must be global and that it is done by defining g as a two-variables function

now, i still have some questions :

  • why does it not work if i replace g[x, n] := by g[x, n] = ? I used to believe that a:=something was just equivalent to a=something; meaning that the := or the =; were just to desactivate the display of a.
  • I would like the Manipulate[] to act in the same time on the graphic and on the formula but, if this works indeed, it is not beautiful :

    Manipulate[{Plot[Evaluate@g[x, n], {x, -1, 1}], g[x, n]}, {n, 1, 7, 1}]

  • for the necessity of evaluate, i just read this : https://reference.wolfram.com/language/ref/HoldAll.html but i still do not really understand. For the moment, i just keep in mind that functions to be plotted need the prefix Evaluate.

POSTED BY: Adrien Guyader

Series needs n to be an integer

modify your manipulate to:

Manipulate[gg[x], {n, 1, 5,1}]
Manipulate[Plot[gg[x], {x, -1, 1}], {n, 1, 5,1}]

should work!

POSTED BY: Sander Huisman

Sorry @Sander Huisman ! I did not see you answering this post ...

Regards -- Henrik

POSTED BY: Henrik Schachner

Hi Adrien,

there are a few issues in you code:

  • a dummy variable inside Manipulate (or Table, etc.) is completely different from a global variable with the same name;
  • the Plot command has the attribute HoldAll, i.e. if an argument has to be evaluated you have to be explicit;
  • the n inside Manipulate needs to be discrete (for Series to work);

Try this:

ClearAll["Global`*"]
g[x_, n_] := Normal@Series[x/Sqrt[1 - x^2], {x, 0, n}]
Manipulate[g[x, n], {n, 1, 5, 1}]
Manipulate[Plot[Evaluate@g[x, n], {x, -1, 1}], {n, 1, 5, 1}]

Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract