March 18

# MATLAB Plot Function: Everything You Need to Know

Tandose Sambo

## Introduction to the MATLAB Plot Function

Plotting 3-D functions takes on a whole new meaning once we start exploring the possibilities that exist in the world of MATLAB. Mathematical functions identify the relationship between two variables. A function f, is defined by the set of ordered pairs (x, f(x) or y). Collectively they form what is known as the domain of the function. When represented in a visual format, the graph of the function is the final outcome. The progressive relationship of the dependent variable and its associated independent variables will be visible to the analyst. In our mathematical analysis, we have identified that graphs can take multiple dimensions if more that one independent variable is fed into a simulation. The resultant outcome is a transition from a two dimensional graph with simple functions, all the way to a multi-dimensional graph with more complicated functions on the three main domains, much like the image above.

From straight lines to curves, MATLAB is the tool that will facilitate the generation of your graphing needs. For the purposes of this tutorial, we will focus predominantly on two-dimensional curves. As a general rule of thumb, the following format will be the details needed to generate graphs of a particular format. With the range of accepted variables, mathematical ordered pairs to ordered triples in continuous functions can plotted into the MATLAB GUI with maximum ease. The image above is a surface plot, identifying a progression in a system model. Three variables – x, y, z all are changing with time. The MATLAB GUI, facilitates a simultaneous plot of these changes in a 3-Dimensional space, thereby allowing the user to see how best the different variables are moving in time.

The purposes of mathematical functions for plotting are variable, and customizable by the user. Since all major science and technological fields requires graphs, it is useful to identify the means via which they can be presented in the appropriate corporate or research settings. Let’s jump into the variety of uses for graphs, and then segway into the detailed MATLAB tutorial explaining how the graphs can be generated in the MATLAB Graphical User Interface (GUI).

## What is the MATLAB Plot Function?

The MATLAB plot function, is that syntax in the MATLAB code system that enables a user to generate a graph, once they have input the relevant equations and constraints.  The plot function is denoted with the syntax “plot”, and can be used to simulate the simplest two dimensional plots such as the relationship with distance versus time  or more complicated functions such as population balance models. The aim for the user is to understand how to facilitate the relevant inputs into the MATLAB system, which will grant the required output. As a code intensive entity, more lower level coding will be required, but the final results are much more powerful than similar tools such as Microsoft Excel, in terms of user control.

## Tutorial: MATLAB Plot Function

Once you have logged into the MATLAB GUI, we will be ready to begin the process of plotting our 3-D graphs. As with all projects, we will start our tutorial off with a fresh MATLAB command page. The version used for this tutorial is the R2018b. The MATLAB system is very versatile with respect to how various functions can be entered into the system, but for the purposes of this tutorial, we will focus simply on the Command Window, and enter all of the relevant code in that sphere.

Now that we’re excited about the possibilities of graphing, we will conduct two attempts – one simple, and one more complicated as a means for introducing more the advanced possibilities in the MATLAB system.

For the first objective, we will begin with a basic graph of the format:

y = f(x)

The MATLAB syntax for a simple two dimensional graph is highlighted by the code below:

plot(x, y)

Objective: In this example, the primary purpose will be to plot a graph of y=x for the range of values of x from 0 to 200, in increments of 10. Following the MATLAB syntax code , the following is what we will enter into the Command window. If you are a beginner, you can copy your code into a text file, and then make adjustments to match the example that you are practicing, and then copy the file into the MATLAB Command window.

x = [0:10:200];       …(1)

y = x;                      …(2)

plot(x, y)

Describing the first two lines of code above, what we are aiming to do is to convert the information that is in the text above into code. Since the graph will have y as the output variable, the independent variables x are highlighted, and these are the values which are from the range 0 to 200.

With the MATLAB syntax code, the domain details are highlighted by the system. For the domain, the first step of the process of indicating the range is highlighted first (0), followed by the increments (10), followed by the upper end of the range (200).

With the equation range defined, the second line of the code, is simply the equation of the line. Let us now enter our data into MATLAB.

MATLAB takes a few seconds to process the code, and once the system executes, the following is the output image. As desired, the intention is for the system to produce a two dimensional graph with the appropriate incremental plots, and the output is generated as follows. The plot function.

Utilizing this syntax will allow the user to create a myriad of additional graphs within the MATLAB User Environment. The first example is indicative of simple two dimensional graphs. As desired, the system can process quadratic graphs, and also more complicated surface graphs like that indicated in that initial visual that’s representative of this tutorial. With time, we’ll delve into more complicated functions. The user also has the ability to manipulate the MATLAB system to add the necessary legends and grid lines to make the graph look as you desire.

Example code for generation of your legends is shown below:

For a graph of a sine curve, the following would be the associated MATLAB code:

x = [0:0.01:10];           …(1)

y = sin(x);                   …(2)

plot(x, y), xlabel(‘x’), ylabel(‘Sin(x)’), title(‘Sin(x) Graph’),   …(3)

grid on, axis equal… (4)

Let us do a line by line analysis. The first line is indicating the range of the function. If you recall from the initial example with the numbers from 0 to 200, the lower end of the range is written first – 0, followed by the increments of 0.01, and finally the upper limit of the range which is the 10.

The second line is the equation of the function, which is simply the sine of all the x inputs in the range.

The third line facilitates our labelling. We want the x-axis to be labelled ‘x”, the y-axis is to be labelled ‘Sine(x)” and the title of the graph to be output as Sin(x). The fourth line of the code is the grid line indicator.

Engineering plots

Engineering plots often include outputs like process control analysis, which require depiction of system conditions such as step changes in a process variable, and the system reaction to the change. When it comes to engineering plots, more complicated functions will often be needed in order  to ensure that accurate representations of the system are being developed. Using the script below, almost ‘twin’ plots will be generated.

y = e−1.5xsin(10x)

y = e−2xsin(10x)

The appropriate coding is highlighted below:

x = [0:0.01:5];

y = exp(-1.5*x).*sin(10*x);

subplot(1,2,1)

plot(x,y), xlabel(‘x’),ylabel(‘exp(–2.5x)*sin(10x)’),axis([0 5 -1 1])

y = exp(-2*x).*sin(10*x);

subplot(1,2,2)

plot(x,y),xlabel(‘x’),ylabel(‘exp(–2x)*sin(10x)’),axis([0 5 -1 1])

grid on, axis equal

The code is entered into the MATLAB Environment, with one minor difference between the two. I’ve added the grid function to the second equation for a visual. The code entry is shown below. Additionally, you’ll realize that I activated the data output for one of the graphs, so that you can see that it was generated behind the MATLAB scenes.

Once the system processes the data, the following will be the outcome in the MATLAB system.

As anticipated, the two sine functions are generated by the system. In the latter graph, we managed to manipulate the graph in order to generate the grid lines along with the axis-labels. That being the case…you’ll see that your visual and your presentation can be manipulated. Line colours, background changes can also be facilitated by the MATLAB system.

## Conclusion

Whether you’re a scientist, engineer or financial planner, one thing that will be critical to your operations is good data analysis. Graphs provide the visual approach to your data analysis needs. With the advent of systems like MATLAB, you’ll be able to visualize your systems, and allow yourself to see your mathematical progressions with ease. The power of MATLAB can be harnessed in order to facilitate this need.

Tandose Sambo is a Chemical Process Engineer, with a focus on improving process efficiency via operational improvements. Six-Sigma certified, and with a Design-focus and Data Analytics interest, she is a driven growing entrepreneur, with the intention to optimize industrial and business process operations. Her interests include sharing time with family and travelling.

Tandose Sambo