Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.2K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Set Manipulate[ ] to show a curve in the right x-range?

Posted 10 years ago

hi i make drafts in order to be sure that i am ok with the basics cirously, this manipulate doesn't work for me :

ClearAll[a, b, x, f]
f[x_] := (a x^2)/(b + x)
Plot[
 {f[x], 10} /. {a -> 10.0, b -> 5.0},
 {x, -60, 60}, 
 PlotRange -> {-60, 60}
 ]

Manipulate[
 Plot[f[x], {x, -10, 10}, PlotRange -> {-20, 20}],
 {a, -4, 4}, {b, -4, 4}]
Manipulate[
 Plot[f[x], {x, -10, 10}, PlotRange -> {-20, 20}],
 {a, 1, 10}, {b, 0, 2 \[Pi]}
 ]

i dont understand why does the Plot[] work but the both Manipulate[] display a x-range from -1.0 to 1.0 which is not what i expect and the curve is not visible, only the axes. thanks for your help...

POSTED BY: Adrien Guyader
2 Replies

Try this:

f[x_] := (a x^2)/(b + x)

Manipulate[
 Plot[f[x], {x, -10, 10}, PlotRange -> {-500, 500}], {a, -10, 
  10}, {b, -4, 4}, TrackedSymbols :> {a, b}, SaveDefinitions -> True]
POSTED BY: S M Blinder

It doesn't work for the same reason that

ClearAll[a, b, x, f]
f[x_] := (a x^2)/(b + x)
With[{a = 10, b = 5},
 Plot[f[x], {x, -10, 10}, PlotRange -> {-20, 20}]
 ]

Mathematica graphics

doesn't work. Both With and Manipulate attempt to make a replacement of the variables a and b with numeric values before running the plot command. But before evaluating f[x], there is no a or b to replace! That's why you had to have a replacement rule inside the plot command before in order to get it to work. In your plot command, it sees f[x], then makes the replacement rule, then plots it. Further, there is probably an issue as to whether Manipulate scopes like Block or like Module.

You could do this in a number of ways. Most obvious, you could modify the definition of f to take three arguments, as in f[x_,a_,b_] := (a x^2)/(b + x). But you could also do what you were doing before, with a replacement rule inside the plot command, but it can't refer to a as the manipulation variable, you need a dummy variable, which I just call aa

Manipulate[Plot[f[x] /. {a -> aa, b -> bb},
  {x, -10, 10}, PlotRange -> {-20, 20}],
 {{aa, -4, "a"}, -4, 4}, {{bb, -4, "b"}, -4, 4}]

enter image description here

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