LAB-02 - Solution
LAB-02 - Solution
Exercise 04: Create the matrices H and J, then use them to perform the indicated operations if possible. If not
possible, explain why the operation cannot be performed.
0 4 2 1 6 3
𝐻 = (6 20 1) , 𝐽 = (4 2 2)
3 2 0 2 1 0
>> 10*(H+J)-J.^2 >> [H;J]*H >> inv(H).\H’
>> (H.*3)+J./2 >> [J,H]*H >> (diag(H)+diag(J))/2
>> 2*(diag(J)+ones(3,1).^10) >> (H^2)/J^H >> min(H/J) + (1/2)
>> [H,J].^2 >> [max(H); min(H)]*trace(J) >> prod(J)-max(H)
>> [max(H);max(J)]-[min(H);min(J)] >> prod(J)\max(H))*H >> (prod(J).\max(H))*H
Exercise 05: Consider the linear systems below. Use MATLAB commands to solve them.
𝑥 − 2𝑦 + 3𝑧 = 9 2𝑥 − 𝑦 + 𝑧 = −1
{ −3𝑥 + 3𝑦 = −4 { −3𝑥 + 3𝑦 − 2𝑧 = 7
2𝑥 − 5𝑦 + 5𝑧 = 17 5𝑥 − 𝑦 + 2𝑧 = 3
1|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
>> V = [13; 14; 15; 16; 17; 18] % First method: Column vector V is manually entered
V =
13
14
15
16
17
18
>> V = [W; [16; 17; 18]] % Second method: Column vector V is created by concatenating W
and vector [16;17;18]]
V =
13
14
15
16
17
18
2|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
1 5 9 13
2 6 10 14
3 7 11 15
% Second method: matrix M is created by created 3 row vectors with the range method.
% [1:4:13] a row vector from 1 to 13 with a step size of 4
% [2:4:14] a row vector from 1 to 14 with a step size of 4
% [3:4:15] a row vector from 1 to 15 with a step size of 4
>> M = [[1:4:13]; [2:4:14]; [3:4:15]]
M =
1 5 9 13
2 6 10 14
3 7 11 15
6 10 14 1 5
1) Create the matrices 𝑀1 and 𝑀2 from 𝑀, where 𝑀1 = ( ) , 𝑎𝑛𝑑 𝑀2 = ( )
7 11 15 3 7
>> M1 = M(2:3, 2:4) % M1 is a extracted from matrix M with the colon operator (:)
M1 =
6 10 14
7 11 15
>> M2 = M(1:2:3, 1:2) % M2 is a extracted from matrix M with the colon operator (:)
M2 =
1 5
3 7
2) Use the function linspace to create the vector V1 of 10 points between 0 and 1.
v = linspace(-pi, pi, 10)
v =
-3.1416 -2.4435 -1.7453 -1.0472 -0.3491 0.3491 1.0472 1.7453 2.4435 3.1416
3|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
6) Use the commands whos and save to respectively display and save X, Y, V, W, M, M1, M2 and T in the
file “exercise-1.mat”, then clear the workspace and the command window.
>> whos X Y V W M M1 M2 T
Name Size Bytes Class Attributes
M 3x4 96 double
M1 2x3 48 double
M2 2x2 32 double
T 2x3 48 double
V 6x1 48 double
W 3x1 24 double
X 1x3 24 double
Y 1x6 48 double
>> save exercise-1.mat X Y V W M M1 M2 T
>> clear all % clear all variables in the workspace.
As you can see in the figure below, the new MAT file “exercise-1.mat” is add to the files of the current directory.
As you can see in the figure below, the workspace is empty after executing the command clear all.
4|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
7) Use the command load in order to load again variables, Y, V, W, M, M1, M2 and T from the MAT-file
“exercise-1.mat” into the workspace and display the values of each one.
>> load("/My-MATLAB-Project/exercise-1.mat") % My-MATLAB-PROJECT is the current
directory
>> M
M =
0 0 0 0
2 6 10 14
0 0 0 0
>> T
T =
1 2 3
9 10 11
>> M1
M1 =
6 10 14
7 11 15
>> M2
M2 =
1 5
3 7
>> X
X =
10 11 12
>> Y
Y =
10 11 1 1 1 1
>> W
W =
13
14
15
>> V
V =
13
14
15
16
17
18
As you can see in the figure below, after running the command load, all the stored variables in the mat file have
loaded to the workspace with their correct values.
5|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Solution of exercise 02
Use the function randi to create a random matrix 𝑋 of (3×3) elements between 10 and 50, then use the pre-defined
functions mean, max, min, diag, trace and det to calculate:
MATLAB command Explication
The randi () function is a versatile tool that allows you to
>> X = randi([10 50],3,3)
generate random integers within a specified range. For
X =
instance, R = randi ([10, 50], 3, 3), Here, [10, 50] represents
11 13 23
the inclusive range within which the random integers are
21 43 48
11 38 11 generated, and the 3 and 3 specify the dimensions of the
resulting matrix or array.
M = mean(A) returns the mean (or the average) of the
elements of the matrix (vector) A along the first array
>> M=mean(X) % mean of each column dimension whose size is greater than 1.
M = ▪ If A is a vector, then mean(A) returns the mean of the
14.3333 31.3333 27.3333 elements.
▪ If A is a matrix, then mean(A) returns a row vector
containing the mean of each column.
M = max(A) returns the maximum elements of an array.
M = max(X) % max of each column
▪ If A is a vector, then max(A) returns the maximum of
M =
the elements of A (a scalar).
21 43 48
▪ If A is a matrix, then max(A) is a row vector containing
the maximum value of each column of A.
M = min(A) returns the minimum elements of an array.
>> M = min(X) % min of each column ▪ If A is a vector, then min(A) returns the minimum of A.
M = ▪ If A is a matrix, then min(A) is a row vector containing
11 13 11 the minimum value of each column of A.
Solution of exercise 03: Type and explain the results of the following commands:
Results of the MATLAB command Explanation of the results
>> X = rand(1,5) X = This command returns a 1-by-5 matrix of random
0.4898 0.4456 0.6463 0.7094 0.7547 numbers
>> mean(X) This command returns the mean (or average) of the
ans = elements of the vector X. (mean is a scalar=0.6091)
0.6091
6|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
7|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
8|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Solution of exercise 04
Create the matrices H and J, then use them to perform the indicated operations if possible. If not possible, explain
why the operation cannot be performed.
0 4 2 1 6 3
𝐻 = (6 20 1) , 𝐽 = (4 2 2)
3 2 0 2 1 0
Operation result and explanation Operation result and explanation
>> clc
>> clear all
>> H = [0 4 2; 6 20 1; 3 2 0]; % Create matrix H
>> J = [1 6 3; 4 2 2; 2 1 0]; % Create matrix J
>> 10*(H+J)-J.^2 >> (H.*3)+J./2
10 ∗ 𝐻+𝐽
⏟ − 𝑗.
⏟ ^2 𝑯.∗ 𝟑
⏟ + 𝑱./𝟐
⏟
1 10 5 1 36 9 𝟎 𝟏𝟐 𝟔 𝟎.𝟓𝟎𝟎𝟎 𝟑.𝟎𝟎𝟎𝟎 𝟏.𝟓𝟎𝟎𝟎
(10 22 3) (16 4 4) (𝟏𝟖 𝟔𝟎 𝟑) (𝟐.𝟎𝟎𝟎𝟎 𝟏.𝟎𝟎𝟎𝟎 𝟏.𝟎𝟎𝟎𝟎 )
⏟ ( 5 3 0 ) 4 1 0 (
⏟ 𝟗 𝟔 𝟎 ) 𝟏.𝟎𝟎𝟎𝟎 𝟎.𝟓𝟎𝟎𝟎 𝟎
10 100 50 𝟎.𝟓𝟎𝟎𝟎 𝟏𝟓.𝟎𝟎𝟎𝟎 𝟕.𝟓𝟎𝟎𝟎
(100 220 30) (𝟐𝟎.𝟎𝟎𝟎𝟎 𝟔𝟏.𝟎𝟎𝟎𝟎 𝟒.𝟎𝟎𝟎𝟎)
⏟ 50 30 0 𝟏𝟎.𝟎𝟎𝟎𝟎 𝟔.𝟓𝟎𝟎𝟎 𝟎
𝟗 𝟔𝟒 𝟒𝟏
(𝟖𝟒 𝟐𝟏𝟔 𝟐𝟔)
𝟒𝟔 𝟐𝟗 𝟎
𝟐 ∗ (𝒅𝒊𝒂𝒈(𝑱)
⏟ +⏟
𝒐𝒏𝒆𝒔(𝟑, 𝟏) . ^𝟏𝟎) [𝐻, 𝐽]
⏟ . ^2
𝟏 𝟏
(𝟐) (𝟏 )
0 4 2 1 6 3
𝟎 ⏟ 𝟏 (6 20 1 4 2 2)
𝟏
(𝟏)
⏟3 2 0 2 1 0
⏟ 𝟏 𝟎 𝟏𝟔 𝟒 𝟏 𝟑𝟔 𝟗
𝟐 (𝟑𝟔 𝟒𝟎𝟎 𝟏
(𝟑 )
𝟏𝟔 𝟒 𝟒)
⏟ 𝟏 𝟗 𝟒 𝟎 𝟒 𝟏 𝟎
𝟒
(𝟔 )
𝟐
9|Page
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
𝑝𝑟𝑜𝑑(𝐽)
⏟ .\ 𝑚𝑎𝑥(𝐻)
⏟ ∗𝐻
[ max(𝐻)
⏟ ; min(𝐻)
⏟ ] ∗ 𝑡𝑟𝑎𝑐𝑒(𝐽)
⏟ (⏟
8 12 0) (6 20 2)
⏟(6 20 2) (0 2 0) 3
𝟏𝟖 𝟔𝟎 𝟔
⏟
(0.7500 1.6667 𝐼𝑛𝑓)
( )
𝟎 𝟔 𝟎 (𝑰𝒏𝒇 𝑰𝒏𝒇 𝑵𝒂𝑵)
𝑝𝑟𝑜𝑑(𝐽)
⏟ \ 𝑚𝑎𝑥(𝐻)
⏟ ∗𝐻
(⏟
8 12 0) (6 20 2)
𝟎 𝟎 𝟎
(𝟎.𝟓𝟎𝟎𝟎 𝟏.𝟔𝟔𝟔𝟕 𝟎.𝟏𝟔𝟔𝟕)
(
⏟ 𝟎 𝟎 𝟎 )
𝟎 𝟎 𝟎
(𝟏𝟎.𝟓𝟎𝟎𝟎 𝟑𝟓.𝟔𝟔𝟔𝟕 𝟐.𝟔𝟔𝟔𝟕 )
𝟎 𝟎 𝟎
10 | P a g e
University of frères Mentouri Engineering: Sciences and Technologies
Faculty of Science and Technology Cours of MATLAB
Department of electronics Academic-Year: 2023-2024
Solution of exercise 05
Consider the linear systems below. Use MATLAB commands to solve them.
𝑥 − 2𝑦 + 3𝑧 = 9 1 −2 3 𝑥 9
(1) { −3𝑥 + 3𝑦 = −4 => (−3 3 0) × (𝑦) = (−4)
2𝑥 − 5𝑦 + 5𝑧 = 17 2 −5 5 𝑧 17
Commands MATLAB
>> A = [1 -2 3; -3 3 0; 2 -5 5] % Creation of the coefficient matrix A
A =
1 -2 3
-3 3 0
2 -5 5
>> B = [9 ; -4 ; 17] % Creation of the column vector B
B =
9
-4
17
>> det(A) % Check if A is inversible? (det(A)≠0)
ans =
12.0000
>> X = inv(A) * B % Solution
X =
0.1667
-1.1667
2.1667
2𝑥 − 𝑦 + 𝑧 = −1 2 −1 1 𝑥 −1
(2) {−3𝑥 + 3𝑦 − 2𝑧 = 7 => (−3 3 −2) × (𝑦) = ( 7 )
5𝑥 − 𝑦 + 2𝑧 = 3 5 −1 2 𝑧 3
Commands MATLAB
>> A = [2 -1 1; -3 3 -2; 5 -1 2] % Creation of the coefficient matrix A
A =
2 -1 1
-3 3 -2
5 -1 2
>> B = [-1 ; 7 ; 3] % Creation of the column vector B
B =
-1
7
3
>> det(A) % Check if A is inversible? (det(A)≠0)
ans =
6.6613e-16
>> X = inv(A) * B % Solution
X =
0
4
0
11 | P a g e