MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: simple sequence problem

  • To: mathgroup at
  • Subject: [mg74411] Re: simple sequence problem
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at>
  • Date: Tue, 20 Mar 2007 05:52:38 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <etnkfe$gqo$>

traz wrote:
> Hi, this might be a silly question, but how do you do this in mathematica.
> x1 == Cos[1];
> x2 == Cos[x1];
> x3 == Cos[x2];
> and so on ... ... so basically x(n)= Cos[x(n - 1)]
> I want to plot n against x(n). How do you do the code for this type of sequence in mathematica?

You want to define a recursive function. To do so, we need an end 
condition, say x[1] = Cos[1], and a general formula for the integer 
value greater than 1. Moreover, for efficiency purpose, we define the 
general formula in such a way that it remembers its values [1].

x[1] = Cos[1];
x[n_Integer /; n > 1] := x[n] = Cos[x[n - 1]];

Now, we can get exact values, as in

Table[x[n], {n, 5}]

{Cos[1], Cos[Cos[1]], Cos[Cos[Cos[1]]], Cos[Cos[Cos[Cos[1]]]], 

and also approximate values thanks to the N function:


{0.540302, 0.857553, 0.65429, 0.79348, 0.701369}

Depending on your needs and tastes, you can plot the function either by 
first  generating a list of pairs {n, x[n]} and then using ListPlot, or 
by using integer functions such as Ceiling, Floor, Round, etc.

Module[{pts = N[Table[{n, x[n]}, {n, 10}]]},
   ListPlot[pts, PlotJoined -> True,
     Epilog -> {AbsolutePointSize[4], Point /@ pts}]; ]

Plot[x[Ceiling[n]], {n, 1, 10}];
Plot[x[Floor[n]], {n, 1, 10}];
Plot[x[IntegerPart[n]], {n, 1, 10}];
Plot[x[Round[n]], {n, 1, 10}];

[ deleted...]


[1] _The Mathematica Book_, Section "2.5.9 Functions That Remember 
Values They Have Found",

  • Prev by Date: Re: Slots & Ampersands
  • Next by Date: compile speed
  • Previous by thread: Re: simple sequence problem
  • Next by thread: Re: simple sequence problem