clc clear all n = 2; t = 0:0.001:10; k = [-2 -1 0 1 2]; ck = zeros(1, 2*n+1); pn = zeros(1, length(t)); for j = 0:2*n tj = ((2*pi)/(2*n)) * j; fj = exp(sin(tj) + cos(tj)); ck = ck + ((1/((2*n)+1)) * (exp(-1i*k*tj) * fj)); end a0 = ck(n + 1); % Corrected index for a0 a1 = ck(n + 2) + ck(n); a2 = ck(n + 3) + ck(n - 1); A = [a1 a2]; b0 = 0; b1 = 1i * (ck(n + 2) - ck(n)); b2 = 1i * (ck(n + 3) - ck(n - 1)); B = [b1 b2]; for j = 1:n pn = pn + (((A(j))*(cos(j*t))) + ((B(j))*(sin(j*t)))); end pn = a0 + pn; fn = exp(sin(t) + cos(t)); plot(t, pn) hold on plot(t, fn, 'r')