Plotting a Sinusoid with Phase Shift in MATLAB
What Is a Phase Shift in a Sinusoid?
A phase shift moves your sine or cosine wave left or right along the time axis. That's it. No magic, no complexity—just horizontal displacement.
The general form:
y(t) = A × sin(ωt + φ)
Where:
- A = amplitude
- ω (omega) = angular frequency in radians/second
- φ (phi) = phase shift in radians
A positive φ shifts the wave left. A negative φ shifts it right. Remember this or you'll waste time debugging plots that look "wrong."
The Quick Version: Single Command
Here's the minimum viable code to plot a sine wave with phase shift:
t = 0:0.01:2*pi;
phi = pi/4; % 45-degree phase shift
y = sin(2*t + phi);
plot(t, y);
xlabel('Time (rad)');
ylabel('Amplitude');
title('Sine Wave with Phase Shift');
That's all you need for a basic plot.
Getting Started: Step-by-Step
Step 1: Define Your Time Vector
MATLAB needs discrete time points. Use linspace for cleaner control or the colon operator:
% Option 1: Colon operator (common)
t = 0:0.01:4*pi;
% Option 2: linspace (precise endpoint control)
t = linspace(0, 4*pi, 1000);
The colon operator gives you a step size of 0.01. linspace gives you exactly 1000 points. Both work.
Step 2: Set Your Parameters
A = 2; % amplitude
f = 1; % frequency in Hz
omega = 2*pi*f; % angular frequency
phi = -pi/3; % phase shift in radians (negative = shift right)
If you want phase shift in degrees, convert first:
phi_deg = -60; % 60 degrees
phi_rad = deg2rad(phi_deg);
Step 3: Build the Signal
y = A * sin(omega * t + phi_rad);
Step 4: Plot It
plot(t, y, 'LineWidth', 2);
grid on;
xlabel('Time (seconds)');
ylabel('y(t)');
title('Sinusoid with Phase Shift');
Step 5: Compare Multiple Phase Shifts
Want to see what different phase shifts look like side by side? Plot them together:
t = linspace(0, 2*pi, 500);
y0 = sin(t); % no phase shift
y1 = sin(t + pi/4); % positive = left
y2 = sin(t - pi/4); % negative = right
plot(t, y0, 'b-', t, y1, 'r--', t, y2, 'g-.', 'LineWidth', 1.5);
legend('φ=0', 'φ=+π/4 (left)', 'φ=-π/4 (right)');
grid on;
Phase Shift: Sine vs Cosine
Here's what most tutorials skip over:
A phase shift of π/2 converts sine to cosine.
sin(t + pi/2) % equals cos(t)
cos(t - pi/2) % equals sin(t)
This matters when you're matching a waveform to real data. Your signal might be a cosine in the real world, but you only have a sine function. Just add π/2 to your phase.
Comparing Phase Shift Methods
| Method | Code | Effect |
|---|---|---|
| Positive phase in sine | sin(t + pi/4) |
Shifts left (earlier peak) |
| Negative phase in sine | sin(t - pi/4) |
Shifts right (later peak) |
| Phase in cosine | cos(t + phi) |
Same behavior as sine |
| Negative amplitude trick | sin(t + pi) |
Flips AND shifts by π |
Common Mistakes That Ruin Your Plot
- Forgetting to convert degrees to radians. MATLAB's trig functions expect radians. Always.
- Phase shift inside the wrong argument. It goes inside the sine/cosine function, not multiplied outside.
- Too few time points. If your plot looks jagged, increase resolution:
t = 0:0.001:2*pi - Confusing phase with frequency. Phase shifts the wave horizontally. Frequency changes how many cycles you see.
Plotting Multiple Sinusoids with Different Phase Shifts
Real applications often need several signals on one plot. Here's a practical example:
t = linspace(0, 1, 500); % 1 second
f = 2; % 2 Hz signal
% Three signals with different phase shifts
y1 = sin(2*pi*f*t); % reference
y2 = sin(2*pi*f*t + pi/6); % 30° lead
y3 = sin(2*pi*f*t - pi/3); % 60° lag
plot(t, y1, 'k-', t, y2, 'r--', t, y3, 'b:', 'LineWidth', 1.5);
xlabel('Time (s)');
ylabel('Amplitude');
legend('Reference', 'Lead (30°)', 'Lag (60°)');
grid on;
Using Subplots to Compare Phase Effects
Sometimes you need separate plots for clarity:
figure;
t = linspace(0, 2*pi, 400);
subplot(2,2,1);
plot(t, sin(t)); title('φ = 0');
subplot(2,2,2);
plot(t, sin(t + pi/4)); title('φ = π/4 (left shift)');
subplot(2,2,3);
plot(t, sin(t - pi/2)); title('φ = -π/2 (right shift)');
subplot(2,2,4);
plot(t, sin(t + pi)); title('φ = π (flip + shift)');
Quick Reference: Phase Shift Values
| Phase Shift | Radians | Visual Effect |
|---|---|---|
| 0° | 0 | Standard sine wave |
| 90° | π/2 | Matches cosine wave |
| 180° | π | Flipped upside down |
| 270° | 3π/2 | Negative cosine |
When Phase Shift Matters
Phase shifts show up in:
- Signal processing — aligning received signals with known patterns
- Control systems — analyzing stability from phase margins
- Audio synthesis — creating phase-offset effects
- Physics simulations — modeling damped oscillations
If you're working with real-world signals, phase matters. A 30° shift in the wrong direction can destroy correlation between your model and measurements.
Bottom Line
Phase shift is just adding a constant inside your sine or cosine function. That's the whole concept.
Define time, set your parameters, add the phase term, plot. The only gotchas are using radians and putting the phase in the right spot.
Start with the quick example above. Modify the parameters. See what changes. That's how you learn MATLAB—not by reading more, but by running code.