I want to plot the auto correlation function for 15 numbers without using the inbuilt function. My series is P=[ 47,64,23,7​1,38,64,55​,41,59,48,​71,35,57,4​0,58 ] and the plot should be autocorrelation function vs lags.

23 views (last 30 days)
I want to plot the auto correlation function for 15 numbers without using the inbuilt function. My code is as follows:
P=[ 47,64,23,71,38,64,55,41,59,48]; %%,71,35,57,40,58 ];
N=length(P);
Q=P(1:N);
R=P(1:N);
N1=length(Q);
N2=length(R);
u=mean(P);
sum= ((Q-u)*(R-u).');
covar0=sum/N1;
for k=1:N-1 %%k is no. of lags and its max value can be 14 here
Q1=P(1:N-k); %%Splitting the series into 2 series separated by lag k
R1=P(1+k:N);
N1_1=length(Q1);
N2_1=length(R1);
sum1= ((Q1-u)*(R1-u).');
covark=sum1/N;
acf=covark/covar0;
acf;
end
But after execution of the loop,I am getting acf value only for last value of k, which rather should have been an array for values of k=1 to 14. I am new to matlab. Plz help me resolve the code.

Accepted Answer

Shoaibur Rahman
Shoaibur Rahman on 1 Jan 2015
Edited: Shoaibur Rahman on 1 Jan 2015
Replace acf=covark/covar0 ; by acf(k)=covark/covar0;
Also no need to use acf after that line.
After the end of for loop, use:
plot(1:N-1,acf)

More Answers (1)

jaya priya bharathy
jaya priya bharathy on 20 Dec 2019
how can the above code can be changed so that i would be alble to get the lag at 0.5 and 0 at the end?please explain me.
thank you

Categories

Find more on Loops and Conditional Statements 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!