## Array indices must be positive integers or logical values.

Asked by Eva Carrillo

### Eva Carrillo (view profile)

on 19 Nov 2019 at 16:23
Latest activity Answered by David Hill

### David Hill (view profile)

on 19 Nov 2019 at 16:49
Accepted Answer by KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

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 (view profile)

Answer by KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

on 19 Nov 2019 at 16:41
Edited by KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

on 19 Nov 2019 at 16:44

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

### David Hill (view profile)

Answer by David Hill

### David Hill (view profile)

on 19 Nov 2019 at 16:49

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