I can't see the cosine graph. How do I generate the cosine graph using 2^10 samples?
조회 수: 1 (최근 30일)
이전 댓글 표시
deltat=0.1;
nsamples=2^10;
time=[0 : deltat : deltat*(nsamples-1)];
for i=1: nsamples
ydata(i,:)=[5+10*cos(2*pi*time+pi/6)];
end
figure(1);
plot(time,ydata,'b')
I try to generate the cosine graph with 2^10 samples.
I used 1024 (=2^10) time samples to create the cosine graph.
The time interval is 0.1 seconds.
Could you explain why I have 1024*1024 ydata rather than 1*1024 ydata?
How do I create a cosine graph using time sample data?
댓글 수: 0
채택된 답변
Voss
2024년 4월 4일
for i=1: nsamples
ydata(i,:)=[5+10*cos(2*pi*time+pi/6)];
end
That calculates 5+10*cos(2*pi*time+pi/6) and assigns it to the first row of ydata, then calculates the same thing again and assigns it to the second row of ydata, etc., etc., for nsamples rows. At the end ydata is a matrix where each row is the same and each row contains the calculated signal for all times.
What you mean to do is simpler than that; just calculate ydata once:
deltat = 0.1;
nsamples = 2^10;
time = (0 : nsamples-1) * deltat;
ydata = 5+10*cos(2*pi*time+pi/6);
figure(1);
plot(time,ydata,'b')
% zooming in
figure
plot(time,ydata,'b')
xlim([0 10])
댓글 수: 0
추가 답변 (2개)
Mitchell Thurston
2024년 4월 4일
You should be able to just get rid of the for loop,
ydata = 5 + 10*cos(2*pi*time+pi/6)
댓글 수: 0
Sam Chak
2024년 4월 4일
deltat = 0.001;
nsamples = 2000;
time =[0 : deltat : deltat*(nsamples-1)];
size(time)
% for i = 1:nsamples
% ydata(i,:) = 5 + 10*cos(2*pi*time + pi/6);
% end
ydata = 5 + 10*cos(2*pi*time + pi/6);
size(ydata)
plot(time, ydata), grid on
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!