MATLAB - Plot Sine Wave
MATLAB - Visualizing a Sine Wave
Section titled “MATLAB - Visualizing a Sine Wave”Visualization is a critical part of data analysis and engineering. MATLAB excels at creating clear, customizable plots. A sine wave is a fundamental periodic signal, and learning to plot it demonstrates several core MATLAB visualization techniques.
We will explore modern, recommended functions for plotting. Note that all plotting functions will generate a visual output in a new ‘Figure’ window.
Method 1: The plot() Function (For Data)
Section titled “Method 1: The plot() Function (For Data)”The plot(X, Y) function is the workhorse of 2D plotting in MATLAB. It’s used when you have a set of data points (X and Y coordinates) that you want to visualize, typically connected by lines.
Example 1: Plotting a Simple Sine Wave
Section titled “Example 1: Plotting a Simple Sine Wave”To plot y = sin(x), we first need to generate a set of x and y data points.
% 1. Create a time vector 't' from 0 to 2*pi with small increments% A smaller step size (e.g., 0.01) creates a smoother curve.t = 0:0.01:2*pi;
% 2. Calculate the sine of each value in ty = sin(t);
% 3. Create the plotfigure; % Creates a new figure window, good practiceplot(t, y);
% 4. Annotate the plot for clarity - THIS IS CRUCIAL!grid on; % Adds a gridtitle('Simple Sine Wave: y = sin(t)');xlabel('Time (t)');ylabel('Amplitude');legend('sin(t)');Expected Output: This code will open a figure window displaying a smooth sine wave that completes one full cycle. It starts at (0,0), peaks at 1, crosses the x-axis at π, troughs at -1, and ends at 2π. The plot will have a title, labeled axes, a grid, and a legend.
Example 2: Customizing the Plot
Section titled “Example 2: Customizing the Plot”You can easily customize the appearance of your plot by adding more arguments to the plot function.
t = 0:0.1:2*pi;y1 = sin(t);y2 = cos(t);
figure;% Plot sin(t) as a red dashed line with circle markersplot(t, y1, '--ro');
hold on; % Hold the current plot to add another line
% Plot cos(t) as a blue solid line with star markersplot(t, y2, '-b*');
hold off; % Release the hold
grid on;title('Sine and Cosine Waves');xlabel('Time (t)');ylabel('Amplitude');legend('sin(t)', 'cos(t)');Expected Output: A figure containing two curves: a red dashed sine wave with circles at each data point, and a blue solid cosine wave with stars at each data point. The legend correctly identifies each line.
Method 2: The fplot() Function (For Functions)
Section titled “Method 2: The fplot() Function (For Functions)”The fplot() function is the modern, recommended way to plot a mathematical function directly without pre-calculating data points. It adaptively chooses the step size to create a smooth plot.
Example: Plotting a 60 Hz Sine Wave
Section titled “Example: Plotting a 60 Hz Sine Wave”% 1. Define the function using a function handle (@)% This creates a function f(x) = sin(2*pi*60*x)f = @(x) sin(2*pi*60*x);
% 2. Plot the function over a specific interval (e.g., two cycles)figure;fplot(f, [0, 2/60]); % The interval is [xmin, xmax]
% 3. Annotate the plotgrid on;title('60 Hz Sine Wave using fplot');xlabel('Time (s)');ylabel('Amplitude');Expected Output: A figure showing a smooth sine wave that completes exactly two cycles over the time interval from 0 to 2/60 seconds.
A Note on the Legacy ezplot() Function
Section titled “A Note on the Legacy ezplot() Function”You may see older code or tutorials that use ezplot(). ezplot() is not recommended for new code. The fplot() function is its modern replacement, offering superior control, performance, and flexibility.
- Legacy:
ezplot('sin(x)', [0, 2*pi]) - Modern:
fplot(@(x) sin(x), [0, 2*pi])
Method 3: The stem() Function (For Discrete Data)
Section titled “Method 3: The stem() Function (For Discrete Data)”The stem() function creates a ‘stem plot’, which is ideal for visualizing data that represents discrete samples rather than a continuous signal.
% 1. Create a time vector with a larger step size to represent samplingt = 0:0.2:2*pi;y = sin(t);
% 2. Create a stem plotfigure;stem(t, y, 'filled'); % 'filled' makes the markers solid
% 3. Annotategrid on;title('Discrete Samples of a Sine Wave');xlabel('Sample time (t)');ylabel('Sample Value');Expected Output: A plot where each data point is represented by a circle marker, with a vertical line (a ‘stem’) connecting it to the horizontal axis. This emphasizes the discrete nature of the data.
Method 4: The polarplot() Function (For Polar Coordinates)
Section titled “Method 4: The polarplot() Function (For Polar Coordinates)”For data that is naturally represented by an angle and a radius, polarplot() is the tool of choice.
% 1. Define the angle vector (theta)theta = 0:0.01:2*pi;
% 2. Define the radius as a function of the angle% For a simple sine wave, this creates a circle offset from the originrho = sin(theta);
% 3. Create the polar plotfigure;polarplot(theta, rho);
% 4. Annotatetitle('Sine function in Polar Coordinates');Expected Output: A polar plot (circular grid) showing a circle centered at (0, 0.5) with a radius of 0.5. This visualizes how the sine value (radius) changes with the angle.