Lab 3 MATLAB
Lab 3 MATLAB
Faculty of Engineering
Department of Electrical
كلية الهندسة
Engineering قسم الهندسة الكهربائية
ENGR 3032
LAB 3
Submitted To
Eng. Osamah Alkhalifah
Submitted By
Mohammed Rasheed
431014033
ENGR 3032
Objective:
The objective of this Lab session is to learn how to useuse matrices, combine small matrices into larger
ones, extract information from Excel spreadsheet and create very large matrices
✓ Matrices:
In MATLAB / OCTAVE, matrix can be defined by typing in a list of numbers enclosed in square
brackets. Matrices can be used to represent a system of linear equations. The numbers can be separated
by spaces or by commas. (You can even combine the two techniques in the same matrix definition.) To
assign two rows in a single line, use semicolon to separate rows.
For example:
-1 0 0
1 1 0
0 0 2
To change elements in a matrix, or include additional values, the index number is used to specify a
particular element. This process is called indexing into anarray. Thus, the command:
S(2) = -1.0;changes the second value in the vector S from 1.5 to –1. If we type the matrix (or
vector) name S into the command window, then MATLAB / OCTAVE returns
S=
3.0 -1.0 3.1
ENGR 3032
✓ Using the Colon Operator
The colon operator is used to in define new matrices and modify existing ones. To define an evenly
spaced vector. For example,
H = 1:8
returns
H=
12345678
The default spacing is 1. However, when colons are used to separate three numbers, the middle value
becomes the spacing or the step (increment). Thus,
time = 0 : 0.5 : 2
returns
time =
0 0.5 1.0 1.5 2.0
The colon operator can also be used to extract data from matrices, a feature that is very useful in data
analysis. When a colon is used in a matrix reference in place of a specific index number, the colon
represents the entire row or column.
Suppose we define M as
>> M = [1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7]
M=
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
We can select a column and row from matrix M with the commands:
>>w = M(2:3,:) %% The second and third rows (with all columns) are assigned to w
w=
ENGR 3032
2 3 4 5 6
3 4 5 6 7
>>w = M(2:3, 4:5)%% The the 2nd and 3rd rows with the 4th and 5th columns are selected and assigned to w
w=
5 6
6 7
>>M(2, 3) %% The element at index 2, 3 (2nd row and 3rd column)
ans =
4
>>M(8) %% The 8th element is selected starting from the first element on the top left.
ans =
4
>>j'*j
%% matrix multiplication is possible (multiplication between a matrix and its transposed matrix)
ans =
16 20 24 28
20 25 30 35
24 30 36 42
28 35 42 49
>> A = [1 3; 2 4]
A=
1 3
2 4
>> B = [3 0; 1 5]
B=
3 0
1 5
>> A*B
ans =
6 15
ENGR 3032
10 20
Element-wise product: The result of the command A.*B produces a new matrix; multiplies each element
in matrix A by the corresponding element of matrix B. This is possible only if the both matrices have
similar row or column sizes. Otherwise, an error is displayed.
Examples:
J = [4 5 6 7]
>> j.*j %% each point in j is multiplied by itself
ans =
16 25 36 49
A.*B
ans =
3 0
2 20
(Notice that A.*B and A*B are not the same; A.*B is element-wise product and A*B is matrix multiplication)
>>x=1:5
x=
1 2 3 4 5
>>y=1:3
y=
1 2 3
>>a=x.*y
Error using.*
Matrix dimensions must agree.
In order to avoid the error, MATLAB has a built-in function called meshgrid that will help to accomplish
with solution even if the x and y are not in same size.
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
ENGR 3032
>> A = new_x.*new_y
A=
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
ENGR 3032
✓ Special Matrices and special operations
Table 3.1
ENGR 3032
Task 1
Q1. Create the following matrices, and use them in the exercises that follow:
51 23 12
a= [30 18 15]
44 63 92
b= [11 8 2]
c= [72 38 55 82]
ENGR 3032
ENGR 3032
ENGR 3032
ENGR 3032
Task 2
ENGR 3032
ENGR 3032
2. The volume of a circular cylinder is, volume = πr2h. Find the volume of cylindrical
containers with radii from 0 to 14 m and heights from 5 to 15 m. Increment the radius
dimension by 5 m and the height by 2 m as you span the two ranges.
ENGR 3032
ENGR 3032
Task 3
1. Create a 4 × 6 matrix in which all the elements have a value of pi.
2. Create a 6 × 6 magic matrix.
a) What is the sum of each of the rows?
b) What is the sum of each of the columns?
c) What is the sum of each of the diagonals?
3. Create a 10 × 10 magic matrix.
a) Extract the diagonal from this matrix.
b) Extract the diagonal that runs from lower left to upper right from this matrix.
c) Confirm that the sums of the rows, columns, and diagonals are all the same.
ENGR 3032
ENGR 3032
ENGR 3032
ENGR 3032
ENGR 3032
ENGR 3032
Task 4
1. Using the xlsread function perform the following operations:
a. Extract the scores and the student ID for the 7th student into a row vector named student_5 .
b. Extract the scores for the Lab Experiment into a column vector named Lab.
c. Find the standard deviation and variance for the total score and for the lab experiment.
ENGR 3032
ENGR 3032
ENGR 3032