Plot Expression or Function in MATLAB
Last Updated :
28 Apr, 2021
In this article, we will discuss how to plot expressions or functions in MATLAB. We can make use fplot() function in MATLAB to generate the plot corresponding to an expression or function.
There are different variants of fplot() function
- fplot(f)
- fplot(f,xinterval)
- fplot(___,LineSpec)
- fplot(___,Name,Value)
Now discussing each variant in detail. Below are the various ways to plot an expression or a function in MATLAB:
fplot(f): Plots the expression passed to it as a parameter.
Example : Plotting cos(x) function in default interval [-5 5]
Matlab
% Plots cos(x) function from x=-5 to 5
fplot(@(x) cos(x))
Output :
fplot(f,xinterval): Plots the curve defined by function y = f(x) in the specified interval. Specify the interval as a two-element vector of the form [xmin xmax].
Example : Plotting cos(x) function in the interval [-3 3].
Matlab
% Plots the sin(x) cureve from x =[-3:3]
fplot(@(x) sin(x),[-3 3])
Output :
fplot(___,LineSpec)
- It allows specifying the line properties such as line style, marker symbol, and color.
- For example, 'b' plots a blue line. We can specify these properties after specifying the function and the interval.
- Line style, marker, and color, specified as a character vector or string containing symbols in any order. For example: '-*r' means red solid line with Asterisk. You can also omit some properties.
Some values of each property are
Linestyle | Meaning | Marker | Meaning | Color | Meaning |
---|
'-' | Solid | 'o' | Circle | 'r' | Red |
':' | Dotted | '+' | Plus sign | 'q' | Green |
'--' | Dashed | '*' | Asterik | 'b' | Blue |
Example : Plotting exp(x) function in the interval [-5 8] with a red solid line with star marker.
Matlab
% plotting exp(x) function in the interval [-5 8]
% with a red solid line with star marker.
fplot(@(x) exp(x),[-5 8],'-*r')
Output :
fplot(___,Name,Value)
- Another way to specify the properties of line with Name-Value pair of arguments.
- For example 'Color', 'r' indicated the red line.
- We can also specify properties such as LineWidth, Marker, Color, LineStyle.
Example : Plot the sin(2x) function with a linewidth of 2, and blue dotted line with a circle marker by specifying their name and value pairs.
Matlab
% Plot the sin(2x) function with a
% linewidth of 2, and blue dotted
% line with circle marker
fplot(@(x) sin(2*x),[-5 8],'Color','b','Marker','o',
'LineWidth',2,'LineStyle','--')
Output :
We can depict multiple expressions in a single plot. Below is an example where we plot sin(x) and cos(2*x) in the same illustration:
Matlab
% Plotting sin(x)
fplot(@(x) sin(x))
hold on
% Resuming the plot and
% including cos(2*x)
fplot(@(x) cos(2*x))
hold off
Output :
In the above program, we depict two expressions in the same plot. The hold keyword is used in MATLAB to hold on and hold off expressions in a plot.
Similar Reads
User defined function in MATLAB Functions let you do a specific task. User defined functions are the functions created by the users according to their needs. This article explains how the user defined function in MATLAB is created. Syntax : function [a1,...,an] = func(x1,...,xm) func is the function name a1,...,an are outputs x1,.
2 min read
Scripts and Functions in MATLAB In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
2 min read
Find() function in MATLAB The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t
3 min read
Function Argument Validation in MATLAB The basics of Function Argument is, " The functions receive certain inputs from the calling command which applies in function syntax and processes it. sometimes It may or may not produce output argument it dependence completely on the functions efficiency or the accuracy of your code." Function Argu
5 min read
Alternatives To Eval Function in MATLAB The eval function is one of MATLAB's most powerful and flexible commands. Eval is an acronym for evaluating, which is exactly what it does: it evaluates MATLAB expressions. Any command that can be executed from the MATLAB prompt may be executed from a Mfile using eval. Use of Eval Function:The Eval
3 min read
Private Functions in MATLAB Private functions are useful when you want to limit the scope of a function. Here we will learn how to create private functions and also use them. Private functions are primary functions that are visible only to a limited group of other functions. Generally, we make private functions, if we want to
2 min read