MATLAB: Do I get NaN when I fill the matrix at 1 = n*11>

nan

For some reason my graphs are coming out with slighty more error than they should have. I believe this is an error brought about by the fact that the 11th, 21st, 31st (and so on) value of ga1_t are considered by the program "NaN" not a number. Can anyone tell me why this is?
%constants
a = 1;
b = 10;
c = .25;
d = .5;
w1 = 2*pi;
w2 = 6*pi;
w3 = 18*pi;
%initialize the function g, which weare going to add the values of the odd harmonics int
ga_recon = zeros(1,201);
delta = zeros(1,201);
%time linespace
t1 = 0:.005:1;
ga = sin(w1*t1)+d*cos(w2*t1)+c*cos(w3*t1);
ga_real = sin(w1*t1)+d*cos(w2*t1)+c*cos(w3*t1);
%Creating all 20 of sinc functions for this problem
g1_t = sin_c(((2*10*pi)*t1)-(1*pi));
ga1_t = ga(10).*(sin_c(((2*10*pi)*t1)-(1*pi)));
g2_t = sin_c(((2*10*pi)*t1)-(2*pi));
ga2_t = ga(20).*(sin_c(((2*10*pi)*t1)-(2*pi)));
g3_t = sin_c(((2*10*pi)*t1)-(3*pi));
ga3_t = ga(30).*sin_c(((2*10*pi)*t1)-(3*pi));
g4_t = sin_c(((2*10*pi)*t1)-(4*pi));
ga4_t = ga(40).*sin_c(((2*10*pi)*t1)-(4*pi));
g5_t = sin_c(((2*10*pi)*t1)-(5*pi));
ga5_t = ga(50).*sin_c(((2*10*pi)*t1)-(5*pi));
g6_t = sin_c(((2*10*pi)*t1)-(6*pi));
ga6_t = ga(60).*sin_c(((2*10*pi)*t1)-(6*pi));
g7_t = sin_c(((2*10*pi)*t1)-(7*pi));
ga7_t = ga(70).*sin_c(((2*10*pi)*t1)-(7*pi));
g8_t = sin_c(((2*10*pi)*t1)-(8*pi));
ga8_t = ga(80).*sin_c(((2*10*pi)*t1)-(8*pi));
g9_t = sin_c(((2*10*pi)*t1)-(9*pi));
ga9_t = ga(90).*sin_c(((2*10*pi)*t1)-(9*pi));
g10_t = sin_c(((2*10*pi)*t1)-(10*pi));
ga10_t = ga(100).*sin_c(((2*10*pi)*t1)-(10*pi));
g11_t = sin_c(((2*10*pi)*t1)-(11*pi));
ga11_t = ga(110).*(sin_c(((2*10*pi)*t1)-(11*pi)));
g12_t = sin_c(((2*10*pi)*t1)-(12*pi));
ga12_t = ga(120).*(sin_c(((2*10*pi)*t1)-(12*pi)));
g13_t = sin_c(((2*10*pi)*t1)-(13*pi));
ga13_t = ga(130).*sin_c(((2*10*pi)*t1)-(13*pi));
g14_t = sin_c(((2*10*pi)*t1)-(14*pi));
ga14_t = ga(140).*sin_c(((2*10*pi)*t1)-(14*pi));
g15_t = sin_c(((2*10*pi)*t1)-(15*pi));
ga15_t = ga(150).*sin_c(((2*10*pi)*t1)-(15*pi));
g16_t = sin_c(((2*10*pi)*t1)-(16*pi));
ga16_t = ga(160).*sin_c(((2*10*pi)*t1)-(16*pi));
g17_t = sin_c(((2*10*pi)*t1)-(17*pi));
ga17_t = ga(170).*sin_c(((2*10*pi)*t1)-(17*pi));
g18_t = sin_c(((2*10*pi)*t1)-(18*pi));
ga18_t = ga(180).*sin_c(((2*10*pi)*t1)-(18*pi));
g19_t = sin_c(((2*10*pi)*t1)-(19*pi));
ga19_t = ga(190).*sin_c(((2*10*pi)*t1)-(19*pi));
g20_t = sin_c(((2*10*pi)*t1)-(20*pi));
ga20_t = ga(200).*sin_c(((2*10*pi)*t1)-(20*pi));
%Adds together every row element of each harmonic all into one linespace
for i = 1:201
ga_recon(i) = ga1_t(i)+ga2_t(i)+ga3_t(i)+ga4_t(i)+ga5_t(i)+ga6_t(i)+ga7_t(i)+ga8_t(i)+ga9_t(i)+ga10_t(i)+ga11_t(i)+ga12_t(i)+ga13_t(i)+ga14_t(i)+ga15_t(i)+ga16_t(i)+ga17_t(i)+ga18_t(i)+ga19_t(i)+ga20_t(i);
end
for i = 1:201
delta(i) = ga_real(i) - ga_recon(i);
end
%Plot Area
plot(t1,ga_recon,'r',t1,ga_real,'b',t1,delta,'y')
title('sync waveform reconstruction')
axis([0 1 -2 2])
xlabel('time t')
ylabel('amplitude g(t)')
% Datafile Operations
i = 1:1:89;
Q = [i; ga1_t(i); ga7_t(i); ga_recon(i); delta(i);];
fileID = fopen('DSP_HW3.txt', 'wt');
fprintf(fileID, '%32s\n', 'Signal Reconstruction');
fprintf(fileID, '%31s\n\n', 'Using Sinc Waveform');
fprintf(fileID, '%2s', 'i');
fprintf(fileID, '%9s', 'prod1');
fprintf(fileID, '%9s', 'prod7');
fprintf(fileID, '%9s', 'gr(t)');
fprintf(fileID, '%9s', 'delta');
fprintf(fileID, '\n');
fprintf(fileID,'%2.0f %+8.3f %+8.3f %+8.3f %+8.3f\n', Q);
fclose(fileID);
%type('DSP_HW3.txt')
disp(ga(10).*(sin_c(((2*10*pi)*11)-(1*pi))))
%sinc function
function [s]= sin_c(x)
if x ==0
s = 1;
else
s = sin(x)./(x);
end
end

Best Answer

You have
function [s]= sin_c(x)
if x ==0
s = 1;
else
s = sin(x)./(x);
end
end
In MATLAB, an if is considered to be true only if all of the comparisons are true. Your code is equivalent to
function [s]= sin_c(x)
if all(x ==0)
s = 1;
else
s = sin(x)./(x);
end
end
The difference between these is important because you are passing in a vector of x values, and most of them are not 0.
You might be tempted to change to if any(x == 0) but that will be true for the entire vector because of the 0 at the beginning of the vector, so you would return the scalar constant 1 instead of the correct vector value.
... Time for you to start using logical indexing.