       Re: simple sequence problem

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

```traz wrote:
> Hi, this might be a silly question, but how do you do this in mathematica.
>
> x1 == Cos;
> 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 = Cos, 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 .

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

Now, we can get exact values, as in

In:=
Table[x[n], {n, 5}]

Out=
{Cos, Cos[Cos], Cos[Cos[Cos]], Cos[Cos[Cos[Cos]]],
Cos[Cos[Cos[Cos[Cos]]]]}

and also approximate values thanks to the N function:

In:=
N[%]

Out=
{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.

In:=
Module[{pts = N[Table[{n, x[n]}, {n, 10}]]},
ListPlot[pts, PlotJoined -> True,
Epilog -> {AbsolutePointSize, 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}];

[...graphics deleted...]

Regards,
Jean-Marc

 _The Mathematica Book_, Section "2.5.9 Functions That Remember
Values They Have Found",
http://documents.wolfram.com/mathematica/book/section-2.5.9

```

• 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