%-------------%
% Part 1: Fouier Series.
%-------------%clear all; close all; clc;incrm = 0.01;t = incrm:incrm:10;xt = cos(2*t);plot(t,xt,'LineWidth',3)hold ongrid onxlabel('t')ylabel('x(t)')legend('x(t)')set(gca,'FontSize',13)%%Fouier Series Approximation of x(t)
%---Fourier Coefficients---%
T0 = pi;w0 = 2*pi/T0;a0 = (1/T0) * trapz(t(1:1+T0/incrm), xt(1:1+T0/incrm), 2);for nn = 1 : 17 an(nn) = (2/T0) * trapz(t(1:1+T0/incrm-1), xt(1:1+T0/incrm-1).*cos(nn*w0*t(1:1+T0/incrm-1)), 2); bn(nn) = (2/T0) * trapz(t(1:1+T0/incrm-1), xt(1:1+T0/incrm-1).*sin(nn*w0*t(1:1+T0/incrm-1)), 2);end%---Fourier Series Approximation---%
sumk = 0;for nn = 1 : 17 sumk = sumk + an(nn)*cos(nn*w0*t) + bn(nn)*sin(nn*w0*t);endxt_FS = a0 + sumk;figure;plot(t, xt, 'LineWidth', 3)hold ongrid onxlabel('t')ylabel('x(t)')plot(t,xt_Fs,'r','LineWidth',1.5)ylim([-1,1])legend('x(t)','FS Approximation')set(gca,'FontSize',13)%%Fourier Coefficients for Frequency Plot
%---Problem 3---%
w = w0 * (1 : 17);%---Problem 4---%
figure('Position', [50,100,1200,400]);subplot(1,2,1)stem(w,an(nn),'MarkerSize', 14,'LineWidth', 2)grid onbox onxlabel('\omega')ylabel('a_n')set(gca,'FontSize', 13)xlim([0 17])ylim([-0.5 2])ax = gca;ax.XTick = 0:1:17;ay = gca;ay.YTick = -0.5:0.5:2;subplot(1,2,2)stem(w,bn(nn), 'MarkerSize', 14, 'LineWidth', 2)grid on box onxlabel('\omega')ylabel('b_n')set(gca, 'FontSize', 13)xlim([0 17])ylim([-0.5 2])ax = gca;ax.XTick = 0:1:17;ay = gca;ay.YTick = -0.5:0.5:2;
I am getting 2 error codes and I don't know why!
Warning: Integer operands are required for colon operation when used as an index
Error using stem (line 43) X must be the same length as Y
Best Answer