5
$\begingroup$

I'm trying to use Manipulate on my function so that I can adjust parameters a, r, and b to see how the graph changes.

When I use Manipulate and try to change the values of a, r, and b using the sliders, nothing happens. The graph doesn't change....what am I doing wrong?

f[x_] := f[x - 1] + 2 r*b (f[x - 1]) - a (f[x - 1]);
f[0] = 100000;
Manipulate[DiscretePlot[f[x], {x, 0, 5}], {r, 0, 1}, {a, 0, 1}, {b, 1, 10}]

I've also tried:

Manipulate[ListPlot[Table[f[x], {x, 0, 5}]], {r, 0, 1}, {a, 0, 1}, {b, 1, 10}]

but the sliders still won't do anything.

$\endgroup$
1

2 Answers 2

5
$\begingroup$

Steph, the problem is that Manipulate does not "see" the explicit dependence of f on the parameters you are trying to manipulate. In other words, those parameters must appear explicitly in the expression to be manipulated. You can change the definition of your function to accomplish that:

ClearAll[f]
f[x_, a_, r_, b_] := f[x - 1, a, r, b] + 2 r*b (f[x - 1, a, r, b]) - a (f[x - 1, a, r, b]);
f[0, a_, r_, b_] = 100000

Manipulate[
 DiscretePlot[f[x, a, r, b], {x, 0, 5}], {r, 0, 1}, {a, 0, 1}, {b, 1, 10}
]

See the following previous posts for more details:

$\endgroup$
3
  • $\begingroup$ Thank you so much. I really, really appreciate your help. I was reading through the other answers @Kuba also pointed out but was still unable to get the syntax right for my use case. Is there a reason, though, why only the first data point displays? Can I change the y-axis range? $\endgroup$
    – steph
    Jun 22, 2016 at 15:46
  • $\begingroup$ @steph My code was incomplete before, sorry about that. I had neglected to change a few function calls to the new format within the definition of f. It should work now. $\endgroup$
    – MarcoB
    Jun 22, 2016 at 15:51
  • $\begingroup$ yes that does it. thank you so much! $\endgroup$
    – steph
    Jun 22, 2016 at 16:04
1
$\begingroup$

I think a little discussion on how to solve the OP's problem is in order, because in this case a little preliminary work can make things much simpler.

f[x] == f[x - 1] + 2 r*b (f[x - 1]) - a (f[x - 1])
f[0] == 100000

is a very simple recursion relation which obviously has the solution

100000 (1 - a + 2 b r)^x

Since we have a close form solution, why use fuss with Table and use ListPlot or any of its variants? Why not just make a simple 2D plot the closed-form expression?

Manipulate[
  Plot[100000 (1 - a + 2 b r)^x, {x, 0, 5}],
  {r, 0, 1, Appearance -> "Labeled"},
  {a, 0, 1, Appearance -> "Labeled"},
  {b, 1, 10, Appearance -> "Labeled"}]

demo

Update

This is to address the OP comment below.

I did it my head. However, you can have Mathematica do it for you.

RSolve[
  {f[x] == f[x - 1] + 2 r*b (f[x - 1]) - a (f[x - 1]), f[0] == 100000}, 
  f[x], x]

{{f[x] -> 100000 (1 - a + 2 b r)^x}}

$\endgroup$
1
  • $\begingroup$ this looks great and I would really like to know more about it, especially the basic math behind finding the closed form solution. Any chance I could ask you about that? $\endgroup$
    – steph
    Jun 22, 2016 at 23:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.