MATLAB Function: A Guide to Creating Various Functions in MATLAB
MATLAB function is an interpreted programming language that has its functions in separate files. It is used for performing numerical linear algebra. It also helps in data analysis and visualization, plots graphs, and develops algorithms.
It is an advanced calculator used to perform operations based on specific demands. It makes working efficient and less time-consuming. Some features in the MATLAB IDE allow users to work without worrying about being professionally trained. In this blog, we will go through the MATLAB function, transfer function, plot function, and more.
MATLAB Plot Function
MATLAB plot function is a syntax that allows the user to generate a graph based on the operations and commands. With relevant equations and constraints, the plotting of graphs becomes presentable and appealing. It is usually denoted by the syntax “plot” and gives the graph based on the operational instructions. Be it straight lines or curves, it facilitates the graphs that you may need.
It is important for a user using MATLAB to understand the relevant inputs to get the desired output. One must know the variable knowledge and the association of dependent and independent variables.
There are various types of MATLAB plot functions available. These include:
- Line plots
- Scatter and bubble charts
- Data distribution plots
- Discrete data plots
- Geographic plots
- Polar plots
- Contour plots
- Vector fields
- Surface and mesh plots
- Volume visualization
- Animation and images
Following are the syntaxes of the plot function:
- fplot (f)
- fplot (f, xinterval)
- fplot (funx, funy)
- fplot (___, LineSpec)
- fplot (___, Name, Value)
- fp = fplot (___)
To learn more about MATLAB functions and excel in your career, you can take a MATLAB course.
MATLAB Rand Function
The uniformly distributed random numbers and arrays are known as Matlab Rand Function. The technique of arranging the distributed arrays uniformly distributed in an interval is the primary work of the rand function.
The syntax that it uses is as follows:
Y = rand (n)
Y = rand (m,n)
Y = rand ([m n])
Y = rand (m,n,p,…)
Y = rand (size(A))
rand
s = rand (‘state’)
The description of the above-mentioned syntax is as follows:
- Y = rand (n) usually deals with the scalar numerical, it returns ‘n’ by ‘n’ matrix of random entries. If the ‘n’ is scalar, an error prompt appears on the screen.
- Y = rand (m, n) or Y = rand ([m n]) returns ‘m’ by ‘n’ matrix of any random entry.
- Y = rand (m,n,p…) or Y = rand ([ m n p…]) plays the role of generating random arrays.
- Y = rand (size(A)) works in returning an array of random entries of the same size as A.
- rand, plays the role of returning a scalar whose value changes each time it is referenced.
- s = rand (‘state’) returns a 35-element vector that contains the current state of the uniform generator.
The rand function usually works in returning the real numbers between 0 to 1. These have already been drawn from a uniform distribution in MATLAB.
For example –
If r1 = rand (100,1)
Then r1 is a 100 by 1 column vector, containing real floating point numbers drawn from a uniform distribution. All the values that are in r1 would be in an open interval.
Stem Function in MATLAB
The method of plotting to present any data in a discrete form is known as the stem function in MATLAB. It generates a plot in the form of vertical lines extending from the base line, which represents the value of given data. It does not join value points with each other to create graphs like the plot function.
The Stem function in MATLAB emphasizes the fact that the data points are discrete and not continuous. There are a variety of syntaxes available to implement stem such as:
- Stem Y stands for data sequence that is to be presented in the plot.
- Stem X, Y indicates the input vectors, and the plot is generated for Y data points concerning X data points.
- Stem (X, Y, p1, p2 value, p2, p2 value,…, pn, pn value) here X and Y are input vectors and pn is the attribute name.
The following are some of the properties of the Stem Function in MATLAB:
Attribute | Description |
Color | It decides the color of the stem. |
LineStyle | The style of the steam line is decided by this attribute. |
LineWidth | The width of the marker edge and the stem depends on this attribute. |
ColorMode | It decides if the color property is set manually or auto. |
LineStyleWidth | It decides how the LineStyle property is set-auto or manual. |
BaseValue | It decides the value for the baseline depending on the orientation of the plot. |
Visible | The visibility of the object is decided by this attribute. |
Tag | Works as an object identifier for a stem object. |
Transfer Function MATLAB
The ratio of the output of a system to the input of the system is known as the transfer function in MATLAB. It considers the initial conditions and equilibrium of the ratio point to be zero. It is a convenient way to represent a linear, invariable time system because of its input-output relationship.
The syntax for the transfer function MATLAB is as follows:
sys = tf (numerator, denominator)
sys = tf (numerator, denominator, ts)
sys = tf (numerator, denominator, ltiSys)
sys = tf (m)
sys = tf (__, name, value)
sys = tf (ltisys)
sys = tf (ltisys, component)
s = tf (‘s’)
z = tf (‘z’, ts)
This function works on the LTI system, which means linear and time-invariant systems. Here, the input becomes zero so the output automatically becomes zero. It is important to consider initial conditions to be zero because if that wouldn’t happen the output wouldn’t be zero. Therefore, the linear property will fail.
Conclusion
- MATLAB function has its functions in separate files.
- MATLAB plot function allows the user to generate a graph based on the operations.
- Stem function in MATLAB emphasizes that the data points are discrete.
- Transfer function MATLAB defines the ratio of the output of a system to the input of the system.