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?

채택된 답변

Voss
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])

추가 답변 (2개)

Mitchell Thurston
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)

Sam Chak
Sam Chak 2024년 4월 4일
deltat = 0.001;
nsamples = 2000;
time =[0 : deltat : deltat*(nsamples-1)];
size(time)
ans = 1x2
1 2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% 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)
ans = 1x2
1 2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(time, ydata), grid on

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by