# How to plot particular piecewise defined functions in mathematica

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.

-
Take a look at f[x_] := f[x] = ... too. –  Kuba Apr 8 '14 at 9:22
Be aware of the fact that what you presented is a sequence not a function. You could of course define a piecewise constant function which takes the values of a_n, or do a ListPlot (see answer below). –  Wizard Apr 8 '14 at 9:24
Tom, welcome to mathematica.stackexchange.com! In future questions, please try to show/explain what you have tried yourself. Maybe you tried to plot a discrete function with Plot and it didn't work. That kind of information sometimes doesn't make the question any clearer (most of the time showing us your code helps a lot), but in any case it shows us your effort. I like that you formatted your question though, so I'd say this is a pretty nice first question. –  Jacob Akkerboom Apr 8 '14 at 10:18
–  Jacob Akkerboom Apr 8 '14 at 10:21

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]


-
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 8 '14 at 11:08