1
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ Take a look at f[x_] := f[x] = ... too. $\endgroup$
    – Kuba
    Apr 8, 2014 at 9:22
  • 1
    $\begingroup$ 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). $\endgroup$
    – Wizard
    Apr 8, 2014 at 9:24
  • 1
    $\begingroup$ 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. $\endgroup$ Apr 8, 2014 at 10:18
  • 1
    $\begingroup$ Related: How to model shocks to parameter in a dynamic system? $\endgroup$ Apr 8, 2014 at 10:21

1 Answer 1

3
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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 :) $\endgroup$
    – Kuba
    Apr 8, 2014 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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