Mastering Multiple Function Plots in MATLAB

This article aims to guide you through the process of plotting multiple functions in MATLAB, highlighting its crucial role in data visualization. We will explore various methods and techniques that wi …

Updated October 9, 2023


Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

This article aims to guide you through the process of plotting multiple functions in MATLAB, highlighting its crucial role in data visualization. We will explore various methods and techniques that will allow you to easily create informative plots for analysis and presentations. Additionally, we will provide code samples to demonstrate the concepts discussed throughout this article.

Introduction

Plotting multiple functions is a common task when working with data in MATLAB. It allows us to compare different curves, study patterns or trends, and make decisions based on our visual insights. Whether it’s for research, education, or business purposes, a well-designed plot can effectively communicate the results of your computations. This tutorial will walk you through the various methods of plotting multiple functions in MATLAB while touching upon the key concepts that underlie these techniques.

Prerequisites and Setup

To follow this guide, you must have a basic understanding of MATLAB programming and its plotting functions such as plot, subplot, and axes. If you’re unfamiliar with these topics, you may want to review the following resources:

  • MATLAB Plotting Tutorials (official documentation)
  • Basic 2D Plots in MATLAB (Tech Explorations)

Throughout this tutorial, we will work within a MATLAB session and save our results for future use.

Simple Multiple Function Plotting with plot

The most straightforward approach to plot multiple functions is to use the built-in function called plot. This function accepts one or more vector inputs representing a set of data points, and plots them on a 2D graph. To illustrate this method, let’s create two simple functions:

y1 = sin(x);
y2 = cos(x) + x^3;

We can then plot these functions together using the plot function as follows:

figure; % Create a new figure window
plot([-5 5], [y1 y2]); % Plot both functions on the same graph
title('Simple Multiple Function Plots'); % Add title to the plot
xlabel('X Axis Label'); % Set x-axis label
ylabel('Y Axis Label'); % Set y-axis label

The resulting plot should look like this:

Plotting Functions in Parallel Using subplot and axes

Another useful method for plotting multiple functions involves the use of MATLAB’s subplot and axes commands. These allow us to organize multiple plots within a single figure window, providing an effective way to visualize complex data sets. In this section, we will create three different functions to be plotted on separate subplots using these powerful tools.

First, let’s define our functions:

function1 = @(x) sin(x).*cos(x); % Define a function as an anonymous function
function2 = @(x) 1./(3.*exp(x)); % Define another function using the anonymous function syntax
function3 = @() [randn(10,1) randn(10,1)]'; % Create a matrix with random data (two columns)

Now, let’s set up the figure window and plot our functions on separate subplots:

figure; % Open a new figure window
subplot('221'); % Place a subplot in position (1,1) of a 2x2 grid layout
plot(function1); % Plot function1 on this subplot
title('Subplot 1'); % Add title to the subplot

subplot('222'); % Create another subplot in position (2,1) of the same 2x2 grid layout
plot(function2); % Plot function2 on this subplot
title('Subplot 2'); % Add title to the subplot

subplot('223'); % Place a new subplot in position (2,2) of the 2x2 grid layout
plot(function3); % Plot our matrix as separate curves
title('Subplot 3'); % Add title to the subplot

The resulting plot will look like this:

Customizing Multiple Function Plots with hold on, axis and legend

In the previous sections, we explored different approaches for plotting multiple functions. In this section, we will cover additional techniques that can enhance our plots even further. We will use hold on, axis, and legend commands to customize our graphs.

Let’s define a new set of functions:

function4 = @(x) x.*exp(x); % Define a new function
function5 = @(x) 3*sin(2*pi*x)./cosh(6*x); % Define another function using the anonymous function syntax
function6 = @() [1:10; 11:-1:1]; % Create a vector with a sequence of numbers (increasing from 1 to 10 and decreasing from 11 down to 1)

We will now plot our new functions, applying various customizations to make the plots more visually appealing.

figure; % Open a new figure window
subplot('221'); % Place a subplot in position (1,1) of a 2x2 grid layout
hold on; % Preserve existing plot commands (useful for adding additional lines or plots)
plot(function4); % Plot function4 on this subplot
axis xy; % Set the axis limits to display an X-Y Cartesian view (default is X-only)
title('Subplot 1'); % Add title to the subplot

subplot('222'); % Create another subplot in position (2,1) of the same 2x2 grid layout
hold on; % Preserve existing plot commands
plot(function5); % Plot function5 on this subplot
axis xy; % Set the axis limits to display an X-Y Cartesian view
title('Subplot 2'); % Add title to the subplot

subplot('223'); % Place a new subplot in position (2,2) of the 2x2 grid layout
hold on; % Preserve existing plot commands
plot(function6); % Plot function6 as separate curves on this subplot
axis xy; % Set the axis limits to display an X-Y Cartesian view
title('Subplot 3'); % Add title to the subplot

legend('Plot 1', 'Plot 2', 'Plot 3', 'Plot 4', 'Plot 5', 'Plot 6'); % Add a legend explaining each plot

The resulting plot will look like this:

Conclusion

In this article, we have explored several methods for plotting multiple functions in MATLAB. We learned how to utilize plot, subplot, and axes for simple and parallel function plots. Furthermore, we discussed the use of hold on, axis, and legend commands to customize our graphs according to specific requirements or data visualization needs. By mastering these techniques, you can effectively communicate your MATLAB results through well-designed plots.