Array indices must be positive integers or logical values.

1 次查看(过去 30 天)
clc;
spectrum=[];
omega=2*pi*10;
t=0:0.001:10;
f=0:0.1:500;
for n=1:0.001:10
y(n)=sin(omega*t(n));
spectrum=abs(fft((y)));
end
plot(f,spectrum);
hold on;
xlabel('frequency');
hold on;
ylabel('power');
hold on;
xlim([0 1]);
I keep getting:
%Array indices must be positive integers or logical values.
%Error in SINEfunction2 (line 8)
y(n)=sin(omega*t(n));
I've tried many ways to try to fix it but can't figure it out
Help is appreciated!

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-11-19
编辑:KALYAN ACHARJYA 2019-11-19
Please check it has modified (slightly), no need of loop here.
omega=2*pi*10;
t=0:0.001:10;
f=linspace(0,500,length(t));
n=1:0.001:10;
y=sin(omega*t);
spectrum=abs(fft((y)));
plot(f,spectrum);
xlabel('frequency');
ylabel('power');
%xlim([0 1]);

更多回答(1 个)

David Hill
David Hill 2019-11-19
clc;
omega=2*pi*10;
t=0:0.001:10;
f=0:0.1:500;
for n=1:length(t)
y(n)=sin(omega*t(n));
end
spectrum=abs(fft(y));%does not need to be in the loop
plot(t,spectrum);%f needs to be the same length as t
hold on;
xlabel('frequency');
hold on;
ylabel('power');
hold on;
xlim([0 1]);

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by