A question about Manipulate command in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter kindlychung
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

The discussion centers on the behavior of the Manipulate command in Mathematica 7.0, specifically regarding variable scoping. The example demonstrates that defining a function as f[x_] := E^(n*x) does not allow Manipulate to access the variable n, resulting in an error. In contrast, using Subscript[f, n_][x_] := E^(n*x) allows Manipulate to function correctly, as it recognizes n as a local variable. This highlights the importance of variable scope in Mathematica when using dynamic constructs like Manipulate.

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with variable scoping in programming
  • Knowledge of the Manipulate command in Mathematica
  • Basic experience with plotting functions in Mathematica
NEXT STEPS
  • Explore the use of variable scoping in Mathematica functions
  • Learn about the differences between local and global variables in Mathematica
  • Investigate advanced features of the Manipulate command in Mathematica
  • Study the implications of using Subscript in function definitions in Mathematica
USEFUL FOR

Mathematica users, educators, and students who are looking to understand dynamic interactivity in their visualizations and function definitions.

kindlychung
Messages
10
Reaction score
0
This works:
Code:
Manipulate[Plot[E^(n*x), {x, -10, 10}], {n, 1, 10}]

This doesn't:
Code:
f[x_] := E^(n*x);
Manipulate[Plot[f[x], {x, 1, 10}], {n, 1, 10}]

Why?
(I used Mathematica 7.0)
 
Physics news on Phys.org
Then again, this does:
Code:
Subscript[f, n_][x_] := E^(n*x);
Manipulate[Plot[Subscript[f, n][x], {x, 1, 10}], {n, 1, 10}]

Apparently the n in the definition of f[x] is not the same n as that in Manipulate. When you execute
Code:
Subscript[f, n_][x_] := E^(n*x);
Manipulate[Plot[Subscript[f, n][x], {x, 1, 10}] // Hold, {n, 1, 10}]
you will see that the running n is a temporary variable (FE`n$$27, or something like that) different from the Global`n that f[x] uses. When you pass n to f[x] as well, by defining fn[x] or f[n, x], it works.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K