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.

I have the following code:

ϕ[0] := π/4
ϕ[n_] := Exp[-n] ϕ[n - 1] + Sqrt[1 - Exp[-2 n]] M
M = RandomVariate[NormalDistribution[0, 1]]

For this recursion relation, is the normal deviate taking a random variable during each recursion or is it just taking one for the whole recursion process?

Thanks,

share|improve this question
2  
M is a nailed thing in your code ... –  belisarius Jul 20 '12 at 19:46
3  
As it is one for the whole recursion. You can use M:=... if you need a new one at each recursion. –  b.gatessucks Jul 20 '12 at 19:46
    
Thanks guys! Always helpful, I'll push you both up. –  kηives Jul 20 '12 at 19:51
add comment

1 Answer

up vote 7 down vote accepted

Set (=) stores the value at the time of computation, while SetDelayed (:=) stores the definition, so that the value is computed again each time it is called. Therefore, writing

M = RandomVariate[NormalDistribution[0, 1]]

means using the same value over and over, while writing

M := RandomVariate[NormalDistribution[0, 1]]

means computing a new RandomVariate at each iteration.


Moreover, no matter what your choice is about M, while we're at it, you might want to do memoization for the whole process, i.e. write

ϕ[0] = π/4
ϕ[n_] := ϕ[n] = Exp[-n] ϕ[n - 1] + Sqrt[1 - Exp[-2 n]] M

This way when computing ϕ[n] you store the value in your memory, so that when computing ϕ[n + 1] you only have to recall it, instead of computing it from scratch. It is usually worth it to trade memory for speed this way.

share|improve this answer
    
Thank you. Here is some text. –  kηives Jul 20 '12 at 21:34
add comment

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.