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}]
]

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}]
