1

How to plot such functions in Mathematica?

Let $a_0, p,g,c$ be any positive integers, defining:

$$a_{n+1} = \begin{cases}\frac{a_n}{p} &, a_n \text{ divisible by p}\\ ga_n +c &, a_n \text{ odd}. \end{cases}$$

PS I am new to Mathematica, I am really sorry if my ignorance is borderline offensive.

Kuba
  • 136,707
  • 13
  • 279
  • 740
Tom Lynd
  • 121
  • 4

1 Answers1

3

This is quite strightforward;

a[n_, p_: 3, g_: 2, c_: 1] := a[n] = If[Divisible[a[n - 1], p], 
                                        a[n - 1]/p, 
                                        g a[n - 1] + c]

Manipulate[
 a[0] = a0;
 DiscretePlot[a[x, p, g, c], {x, 1, 50}, BaseStyle -> {Bold, 18}],
 {{p, 3}, 2, 10, 1},
 {{g, 1}, 0, 10, 1},
 {{c, 1}, 0, 10, 1},
 {{a0, 2}, 1, 10, 1},
 ControlPlacement -> Left]

enter image description here

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Notice that there is a silent assumption that we know DiscretePlot will plot points in order. It is important because if for example it starts from x=50 then after switching a0 it will refer to a[49] which was calculated for previous a0. But it's not the case so don't worry :) – Kuba Apr 08 '14 at 11:08