Filter löschen
Filter löschen

I can't see the cosine graph. How do I generate the cosine graph using 2^10 samples?

1 Ansicht (letzte 30 Tage)
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?

Akzeptierte Antwort

Voss
Voss am 4 Apr. 2024
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])

Weitere Antworten (2)

Mitchell Thurston
Mitchell Thurston am 4 Apr. 2024
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 am 4 Apr. 2024
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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by