Matlab-Commands and Operations
Matlab-Commands and Operations
OPENING MATLAB
To open Matlab, click Start-> All Programs->Math Programs -> MATLAB R2015a. Matlab is loading,
and may take a few seconds before the MATLAB Command Window opens.
Commands can either be typed directly into the command prompt of the MATLAB Command Window, or
an M-file may be used to store the code. To create a new M-file, click New Script, located at the top of the
screen under the Home tab in the File pane. This opens the Matlab Editor/Debugger. The code can then
be typed in the window that opens. To execute the code, click on Run button under the Run pane in the
Editor tab. Alternatively, save the M-File; then type the name of the M-File at the command prompt.
Define two matrices A and B. There are various ways to denote the end of a row in Matlab. Matrix A uses
a line return to denote the end of a row, while matrix B uses semicolons.
A=[10 20 30 40 50
60 70 80 90 10
20 30 40 50 60
70 80 90 44 34
32 65 76 32 11];
Be sure to include the semi colon at the end of each number in B. Otherwise you will input a horizontal
matrix instead of a vertical one!
The semicolon (;) at the end of each line hides what A and B are. To view A and B when the command is
executed, omit the semicolon.
%DEFINE C AS A TRANSPOSE
The apostrophe (‘) at the end of Matrix C, transposes the matrix.
C= A'
The resultant matrix is shown below.
C =
10 60 20 70 32
20 70 30 80 65
30 80 40 90 76
40 90 50 44 32
50 10 60 34 11
9500
22500
13000
23340
15870
2.8977
-23.0455
19.3977
-0.2500
-1.0000
2.8977
-23.0455
19.3977
-0.2500
-1.0000
Save the m-file in the C: directory by clicking File-> Save in the MATLAB editor/debugger toolbar. In the
window that opens, type the name of the file. To execute the program, click Run in the Run pane of the
Editor tab or type the name of the M-File at the command prompt in the MATLAB Command Window.
SIMPLE PLOTS
Follow the steps described in the OPENING MATLAB section to open a new m-file, or add this code to the
previous M-File. Comment this code as:
Matlab automatically numbers figures created in numerical order. The user can also define which figure
number is associated with the data. Typing figure(1) ensures this set of data plots into figure 1.
figure(1)
%DEFINE X – This sets the interval for which “y” will be plotted;
x=[0*pi:.25*pi:8*pi];
As described in the previous example, to run the program, save the m-file to the C: directory. In the
MATLAB Command Window, type the name of the file and hit enter. This will run the program. A
window titled “Figure No. 1” opens showing the plot. The completed plot is shown below.
OTHER FUNCTIONS
The impulse response of function shown below is obtained using the “impulse” command.
4 x 4 + 5x 3 − 7 x
H ( z) = 4
7 x + 3x 2 + x
%Declare the Numerator in order of MS power
num= [4 5 0 -7 0];
%declare the denominator in MS powers
den= [7 0 3 1 0];
figure(2);
impulse(num,den);
The Bode Plot of the following function is found using the Matlab command Bode.
1x 6 + 4 x 5 + 23 x 4 + 8 x 3 + 30 x 2 + x
H ( z) =
2 x 4 + 18 x 3 + 3 x 2 + 26 x
To define the function in terms that Matlab understands, the numerator and denominator are defined with
the coefficient of the variables starting with the most significant power.
num=[1 4 23 8 30 1 0];
den=[2 18 3 26 0];