0
$\begingroup$

I am trying to plot a sequence of functions, for example{(a+b)^i},{i,1,100}. All the expressions I have stored in the list. Now I want to plot them and see how their value would change if I change variable b. I defined test2 as the stored sequence and give the a the value of 1. I tried, Manipulate[ListPlot[test2], {b, 0.1, 0.22, 0.01}], but it doesn't work. Could you give me some suggestions?

Thanks a lot!

$\endgroup$
  • 1
    $\begingroup$ Try Manipulate[ ListPlot[Table[(1 + b)^i, {i, 1, 100}]], {b, 0.1, 0.22, 0.01}]? $\endgroup$ – kglr May 20 '15 at 19:38
  • 1
    $\begingroup$ Yes, the point is that you need to make the dependence of the first argument of Manipulate on b explicit. $\endgroup$ – Sjoerd C. de Vries May 20 '15 at 19:40
  • $\begingroup$ If the expression inside the ListPlot becomes unwieldy, you could take advantage of With for value injection inside the Manipulate expression, so you can define the values separately. Functionally it will lead to the same result, but it may be more readable. For instance: With[{ yourtable = Table[(1 + b)^i, {i, 1, 100}] }, Manipulate[ ListPlot[ yourtable ], {b, 0.1, 0.22, 0.01}] ] $\endgroup$ – MarcoB May 20 '15 at 20:44
  • $\begingroup$ Thank you guys! However, what I said is just an example. What I wanted to do is more complex: I cannot simply use Table[(1 + b)^i, {i, 1, 100}] and the sequence is a function sequence constructed by substitution. $\endgroup$ – Aguang May 20 '15 at 22:19
  • $\begingroup$ For instance, call this sequence I want to analyze as A. I know A[[0]]=f(a,b), and I know A[[1]]=g(A[0]), A[[2]]=g(A[1]),...etc. After I constructed the A sequence, I want to plot all of them and analyze them using Manipulate function. It's not easy to simply use Table function to finish this. Thanks again! $\endgroup$ – Aguang May 20 '15 at 22:26
1
$\begingroup$

You can construct your list within the argument of manipulate using NestList (see its documentation). This function applies a function you give it (e.g. your $g$ function) to an argument repeatedly.

For instance, the following expression starts from the value $2$, applies the pure function 3# & to it (= "take the argument, multiply it by 3"), and reports the result, then repeats the process on this result. This is repeated a total of $5$ times:

NestList[3# &, 2, 5]

(* Out: {2, 6, 18, 54, 162, 486} *)

You can use this to construct a recurrence based on any starting point, including a variable one whose value you can control with Manipulate. For instance, in the following we take a starting value that depends on $b$, the Manipulated variable, then apply the function (10+#/2&) it ten times (which is also a value you could control from Manipulate, if you wanted), and plot the resulting list of values:

Manipulate[
   ListPlot[
    NestList[10 + #/2 &, 10 b, 10], 
    PlotRange -> {0.5, 110}
    ],
  {{b, 3}, 1, 10}
]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks a lot. I think this works. I will try it. $\endgroup$ – Aguang May 22 '15 at 0:32
  • $\begingroup$ @Aguang glad to hear it, let me know how it works out for you $\endgroup$ – MarcoB May 22 '15 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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