Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Take a look at f[x_] := f[x] = ... too. –  Kuba Apr 8 at 9:22
1  
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 at 9:24
1  
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 at 10:18
1  

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
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 at 11:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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