find the variance of autocorrelation function

6 views (last 30 days)
Chithralekha
Chithralekha on 17 Aug 2013
Answered: Jaynik on 25 Jul 2024
i want to find the sum of the below.but i am getting an error.please help me. i write the program in two ways.both are same.but both getting different answer
for i=1:20 s(i)=(2*(acf(i+1)^2)); s1=sum(s(i)); end
AND for k=1:20 s=0; for j=1:k s=s+2*acf(j+1)^2; end end
gives different answer.what is the error in this programs? both are same

Answers (1)

Jaynik
Jaynik on 25 Jul 2024
Hi Chithralekha,
The two pieces of code are not equivalent, and that is why they are producing different results. In first piece of code, the value of s(i) is being calculated for each i from 1 to 20, and then summed up the current value of s(i) in each iteration. This means s1 will only hold the value of the last element s(20) because it is being overwritten in each iteration.
In the second piece of code, s is initialized to 0 at each iteration of k, then sum is calculated for the value of 2 * acf(j+1)^2 from j=1 to j=k. The final value of s will be the sum of all these values up to k=20.
Hope this helps!

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!