Manipulate the first value of a recursive function, redraw plot

1

I'm new to this, casually executing basic lines of code online in Wolfram Lab; and I have a question I couldn't figure myself.


Example; I have a recursive function and its initial value

f[x_]:=Log[Abs[f[x-1]]]
f[0] = 2

I can draw the connecting plot for the initial value:

ListPlot[Table[f[t], {t, 0, 100}], Joined -> True]

I would like to know how can I manipulate f[0]=k and redraw the plot as I move the slider:

Manipulate[f[0]=k, {k, 0, 10, 1}]

Can you then nest multiple plots of this same kind on the same graph in colors, each having their own $k_1,k_2,k_3\dots$ sliders?

Vepir

Posted 2017-06-29T20:21:15.467

Reputation: 554

1Manipulate[f[x_] := Log[Abs[f[x - 1]]]; f[0] = k; ListPlot[Table[f[t], {t, 0, 100}], Joined -> True], {k , 0, 2}]? – kglr – 2017-06-29T20:32:19.413

@kglr That seems to work for a moment but ends up collapsing; Instead of a graph I see a red rectangle in the white area under the slider. – Vepir – 2017-06-29T20:42:36.677

Answers

1

Use RecurrenceTable with vector initial values {k1, k2, k3, k4}:

ClearAll[f]

Manipulate[ListLinePlot[Transpose@RecurrenceTable[{f[x] == Log[Abs[f[x - 1]]], 
  f[0] == {k1, k2, k3, k4}}, f, {x, 1, 40}], PlotRange -> {-5, 5}], 
 {{k1, 2}, 2, 10}, {{k2, 2}, 2, 10}, {{k3, 2}, 2, 10}, {{k4, 2}, 2, 10}]

Mathematica graphics

kglr

Posted 2017-06-29T20:21:15.467

Reputation: 302 076

Thanks. Thought it collapsing must have something to do with Wolfram Lab itself I think. It randomly switches me from "Use as a deployable object instead" and "Use as a manipulate expression instead", and sometime just collapses either way. – Vepir – 2017-06-29T20:54:58.760

Vepir, thank you for the accept. Welcome to mma.se. – kglr – 2017-06-29T20:55:47.950

1

I'm going to recommend a somewhat different approach, just in my own style. I hope it is helpful, despite kglr already offering a suitable work up.

f[k0_][x_]:=f[k0][x]=Log[Abs[f[k0][x-1]]]
f[k0_][0]:=f[k0][0]=k0
Manipulate[
 ListPlot[
  Transpose[Table[{f[k1][x],f[k2][x],f[k3][x],f[k4][x]},{x,0,10,1}]],
  Joined->True
 ],
 {{k1,2},2,10},{{k2,3},2,10},{{k3,4},2,10},{{k4,5},2,10}
]

This makes use of memoization and also gives us a chance to make use of the function in other ways if we want, e.g.

f[k0][7]
(* Output= Log[Abs[Log[Abs[Log[Abs[Log[Abs[Log[Abs[Log[Abs[Log[Abs[k0]]]]]]]]]]]]]] *)
N[f[3]/@Range[6]]
(* Output= {1.09861,0.0940478,-2.36395,0.860335,-0.150434,-1.89423} *)

Screen capture:

Image of Wolfram Programming Lab Session Using Code Above

Kellen Myers

Posted 2017-06-29T20:21:15.467

Reputation: 2 558

I appreciate this :) – Vepir – 2017-06-30T12:42:18.493