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!