1. Matlab Advanced

Plot Function

MATLAB or Matrix Laboratory is a programming language that was developed by MathWorks. This powerful language finds its utility in technical computing. MATLAB provides us with a convenient environment that can be used to integrate tasks like manipulations on matrix, plotting data and functions, implementing algorithms, creating user interfaces, etc. MATLAB is also convenient as it gives the solutions in the form that its user can easily understand. It uses mathematical notations to display the solutions.

Where can MATLAB be used?

Below are a few areas where we can use MATLAB:

  • Computation
  • Development of Algorithms
  • Modeling
  • Simulation
  • Prototyping
  • Analysis and Visualization of data
  • Scientific graphs
  • Engineering graphics
  • Developing applications

MATLAB comprises a number of techniques to perform the above-mentioned uses. The objective of this article is to have a thorough understanding of plot functions in MATLAB. As the name suggests, the purpose of the plot function is to plot the graph of a function in MATLAB. We use a plot function to create a graphical representation of our data. It is straightforward and easy to visualize a trend in data when it is plotted as compared to just looking at the raw figures.

Syntax of Plot Function

Below are the syntax of Plot Function:

  • fplot (f)
  • fplot (f, xinterval)
  • fplot (funx, funy)
  • fplot (___, LineSpec)
  • fplot (___, Name, Value)
  • fp = fplot (___)

Let us now understand all the above-mentioned functions one by one:

1. fplot (f)

This will plot the graph defined by the function y = f(x). The plot is created over the default interval [-5, 5].

Let’s understand this function with the help of an example:

fplot (@(x)  x^2)

Here we have taken square function as the one which we need to plot. The output of our example will be as shown below:

matlab plot function

2. fplot (f, xinterval)

In the above example, the interval was defaulting, however, if we need the interval of our choice, we can use fplot (f, xinterval). We need to specify the interval as a 2-element vector in the form [xmin xmax]

Let’s understand this clearly with the help of an example:

fplot (@(x)  x^2, [0 3])

Here we have taken the same function as in the example above. For this example, we will take the interval 0 to 3. The output of our example will be as shown below:

matlab plot function

3. fplot (funx, funy)

This function will plot the 2 functions passed as arguments. Here we define 2 functions ‘at’ and ‘bt’ as follows:

at = @(t) cos(2*t);
bt = @(t) sin(1*t);

The output of the final function fplot (at, bt), will be

matlab plot function

4. fplot (___, LineSpec)

This function is used to get the marker symbol, line style or color of the plotline as per the choice of user. i.e user can decide how he/she wants to visualize the final output.

For example, if we need to plot a graph of red color, we will pass ‘r’ as the argument in any of the functions explained above.

Here is an example to understand this:


fplot (@(x)  x^2, ‘r’)

The output that we will get here will be a red color plot of square function:

matlab plot function

Similarly, we can use ‘b’ for blue color and ‘y’ for yellow color.

5. fplot (__, name, value)

This function is used to define properties of the line in the plot as per the pair of name-value passed in the argument. For example, if we pass ‘Linewidth’, 3 as an argument in any of the above examples, we will get a plot of width 3.

Here is an example to understand this:

fplot (__, name, value)

 6. fp = fplot (___)

This function is used to modify the properties of an existing plot. So, we can change the properties of a plot that is already created, by using this function.

Let us first draw plot for a sin wave using the function fp = fplot (@(x) sin(x))

Let us now understand various ways in which we can modify properties of our plot obtained from fp:

  • To change the line of the plot into a line with colons, we can use fp.LineStyle = ‘:’;
  • To change the color of the line, we can use fp.Color = ‘r’;
  • To get marker points on the plot, we can use fp.Marker = ‘x’;
  • To get the desired color of the markers, we can use fp.MarkerEdgeColor = ‘b’;

After implementing the above functions, our final output will be:

fp = fplot (___)

Leave a Reply

Your email address will not be published. Required fields are marked *