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[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].
In[1]:=
Remove[x];
x[1] = Cos[1];
x[n_Integer /; n > 1] := x[n] = Cos[x[n - 1]];
Now, we can get exact values, as in
In[4]:=
Table[x[n], {n, 5}]
Out[4]=
{Cos[1], Cos[Cos[1]], Cos[Cos[Cos[1]]], Cos[Cos[Cos[Cos[1]]]],
Cos[Cos[Cos[Cos[Cos[1]]]]]}
and also approximate values thanks to the N function:
In[5]:=
N[%]
Out[5]=
{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[6]:=
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}];
[...graphics deleted...]
Regards,
Jean-Marc
[1] _The Mathematica Book_, Section "2.5.9 Functions That Remember
Values They Have Found",
http://documents.wolfram.com/mathematica/book/section-2.5.9