You are on page 1of 7

ECE120L – INTRODUCTION TO MATLAB

LABORATORY ACTIVITY #2
MATLAB Basic Functions and Commands

Name: Tagalog, Michelle Anne Therese C. Date: April 01, 2020


Section: A33 Laboratory Instructor: Jipcy Maurris Narvaez

I. Learning Outcomes:
At the end of the laboratory activity, the students should be able to:
1. Familiarize MATLAB environment, its capabilities, functions and basic commands
2. Use MATLAB in solving basic arithmetic problems

II. Laboratory Activity:


1.
a. Problem:
Create the following matrices using matrix shortcuts.
a. 5 – by – 4 identity matrix
b. 6 – by – 3 ones matrix
c. 4 – by – 5 zeros
d. 4 – by – 4 uniformly distributed pseudorandom numbers
e. 3 – by – 5 normally distributed pseudorandom numbers

b. MATLAB Codes:
a. >>x=eye(5,4)

x =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0

b. >>x=ones(6,3)
x=
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
c. >>x=zeros(4,5)
x=
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
d. >>x=rand(4)
x=
0.9040 0.9757 0.2695 0.7098
0.9409 0.3172 0.5896 0.3383
0.8025 0.8128 0.8330 0.0466
0.2420 0.6974 0.3638 0.7636
e. >>x=randn(3,5)
x=
-0.1052 -0.0006 -0.3317 -1.6747 -0.4051
2.6825 0.7730 0.4809 -0.7688 -0.5308
-1.5305 0.4586 -0.4939 -1.6105 0.3157

2. a. Problem:
If A = magic (24), what is the value in row 21, column 22?

b. MATLAB Codes:
>>A=magic(24);
A(21,22)
ans = 502

3. a. Problem:
Create the matrix X = diag ([3:3:15]).

b. MATLAB Codes:
>>X = diag ([3:3:15])
X=
3 0 0 0 0
0 6 0 0 0
0 0 9 0 0
0 0 0 12 0
0 0 0 0 15

4. a. Problem:
Using magic square of 5, write the values in lower triangular.
b. MATLAB Codes:
>>x=magic(5)
x =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> y=tril(x)
y=
17 0 0 0 0
23 5 0 0 0
4 6 13 0 0
10 12 19 21 0
11 18 25 2 9

5. a. Problem:
Using magic square of 4, write the values in upper triangular.

b. MATLAB Codes:
>>x=magic(5)
x =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>>z=triu(x)
z =
17 24 1 8 15
0 5 7 14 16
0 0 13 20 22
0 0 0 21 3
0 0 0 0 9

6. a. Problem:
Determine the determinant of the matrix.

b. MATLAB Codes:
>>x=[1 3 6;-2 5 9;7 -2 23];
y=det(x)
y = 274
7. a. Problem:
Determine the determinant and coefficients w, x, y, z
Given: w – 12x +13y – 7z = 23
2w + 15x + 16y + 12z = 19
4w – 10x + y + 17z = 24
3w + 2x – 3y + 2z = 14

b. MATLAB Codes:
>>x=[1,-12,13,-7;2,15,16,12;4,-10,1,17;3,2,-3,2]
x=
1 -12 13 -7
2 15 16 12
4 -10 1 17
3 2 -3 2
>>y=det(x)
y = 2.8495e+04

8. a. Problem:
Define the matrices below: (use shortcuts if applicable)

b. MATLAB Codes:
>>A=[1,2,3,4;6,8,1,5;9,4,2,7;9,4,7,2]
A=
1 2 3 4
6 8 1 5
9 4 2 7
9 4 7 2
>>B=ones(4)
B=
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>>C=magic(4)
C=
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>>D=diag([1,3,5,7])
D=
1 0 0 0
0 3 0 0
0 0 5 0
0 0 0 7
>>E=[1,0,0,0;0,1,0,0;0,0,0,1;1,0,0,0]
E=
1 0 0 0
0 1 0 0
0 0 0 1
1 0 0 0

>> F=[3,6,9,12;18,24,3,15;27,12,6,21;27,12,21,6]
F=
3 6 9 12
18 24 3 15
27 12 6 21
27 12 21 6
9. a. Problem:
Using the matrices of #8, solve the following:
a. 3A+4F
b. ABC/4F
c. 2DEF+4B
d. FEB/2CD
e. (2B – 3D)(2ABC)

b. MATLAB Codes:
a.>> (3*A)+(4*F)
ans =
15 30 45 60
90 120 15 75
135 60 30 105
135 60 105 30
b.>> (A*B*C)/(4*F)
ans =
4.7222 1.5741 -0.3148 1.8889
9.4444 3.1481 -0.6296 3.7778
10.3889 3.4630 -0.6926 4.1556
10.3889 3.4630 -0.6926 4.1556

c.>> (2*D*E*F)+(4*B)
ans =
10 16 22 28
112 148 22 94
274 124 214 64
46 88 130 172

d.>> (F*E*B)/(2*C*D)
ans =
-0.3539 -1.0616 1.0616 0.3539
-0.7077 -2.1231 2.1231 0.7077
-0.7785 -2.3354 2.3354 0.7785
-0.7785 -2.3354 2.3354 0.7785

e.>> ((2*B)-(3*D))*(2*A*B*C)
ans =
8024 8024 8024 8024
-2176 -2176 -2176 -2176
-12376 -12376 -12376 -12376
-21352 -21352 -21352 -21352
10.
a. Problem:
Create the matrix below using the matrices of #8.

b. MATLAB Codes:
>>[A B*2 E+3; F-2 C D]
ans =
1 2 3 4 2 2 2 2 4 3 3 3
6 8 1 5 2 2 2 2 3 4 3 3
9 4 2 7 2 2 2 2 3 3 3 4
9 4 7 2 2 2 2 2 4 3 3 3
1 4 7 10 16 2 3 13 1 0 0 0
16 22 1 13 5 11 10 8 0 3 0 0
25 10 4 19 9 7 6 12 0 0 5 0
25 10 19 4 4 14 15 1 0 0 0 7
III. Learnings and Conclusion

MATLAB® has been introduced in this experiment. MATLAB® can perform different commands that
could satisfy the basic needs of a problem, could serve as a solution and could be applied in different
aspects of life. This just proved that MATLAB® is useful and important in all professions especially
engineering professions.

To be familiarized in the MATLAB® language, tools and commands, this experiment gave a simple
introduction in its environment and a simple discussion of its basic built-in functions. The experiment
provided different mathematical problems to serve as an exercise in the familiarization of MATLAB®.
Matrix, which is one of the basic data elements of MATLAB®, is practiced. Several mathematical operations
that work on arrays and matrices are included in the functions of MATLAB® such as addition and
subtraction of matrices determinants and inverse matrices.

All the objectives of the experiment were reached so this experiment is considered as a success.

You might also like