Skip to content

How to plot an equation in Matlab?

  • by
  • 5 min read

Matrix Laboratory or Matlab is software used to analyse and design systems and other products. It is used efficiently to plot various types of lines and graphs, as per need and preference. Graphs are a primary method of representation of data visually. Many graphs can be made in Matlab, such as line plots, bubble and scatter charts, data distribution plots, discrete data plots, geographic plots, polar plots, contour plots, volume visualisation, and many more.

In this article, we will see the different functions and methods to plot equations in MATLAB.

Also read: How to plot multiple lines in Matlab?


ezplot

In Matlab, ezplot is one of the many available functions but is not the most recommended. explot plots a symbolic expression, function or equation. It plots a univariate function in the range of [negative 2pi to positive 2pi].

How to plot an equation in Matlab?
Syntax (Extreme left), Output (Center), Workspace (Extreme right)

Here, syms is a function used to create symbolic scalar variables, matrix variables or functions.


fplot

The function fplot plots the symbolic expression or function within a default range of [-5, 5].

How to plot an equation in Matlab?
Syntax (Extreme left), Output (Center), Workspace (Extreme right)

The default range can, however, be varied depending on the requirements.

How to plot an equation in Matlab?
Syntax (Extreme left), Output (Center), Workspace (Extreme right)

The function of fplot can specify the range for the graph if the default range is unfavourable.

Also read: How to inverse matrix in Matlab?


plot

2-D Line Plot in Matlab is created using the basic inbuilt function plot(x, y), where x and y are both vectors, and the graph is y versus the corresponding values of x. Within this function, by just adding another comma, line colour, line width, and many other characteristics can be added to the graph. Consider the example below.

How to plot multiple lines in Matlab? | Candid.Technology
Syntax (Extreme left), Output (Center), Workspace (Extreme right)

The graph above is a sine graph. Note that the graph has not been modified to change colour, line width or line style. Consider the change in the syntax below with a change in colour, line width, and line style.

How to plot multiple lines in Matlab? | Candid.Technology
Syntax (Extreme left), Output (Center), Workspace (Extreme right)

The graph here is green as indicated by the ‘g’. The line width considered here is 4, which is written after the keyword ‘LineWidth’, and the line style here is taken to be hyphen-full stop (-.) in alternation mentioned after the keyword ‘LineStyle’.

Adding colour to the graph or changing its width/style can improve the graph’s readability and make it more visible. Adding colour also makes it easier to add and understand the legends in multiple line graphs.


plot3

The plot3 function plots a 3D parametric curve of the functions or equations x(t), y(t), and z(t). The default range for the plot3 function is [-5, 5]. However, the default range of the plot can be varied using the square bracket with the range specified in it.

How to plot an equation in Matlab?

Also read: How to make a table in MATLAB?


ezpolar

The function ezpolar plots the polar coordinates of the function. A polar curve of the function theta is obtained over the default domain of 0 to 2pi. The default domain can be varied by adding a square bracket with the range in the ezpolar function.

How to plot an equation in Matlab?

fsurf

The function fsurf plots 3D surfaces. It creates a surface plot of the symbolic functions over a default range [-5, 5]. The default range can ve changed based on the user’s requirements simply by adding the range in a square bracket alongside the function.

How to plot an equation in Matlab?

Also read: How does Shazam work?


fcontour

The function fcontour is used in Matlab to plot contours. It plots the contour lines of symbolic variables x, y. The default range for the function is [-5, 5]. However, this range can be changed depending on the requirements by adding a square bracket with the range specified within the function call.

How to plot an equation in Matlab?

The colorbar function can be used to show the colour scale along with the plot. While it is not necessary, it helps make the graph more readable.


fmesh

The fmesh function plots a 3D mesh plot. It creates a mesh plot of the symbolic variables in a default range of [-5, 5]. This range can be varied by using a square bracket along with the function description.

How to plot an equation in Matlab?

Also read: How to make and transpose a Matrix in MATLAB?

nv-author-image

Ishita Maheshwari

A creative nerd, TT player, an avid reader, and an engineering student.

>