Plotting sin and cos Function in MATLAB
In this post, we plot the discrete-time sine and cosine functions in MATLAB using the stem() function. Sine and cosine are the most fundamental periodic signals in signal processing. Visualising them in the discrete-time domain helps build intuition for sampling, frequency analysis, and filter design. MATLAB Code % Plotting Discrete-Time sin and cos in MATLAB % The user supplies n; the time axis runs from -n to +n. clc; % Clear the command window clear all; % Clear all workspace variables % --- Sine plot --- nSin = input('Enter n for sin plot : '); % e.g. 5 tSin = -nSin:1:nSin; % Time axis: -n, ..., 0, ..., +n ysin = sin(tSin); % Compute sin at each sample subplot(1, 2, 1); stem(tSin, ysin); title('sin(n)'); xlabel('n'); ylabel('Amplitude'); ylim([-1.5, 1.5]); % Fix y-axis for readability % --- Cosine plot --- nCos = input('Enter n for cos plot : '); % e.g. 5 tCos = -nCos:1:nCos; % Time axis: -n, ..., 0, ..., +n ycos = cos(tCos); % Compute cos at each sample subplot(1, 2, 2); stem(tCos, ycos); title('cos(n)'); xlabel('n'); ylabel('Amplitude'); ylim([-1.5, 1.5]); % Fix y-axis for readability