Plotting in MATLAB
Here is the MATLAB code used to plot \(x(t) = 10 \cos(2 \pi 1000 t + \pi/2 )\)
A = 10; f0 = 1000; phi = pi/2;
T0 = 1/f0;
tt = -2*T0 : T0/40 : 2*T0;
xx = A*cos(2*pi*f0*tt + phi);
plot(tt,xx)
title('Sinusoid: x(t) = 10 \cos(2*pi*1000*t + pi/2)');
xlabel('Time (sec)');
grid on
Line by line this code does:
- Define the three parameters for the plot, A=amplitude,
f0=frequency, and phi=phase angle.
- The period, T0, is 1 over the frequency.
- tt is the time axis for the plot, here we start
2 periods before 0 and quit 2 periods after.
- Here the values of the cosine are computed.
- The plot.
- Put a title on the plot so we know what it is.
- Also label the x axis.
- Show the grid.
You can copy and paste this code into MATLAB to test it.
Once you have tried the code in MATLAB try modifying
it to plot \(x(t) = 120 \cos(2 \pi 250 t - \pi/4 )\)
for \(t\) from -4 ms to 8 ms. Does your answer make sense?