MATLAB - Plot Sine Wave



Plotting sine waves in MATLAB allows you to visualize periodic signals and study their characteristics. Sine waves are fundamental in signal processing, communication systems, and many other fields. MATLAB provides simple and powerful functions for generating and visualizing sine waves.

In MATLAB, there are various functions available to facilitate sine wave plotting. These include −

  • plot() function
  • fplot() function
  • ezplot() function
  • stem() function
  • polarplot() function

Using plot() function

The plot() function in MATLAB is a fundamental tool for creating 2D plots of data. It allows you to visualize relationships and patterns in your data by plotting points and connecting them with lines. With plot(), you can create various types of plots, including line plots, scatter plots, and more, making it a versatile tool for data analysis and visualization.

Syntax

plot(X, Y)

plot(X,Y) is used in MATLAB to create a 2D plot where X and Y are vectors representing the x and y coordinates of the points to be plotted. Each pair of corresponding elements in X and Y defines a point on the plot. The plot() function connects these points with lines, creating a line plot.

Example 1: Plotting sine wave using plot()

The code we have is as follows −

t = 0:0.01:2*pi; 
y = sin(t);
plot(t, y);
  • In this example, we first define a time vector t using the colon operator (:) to generate values from 0 to 2*pi with a step size of 0.01.
  • Next, we compute the sine values y corresponding to each time value in t using the sin() function.
  • Finally, we use the plot() function to plot the sine wave, with t as the x-values and y as the y-values. This creates a plot of the sine wave with time on the x-axis and amplitude on the y-axis.

When the code is executed in matlab command window the output we get is as follows −

plotting sine wave

Example 2: Plotting a Sine Wave with 10 Hz Frequency

The code we have is −

f = 10;     
a = 1;      
phi = 0;   
t = 0:1/(f*100):1;
y = a * sin(2*pi*f*t + phi);
plot(t, y);

In the example above −

  • The code defines a sine wave with a frequency of 10 Hz (f = 10), an amplitude of 1 volt (a = 1), and a phase of 0 radians (phi = 0).
  • It generates a time vector t from 0 to 1 second with a step size corresponding to the frequency, ensuring that the sine wave completes one cycle within the specified time range.
  • The sine wave equation y = a * sin(2*pi*f*t + phi) computes the values of the sine wave at each time point in t, taking into account the frequency, amplitude, phase, and time vector.
  • Finally, the plot() function is used to plot the sine wave, and labels and a title are added to the plot for clarity.

When the code is executed the output we get is as follows −

sine wave frequency

Using fplot() function

The fplot() function in MATLAB is used to plot a function over a specified range. It is particularly useful for visualizing mathematical functions, including trigonometric functions like sine waves. Unlike the plot() function, which requires explicit calculation of function values, fplot() can directly plot functions of the form y = f(x).

Syntax

fplot(fun, [xmin, xmax])

fplot(fun, [xmin, xmax]) : Here fun: The function to be plotted. It can be an anonymous function or a function handle.[xmin, xmax]: The range of x values over which the function should be plotted.

Example: Plotting Sine Wave Using fplot()

The code we have is as follows −

fun = @(x) sin(2*pi*60*x);
fplot(fun, [0, 1/60]);

In above example

  • We have an anonymous function fun representing a sine wave with a frequency of 60 Hz.
  • The fplot() function is used to plot the sine wave over one cycle (0 to 1/60 seconds).
  • The x-axis represents time in seconds, and the y-axis represents the amplitude of the sine wave.

When you execute the code the output is as follows −

sine wave fplot

Using ezplot() function

The ezplot() function in MATLAB is used to plot implicit and explicit equations in 2D. It is particularly useful for visualizing mathematical expressions without the need to explicitly define a function. ezplot() can handle both single-variable and parametric expressions, making it versatile for various plotting tasks.

Syntax

ezplot(equation)
ezplot(equation, [xmin, xmax])
  • Equation − The equation to be plotted. It can be an explicit equation y = f(x) or an implicit equation f(x, y) = 0.
  • [xmin, xmax] − (Optional) The range of x values over which the equation should be plotted.

Example: Plotting a Sine Wave using ezplot()

The code we have is −

ezplot('sin(x)', [0, 2*pi])

In above code we have is −

  • The ezplot() function is used to plot the sine wave y = sin(x) over the range [0, 2*pi].
  • The equation 'sin(x)' represents the sine function sin(x), where x is the independent variable.
  • The range [0, 2*pi] specifies the x-axis limits over which the sine wave is plotted, covering one full period of the sine function.

When we execute the code, the output is −

sine wave ezplot

With just ezplot(sin(x)), the output we get is as follows −

ezplot sinx

Using stem() function

The stem() function in MATLAB is used to create stem plots, which are similar to traditional plots but represent data as discrete points above a baseline. Stem plots are commonly used for visualizing discrete data, such as time-series data or signal samples.

Syntax

stem(Y)
stem(X, Y)

Y − Vector of data values to be plotted.

X − Optional vector of x-coordinates corresponding to the data values in Y.

Example: Simple Stem Plot of a Sine Wave

The code we have is as follows −

t = 0:0.1:2*pi;
y = sin(t);
stem(t, y);

In this example, we create a time vector t and compute the sine values y.

  • The stem() function is used to plot the sine wave, with t as the x-coordinates and y as the y-coordinates.
  • The plot represents the discrete samples of the sine wave as stems rising from the x-axis at each time point.

When the code is executed the output is as follows −

simple stem plot

Using polarplot() function

The polarplot() function in MATLAB is used to create polar plots, which are plots that use polar coordinates instead of Cartesian coordinates. Polar plots are particularly useful for visualizing periodic functions, such as sine waves, or data that is naturally represented in polar form.

Syntax

polarplot(theta, rho)
  • theta − Vector of angles (in radians) for each data point.
  • rho − Vector of radial distances from the origin for each data point.

Example: Plotting sinewave using polarplot()

The code we have is −

theta = 0:0.01:2*pi;
rho = sin(theta);
polarplot(theta, rho);

In this example,

  • We define an angle vector theta ranging from 0 to 2*pi (one full revolution) with a small step size.
  • We compute the radial distances rho as the sine values corresponding to each angle in theta.
  • The polarplot() function is used to plot the sine wave in polar coordinates, with theta as the angles and rho as the radial distances.
  • The resulting plot shows the sine wave as a spiral around the origin, with the distance from the origin representing the amplitude of the sine wave at each angle.

When the code is executed the output we get is as follows −

plotting sinewave
Advertisements