engineering _workshop_Lab1
engineering _workshop_Lab1
(EL-1006)
LABORATORY MANUAL
INTRODUCTION TO MATLAB
(LAB # 01)
Engr. Shehzad Ahmad
Engr. Tooba
Student Name: ______________________________________________
____________________________________________________________________________________________________________________________________________________________
Description:
Matlab (stands for Matrix Laboratory), it integrates computation, visualization, and programming
in an easy-to-use environment, where problems and solutions are expressed in familiar
mathematical notation.
Examples:
Strengths of MATLAB
“Fig 1.1”
Desktop Tools
1. Command Window
Use the Command Window to enter variables and run functions and M-files.
“Fig 1.2”
2. Command History:
Statements you enter in the Command Window are logged in the Command History. In
the Command History, you can view previously run statements, and copy and execute selected
statements.
Enter a command “commandhistory” in Command Window to see commands historY.
“Fig 1.3”
“Fig 1.4”
“Fig 1.5”
3. Workspace Browser:
The MATLAB workspace consists of the set of variables (named arrays) built up during
a MATLAB session and stored in memory.
“Fig 1.6”
4. Variable Tab:
Double-click a variable in the Workspace browser to see it in the Variable Tab.
Use the Variable Tab to view and edit a visual representation of one- or two-dimensional
numeric arrays, strings, and cell arrays of strings that are in the workspace.
“Fig 1.7”
5. Current Folder:
It lists all the files that are available in current directory.
“Fig 1.8”
“Fig 1.9”
Reserved Words:
ENGINEERING WORKSHOP LAB NUCES FAST, ISLAMABAD Page 6 of 14
Introduction to MATLAB LAB 1
Reserved Words List:
for end if while
function elseif case otherwise
Addition + 3 + 22
Subtraction - 54.4 – 16.5
Multiplication * 3.14 * 6
Division / or \ 10/2 or 2\10
Expressions are evaluated from left to right, with precedence;
Exponentiation > Multiplication & Division > Addition & Subtraction.
The first character MUST be alphabetic followed by any number of letters, digits, or
underscore.
The variable names are case sensitive.
Blanks are NOT allowed in a variable name.
Variable names can contain up to 63 characters.
Punctuation characters are not allowed, because many of them have special meanings in
Matlab.
Examples:
>>a = 2
a=
ans =
6.2832
Types of Variables:
Type Examples
Integer 1362,-5656
Real 12.33,-56.3
Complex X=12.2 – 3.2i (i = sqrt(-1))
Complex numbers in MATLAB are represented in rectangular form. To separate real & imaginary
part
H = real(X)
K = imag(X)
Conversion between polar & rectangular
C1 = 1-2i
Magnitude: mag_c1 = abs(C1)
Angle: angle_c1 = angle(C1)
Note that angle is in radians
Extras:
Using up-arrow key allow user to recall most recently used commands
Another trick is to use a ‘letter’ prior using up-arrow
: specify range
MATLAB Matrices:
MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an
array, in fact, that is how it is stored.
Vectors are special forms of matrices and contain only one row OR one column.
Scalars are matrices with only one row AND one column.
A matrix can be created in MATLAB as follows (note the commas AND semicolons):
ENGINEERING WORKSHOP LAB NUCES FAST, ISLAMABAD Page 8 of 14
Introduction to MATLAB LAB 1
» matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
1 2 3
4 5 6
7 8 9
Row Vector:
A matrix with only one row is called a row vector. A row vector can be created in MATLAB
as follows (note the commas):
» rowvec = [12 , 14 , 63]
rowvec =
12 14 63
Row vector can also defined in a following way:
rowvec = 2 : 2 : 10; % start : step size : stop
rowvec =
2 4 6 8 10
Column Vector:
A matrix with only one column is called a column vector. A column vector can be created in
MATLAB as follows (note the semicolons):
colvec =
13
45
-2
Extracting a Sub-Matrix:
A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of
both matrices and the rows and columns to extract. The syntax is:
sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;
where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the
beginning and ending columns to be extracted to make the new matrix.
A column vector can be extracted from a matrix. As an example we create a matrix below:
1 2 3 col_two = 2
4 5 6 5
7 8 9 8
A row vector can be extracted from a matrix. As an example we create a matrix below:
1 2 3 » rowvec =matrix(2 : 2 , 1 : 3)
4 5 6 rowvec =
7 8 9 4 5 6
Concatenation:
New matrices may be formed out of old ones
Suppose we have:
a = [1 2; 3 4]
a=12
34
Input output
[a , a, a] ans = 1 2 1 2 1 2
3 4 3 4 3 4
[a ; a; a] ans =
1 2
3 4
1 2
3 4
1 2
3 4
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
» c = a * b % Multiply each element of b by a
c=
3 6 9
12 15 18
ans =
min(a) :Return a row vector containing the minimum element from each column.
ans = 1 2 3
max(a) : Return a row vector containing the maximum element from each column.
ans = 7 8 9
size (a) : gives the number or rows and the number of columns:
>> [r c] = size(a)
r=3 c=3
Let a =[ 4 5 6] , length(a) finds the number of elements in row vector.
a.*b :Bitwise multiply the each element of vector ‘a’ and ‘b’:
ans =
4 10 18
Matrix Division:
MATLAB has several options for matrix division. You can “right divide” and “left divide”.
Matrix of Zeros:
Example;
>> zeros(2) >> zeros(1,2)
ans = ans =
0 0 0 0
ENGINEERING WORKSHOP LAB NUCES FAST, ISLAMABAD Page 12 of 14
Introduction to MATLAB LAB 1
0 0
Matrix of Ones:
Example;
>> I=eye(3)
I=
100
010
001
Exercises:-
NOTE:
Solve these questions in MATLAB and write answers/code in the manual.
Q.1 Run the MATLAB help desk by typing helpdesk. The help desk provides a hypertext interface
to the MATLAB documentation.
A=
[ 26 34 ] [ 16 62 ]
,B=
TASKS:
Matrix Manipulation:
A. Generate a 6x6 matrix A with magic command and replace first column with:
1
-2
-3
-4
-5
-6
E. Extract a 4x4 matrix (first 4 rows and first 4 columns) from matrix A.
F. Write commands to make a matrix of 2nd , 4th and 6th columns of the original matrix?