Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Lab 3 MATLAB

Uploaded by

mohammed rasheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lab 3 MATLAB

Uploaded by

mohammed rasheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Islamic University ‫الجامعة اإلسالمية‬

Faculty of Engineering
Department of Electrical
‫كلية الهندسة‬
Engineering ‫قسم الهندسة الكهربائية‬

ENGR 3032

SCIENTIFIC COMPUTING LANGUAGES


Fall Semester 2024 (Term 461)

LAB 3

Problem Solving using scientific computing languages:


Matrix manipulation
[CO 3; PI_6_3; SO 6]

Submitted To
Eng. Osamah Alkhalifah

Submitted By
Mohammed Rasheed
431014033

Section No: 2623

September 25, 2024

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:

A = [3.5]; % produces a 1 by 1 matrix or a vector with one element


B = [1.5, 3.1]; or B = [1.5 3.1];% produces a 1x2 matrix or a vector with 2 elements
C = [-1, 0, 0; 1, 1, 0; 0, 0, 2];% produces a 3x3 matrix.
returns
C=

-1 0 0
1 1 0
0 0 2

(Note: a vector is a matrix with one row or one column)


MATLAB / OCTAVE also allows you to define a matrix in terms of another matrix that has already
been defined. For example, the statements (i.e., a matrix can have sub-matrices inside):
B = [1.5, 3.1];
S = [3.0, B]
returns
S=
3.0 1.5 3.1
Similarly,
T = [ 1, 2, 3; S]
returns
T=
1 2 3
3 1.5 3.1

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:

>> x = M(:, 1) % The first column is selected and assigned to x


x=
1
2
3
>> y = M(:, 4) %% The fourth column (with all rows) is selected and assigned to y
y=
4
5
6
>> z = M(1,:) %% The first row (with all columns) is selected and assigned to z
z=
1 2 3 4 5

>>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

✓ Matrix multiplication versus element-wise product


Matrix multiplication: The result of the command A*B produces a new matrix; multiplies each row in
matrix A by the columns of matrix B. This is possible only if the number of rows in the first matrix
equals the number of columns of the second one. Otherwise, an error is displayed.

>>j*j %% matrix multiplication is not possible


error: operator *: non-conformant arguments (op1 is 1x4, op2 is 1x4)

>>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.

>> [new_x, new_y]=meshgrid(x,y)


new_x =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
new_y =

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

✓ Reading data from Excel sheets:


The function xlsread permits to read data from Excel sheets and save the
data in different matrices; each contain a different view/representation of the
data.The syntax of the command is as follows:
[Dn, Dt, Dtxt] = xlsread(‘filename.xlsx’, ‘name_of_sheet’)
If the name of the sheet is not specified ..the first sheet will be read by default.
To read the first sheet:
[Dn, Dt, Dtxt] = xlsread(‘filename.xlsx’)

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]

a. Create a vector called d from the second column of matrix a


b. Combine matrix b and matrix d into a new matrix e, a two-dimensional matrix with three rows
and two columns.
c. Combine matrix b and matrix d to create matrix f, a one-dimensional matrix with six rows and
one column.
d. Create a matrix g from matrix a and the first three elements of matrix c, with four rows and three
columns.
e. Create a new matrix h with the first element equal to a1, 3, the second element equals c2, 2and
the third element equal to b1, 2

ENGR 3032
ENGR 3032
ENGR 3032
ENGR 3032
Task 2

Using meshgrid function:


1. The area of a rectangle is its length times width (area = length * width). Find the areas of
rectangles with lengths of 11, 30, and 15 cm and with widths of 8, 9, 10, and 12 cm. (You
should have 12 answers.)

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

You might also like