Experiment 1 Introduction to Matlab Basics
Experiment 1 Introduction to Matlab Basics
Objectives
Procedure
Section-1
MATLAB is usually used in a command-driven mode. When single-line commands are entered,
MATLAB processes these commands immediately and displays the result. MATLAB is also
capable of executing sequences of commands that are stored in files. The commands that are
typed may be accessed later by using the up-arrow key.
Statements and Variable: The following command is used to declare a variable a. A value of
10 is assigned to this variable.
>>a = 10 ;
The semicolon is used to suppress the output on the display. Use the same statement without
semicolon as shown below and press the enter key.
>>a = 10
The following command is used to declare a matrix of two row and three columns.
>>b = [1 2 3; 4 5 6] ;
The matrix variable b can be displayed by just typing b and pressing the enter key.
How this variable id displayed on the screen?
The variable b can also be displayed on the screen by entering the above command without
using semicolon at the end of the statement. Note that the semicolon used inside the square
brackets is used to separate rows of the matrix.
>>b(1,2) or
>>f12 = b(1,2)
The following command is used to obtain the list of variables in the workspace with the
variable name, size, number of bytes etc.
>>whos
Several statements can be placed on one line if they are separated by commas or semicolons
as shown below.
>>x = [10 20 5] ; y = [2 4 6] ; m = 15 ;
Now enter the command who and whos to see all the workspace variables.
Interestingly, the MATLAB can be used in the calculator mode. When the variable name
and “= “ are omitted, from an expression, the result is assigned to the generic variable ans.
The result is displayed immediately after pressing the enter key if the expression is not
terminated by the semicolon as shown below.
>>22/7
If the output is suppressed by terminating the expression by semicolon, the result can be
displayed by entering ans as shown below.
>>2*10 ;
>>ans
Section-2
In MATLAB, the basic computational unit is the matrix. Vectors and scalars are special
cases of matrices. Matrix dimensions must be compatible for matrix operations. Following
are few examples.
>>a = [1 2 3; 4 5 6] ;
>>b = [2 2 2; 3 3 3] ;
>>c = [4 3 2; 1 0 2; 7 2 1] ;
>>d= a + b
>>d= a + c
Enter the following command for matrix multiplication. Note that these operations
are performed on the matrix variables declared in the previous step.
>>a*c
>>a*b
MATLAB has a large set of powerful matrix manipulation commands. It is not possible to
exercise all these commands in this introductory session.
I1
I2 I3
Loop-1:
Loop-2:
Loop-3:
Write all the matrices required to calculate I1, I2 and I3 use Cramer’s rule.
=
I1 = I2 = I3 =
Now write the MATLAB command to calculate I1, I2 and I3 in the following space.
I1 = I2 = I3 =
Matrix representation can be used for the loop equations of the circuit shown above as IR=V,
where R is the resistance matrix, I is the current matrix and V is the voltage matrix. Once
these matrices are declared, currents can be calculated as I=R-1V. Once all the matrices are
defined, enter the following MATLAB command
>>I = inv(R)*V
Now show how the MATLAB returns values of I1, I2 and I3 in the following space.
Section-3
In this section you will learn how to plot a function using MATLAB.
MATLAB has a variety of powerful commands used to plot variable(s). Few examples are given
below.
MATLAB uses a colon notation to generate a row vector. The following statement generates
a row vector t with starting value of 0 and a final value of 1 with an increment of 0.1.
>>t = 0 : 0.1 : 1 ;
>>size(t)
Now write
>>length(t)
Following statements are used to generate a sinusoidal function with 50Hz frequency.
The plot you will see is not smooth as it has only 10 values in one cycle.
Following command is used to obtain a 3D plot of variable y1, y2 and y3 used in the
previous step.
You will observe interesting patterns of 3D plot by changing the phase angle of one or more
variable (y1, y2, y3).
Exercise
In this section you will write MATLAB statements to evaluate the following time functions.
y = e-atsin(t)
Note: A MATLAB tutorial at the end of the textbook is also very helpful
for the introduction purpose.
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………