I want to calculate following recursive sequence:
$\alpha_{0}=0,\\ \cos(\alpha_{i})=\cos(\alpha_{i-1})\cdot\cos(\beta_{i})+\sin(\alpha_{i-1})\cdot\sin(\beta_{i})\cdot\cos(\gamma_{i}).$
In mathematica 8.0.1 I try to do this with:
endRecursion = 20;
beta = Import["beta.txt", "List"];
beta = beta[[1;;endRecursion]];
gamma = RandomReal[{0,2*Pi}, endRecursion];
alpha[0]:= 0;
alpha[i_]:=
ArcCos[Cos[alpha[i-1]]*Cos[beta[[i]]]+
Sin[alpha[i-1]]*Sin[beta[[i]]]*Cos[gamma[[i]]]];
result=Table[alpha[i], {i, 1, endRecursion}];
The values for beta
are imported because they are generated by another formula which is not important for my question here. Values for beta
can be found here.
The problem is that the calculation of the result
is very slowly. It takes about 14s for just 20 iterations! And I need about 300 iterations (times ~10000, because I have several lists of beta values).
I recognized that the calculation is much faster if I skip the Cos[alpha[i-1]]
in the first or the Sin[alpha[i-1]]
in the 2nd summand. Then it takes just ~ 0.01 s for 20 iterations. But unfortunately this falsifies my result. I cannot understand why it is so much faster if there is just one time alpha[i-1]
on the right side of the equation.
Am I doing something wrong? Is there any way to increase the speed of this calculation?
I would be glad about any help!
alpha[i_] := alpha[i] = ArcCos[Cos[alpha[i-1]]*Cos[beta[[i]]] + Sin[alpha[i-1]]*Sin[beta[[i]]]*Cos[gamma[[i]]]]
$\endgroup$ – J. M.'s ennui♦ May 26 '12 at 12:44