Error using plot Vectors must be the same length.

3 ビュー (過去 30 日間)
sabayke bremso
sabayke bremso 2019 年 12 月 9 日
コメント済み: Adam Danz 2019 年 12 月 9 日
good evening family I was calculating the autocorrelation of the function cos (wt) but every time I compile I get an error like Error using plot Vectors must be the same length.
Help me please.
this is my code :
function autocorrx
f0=20; N=1000 ;
k=5; Tmax=k/f0;
t=0/2:Tmax/N:Tmax;
x=cos(2*pi*f0*t);
subplot(1,1,1);
plot(t,x);
axis([0 0.1 -1 1]);grid;
xlabel('t(s)');
ylabel('cos(wt)');
C = xcorr(x,x)*Tmax;
subplot(2,2,2) ; plot(t,C) ;
axis([-0 0.1 -1 1]);
grid
xlabel ('t') ;
ylabel ('Cxx(t)') ;
title ('autocorrelation ')
end
  5 件のコメント
sabayke bremso
sabayke bremso 2019 年 12 月 9 日
thank you sir sooooooooooooooooooooooooo much :0
Adam Danz
Adam Danz 2019 年 12 月 9 日
Glad I could help!

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 12 月 9 日
編集済み: Adam Danz 2019 年 12 月 9 日
The cross correlation function xcorr() will produce an output with 2*M-1 elements where M is the number of elements in the input. Perhaps you want to plot the results as a function of the lag.
[C,lags]= xcorr(x,x);
C = C*Tmax; % <--- this is from your code;
plot(lags,C);
Note that your subplots are not defined correctly. Assuming you want two vertically stacked subplot, the subplot definitions should be
subplot(2,1,1)
subplot(2,1,2)

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by