0% found this document useful (0 votes)
18 views11 pages

LAB-02 - Solution

Uploaded by

hufuo0faypi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

LAB-02 - Solution

Uploaded by

hufuo0faypi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

University of frères Mentouri Engineering: Sciences and Technologies

Faculty of Science and Technology Cours of MATLAB


Department of electronics Academic-Year: 2023-2024

LAB 2: MATRICES AND VECTORS IN MATLAB


Exercise 01: use the command window of MATLAB to create the following vectors and matrices in two ways:
▪ 𝑋 = (10 11 12), 𝑌 = (10 11 12 1 1 1 1)
13 1 5 9 13
▪ 𝑊 = (14) , 𝑀 = (2 6 10 14)
15 3 7 11 15
13
14
15
▪ 𝑉 = 16
17
(18)
6 10 14 1 5
1) Create the matrices 𝑀1 and 𝑀2 from 𝑀, where 𝑀1 = ( ) , 𝑎𝑛𝑑 𝑀2 = ( )
7 11 15 3 7
2) Use the function linspace to create the vector 𝑉1 of 10 points between −𝜋 and 𝜋.
3) Find the transpose 𝑻 of 𝑀, and display the element 11 of 𝑇
4) Set all odd elements of the matrix 𝑀 to 0.
5) Remove all elements from even rows of matrix 𝑇.
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.
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.
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:
1) The average for each column
2) The largest element for each column
3) The smallest element for each column
4) The diagonal, the trace and the determinant of 𝑋.
Exercise 03: Type and explain the results of the following commands:
>> x=rand(1,5) >> A=rand(3) >> max(A') >> diff(A)
>> mean(x) >> sort(A) >> max(max(A)) >> D=A([1,2],1:3)
>> std(x) >> [B, I]=sort(A) >> sum(A) >> sum(D,1)
>> median(x) >> sort(A') >> cumsum(A) >> sum(D,2)
>> sort(x) >> max(A) >> prod(A) >> [pascal(3); randi(10,3,3)]
>> [1+2i 7-3i 3+4i; 6-2i 9i 4+7i]

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

LAB 2: MATRICES AND VECTORS IN MATLAB – SOLUTIONS


Solution of exercise 01:
Matrices and vectors creation: There are various methods you can use to generate or define vectors in MATLAB
such as manual entry, or by using functions, or the colon operator (:). Manual entry is suitable for creating
small vectors or matrices, while functions and the colon operator (:) are especially valuable for generating and
handling matrices or vectors that are too large to be inputted element by element.
Note: It is also recommended to start by the clear command in order to empty the workspace and clc command
to clear the command window of MATLAB.
>> clc
>> clear all
>> X = [10 11 12] % First method: raw vector X is manually entered
X =
10 11 12
>> X = [10:12] % Second method: raw vector X is entered by using the colon operator (:)
X =
10 11 12
>> Y = [10 11 12 1 1 1 1] % First method: raw vector Y is manually entered
Y =
10 11 12 1 1 1 1
% Second method: raw vector Y is entered by using the colon operator (:), the predefined
function ones and the concatenation method)
>> Y = [10:11, ones(1, 4)]
Y =
10 11 12 1 1 1 1
>> W = [13;14;15] % First method: column vector W is manually entered
W =
13
14
15

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

>> M = [1 5 9 13; 2 6 10 14; 3 7 11 15] % First method: matrix M is manually entered


M =

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) Find the transpose of 𝑀, and display the element 11 of the transpose of 𝑀.


>> T=M' % T is the transpose of M
T =
1 2 3
5 6 7
9 10 11
13 14 15
Linear indexing: This method involves numbering the elements of a matrix consecutively starting from the first
row and proceeding row by row. For example, in a 3 × 4 matrix, the linear indices would be assigned like
this: The first element (row 1, column 1) of the matrix has the order 1, the second element (row 2, column 1)
of the matrix has the order 2, etc. So, for instance, the element in the second row and third column (row 2,
column 4) would have a linear index of 11.

>> disp(M(11)) % by using the function disp().


14
>> M(11)
ans =
14

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

4) Set all odd elements of the matrix M to 0.


Odd elements of matrix M are in row 1 and 3. Thus, we can just set the elements of these rows to 0 as fellow:
>> M(1:2:3,:)=0 % Specify rows from 1 to 3 with a step size of 2 in order to skip row 2.
M =
0 0 0 0
2 6 10 14
0 0 0 0

5) Remove all elements from even rows of matrix T.


Even rows of matrix M are row 2 and row 4. Thus, we can just delete the elements of these two rows as fellow:
>> T(2:2:4,:)=[] % Specify rows from 2 to 4 with a step size of 2 in order to skip rows
1 and 3.
T =
1 2 3
9 10 11

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.

>> d = diag(X) d = diag(v) returns a square diagonal matrix with the


d = elements of vector v on the main diagonal. For example,
11 𝟏 𝟐 𝟏
𝒅𝒊𝒂𝒈 ([ ]) 𝒊𝒔 [ ]
43 𝟑 𝟒 𝟒
11
c = trace(A) calculates the sum of the diagonal elements of
>> c = trace(X)
c = matrix A. In this example, c is the sum of the elements of d
65 (𝟏𝟏 + 𝟏𝟏 + 𝟒𝟑 = 𝟔𝟓).

dt = det(X) returns the determinant of the square matrix X. If


>> dt=det(X)
dt = X contains only integer entries, the result dt is also an integer
-3.5250e+03

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

>> std(X) s=std(X) returns the standard deviation of the


ans = elements of X along the first array dimension whose
0.1357 size is greater than 1. By default, the standard
deviation is normalized by N-1, where N is the number
of observations.
▪ If X is a vector of observations, then is a scalar.
▪ If X is a matrix whose columns are random variables
and whose rows are observations, then s is a row
vector of the standard deviation corresponding to
each column.
This command returns the standard deviation of the
elements of X (a scalar =0.1357)
>> median(X)
M= median(A) returns the median value of A.
ans =
0.6463 ▪ If A is a vector, then median(A) returns the median
value of A.
▪ If A is a non-empty matrix, then median(A) treats
the columns of A as vectors and returns a row vector
of median values.
▪ If A is an empty 0-by-0 matrix, median(A) returns
NaN ("not a number" that means that they have
undefined numeric results, such as 0/0).
>> X = rand(1,5) % B = sort(A) sorts the elements of the matrix (or
X = vector) X in ascending order.
0.4898 0.4456 0.6463 0.7094 0.7547 ▪ If X is a vector, then sort(X) sorts the vector
>> sort(x) elements.
ans =
▪ If X is a matrix, then sort(X) treats the columns of X
0.4456 0.4898 0.6463 0.7094 0.7547
as vectors and sorts each column.
>> A = rand(3) % Returns an 3-by-3 matrix This command returns a 3-by-3 matrix of random
of random numbers numbers
A =
0.2760 0.1626 0.9597
0.6797 0.1190 0.3404
0.6551 0.4984 0.5853
>> sort(A) This command returns elements of each column of A
ans = in ascending order.
0.2760 0.1190 0.3404
0.6551 0.1626 0.5853
0.6797 0.4984 0.9597
>> [B, I] = sort(A) This command returns elements of each column of A
B = in ascending order.
0.2760 0.1190 0.3404 ▪ B lists the sorted elements and,
0.6551 0.1626 0.5853 ▪ I contains the corresponding indices of X
0.6797 0.4984 0.9597
I =
1 2 2
3 1 3
2 3 1
>> sort(A') This command returns the elements of each column
ans = of the transpose of A in ascending order
0.1626 0.1190 0.4984
0.2760 0.3404 0.5853
0.9597 0.6797 0.6551

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

>> max(A) This command returns a row vector containing the


ans = maximum value of each column of A
0.6797 0.4984 0.9597
>> max(A') This command returns a row vector containing the
ans = maximum value of each column of the transpose of A
0.9597 0.6797 0.6551
>> max(max(A)) This command returns the maximum value of the
ans = matrix A (a scalar)
0.9597
>> sum(A) S = sum(A) returns the sum of the elements of A along
ans = the first array dimension whose size is greater than 1.
1.6108 0.7800 1.8854 ▪ If A is a vector, then sum(A) returns the sum of
the elements.
▪ If A is a matrix, then sum(A) returns a row
vector containing the sum of each column.
This command returns the sum of elements of each
column of the matrix A.
>> cumsum(A)
In mathematics, A cumulative sum is a sequence of
ans = partial sums of a given sequence. For example, the
0.2760 0.1626 0.9597 cumulative sums of the sequence {a, b, c, ….} are a,
0.9557 0.2816 1.3001 a+b, a+b+c, ….
1.6108 0.7800 1.8854 B = cumsum(A) returns the cumulative sum of A
starting at the beginning of the first array dimension in
A whose size is greater than 1.
▪ If A is a vector, then B is a vector of the same size
containing the cumulative sum of A.
▪ If A is a matrix, then B is a matrix of the same size
containing the cumulative sum in each column of A
>> diff(A) Y = diff(X) calculates differences between adjacent
ans = elements of X along the first array dimension whose
0.4037 -0.0436 -0.6193 size does not equal 1:
-0.0246 0.3794 0.2449 ▪ If X is a vector of length m, then Y = diff(X) returns
a vector of length m-1. The elements of Y are the
differences between adjacent elements of X. Y =
[X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]
▪ If X is a nonempty, non-vector p-by-m matrix, then
Y = diff(X) returns a matrix of size (p-1)-by-m,
whose elements are the differences between the
rows of X. Y = [X(2,:)-X(1,:); X(3,:)-X(2,:); ...
X(p,:)-X(p-1,:)]
▪ If X is a 0-by-0 empty matrix, then Y = diff(X)
returns a 0-by-0 empty matrix.
>> D=A([1,2],1:3) The matrix D is a submatrix of A. D includes elements
D = of A that lie in the intersection of rows 1 to 2 and
0.2760 0.1626 0.9597 columns 1 to 3.
0.6797 0.1190 0.3404
A =
>> sum(D,1) 0.2760 0.1626 0.9597
0.6797 0.1190 0.3404
ans =
0.6551 0.4984 0.5853
0.9557 0.2816 1.3001
S = sum(A, dim) returns the sum along dimension
>> sum(D,2) dim. For example, Sum(D,1) returns a row vector
ans = containing the sum of each column. while, Sum(D,2)
-0.2592 returns a column vector containing the sum of each
0.5997 row.

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

>> prod(A) B = prod(A) returns the product of the array


ans = elements of A.
0.1229 0.0096 0.1912 ▪ If A is a vector, then prod(A) returns the product
of the elements.
▪ If A is a nonempty matrix, then prod(A) treats the
columns of A as vectors and returns a row vector
of the products of each column.
▪ If A is an empty 0-by-0 matrix, prod(A) returns 1.
>> [pascal(3); randi(10,3,3)] P = pascal( n ) returns a Pascal's Matrix of order n .
ans = P is a symmetric positive definite matrix with integer
1 1 1 entries taken from Pascal's triangle.
1 2 3
This command creates a Matrix by concatenating a
1 3 6
pascal matrix of dimension (3*3) and a 3-by-3 matrix
9 10 3 of random numbers from 1 to 10.
10 7 6
2 1 10
>> [1+2i 7-3i 3+4i; 6-2i 9i 4+7i] This command manually creates a 2 by 3 matrix of
ans = complex numbers.
1.00+2.00i 7.00-3.00i 3.00+4.00i
6.00-2.00i 0.00+9.00i 4.00+7.00i

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*(diag(J)+ones(3,1).^10) >> [H,J].^2

𝟐 ∗ (𝒅𝒊𝒂𝒈(𝑱)
⏟ +⏟
𝒐𝒏𝒆𝒔(𝟑, 𝟏) . ^𝟏𝟎) [𝐻, 𝐽]
⏟ . ^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(H);max(J)]-[min(H);min(J)] >> [J,H]*H


[𝐻, 𝐽] ∗ 𝐻

0 4 2 1 6 3
max(𝐻)
⏟ ; max(J)
⏟ − min(H)
⏟ ; min(J)
⏟ (6 20 1 4 2 2)
⏟ 20 2) (4 6 3)
(6 ⏟ 2 0) (1 1 0)
(0
⏟3 2 0 2 1 0
6 20 2 0 2 0 𝐓𝐡𝐢𝐬 𝐨𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐜𝐚𝐧 𝐧𝐨𝐭 𝐛𝐞 𝐩𝐞𝐫𝐟𝐨𝐫𝐦𝐞𝐝
[ ( ) ] [ ( ) ]
⏟ 4 6 3 1 1 0 𝐛𝐞𝐜𝐚𝐮𝐬𝐞 𝐭𝐡𝐞 𝐧𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐜𝐨𝐥𝐮𝐦𝐧𝐬 𝐨𝐟 𝐦𝐚𝐭𝐫𝐢𝐱 [𝐇,𝐉] (𝟔)
𝟔 𝟏𝟖 𝟐 𝐢𝐬 𝐧𝐨𝐭 𝐞𝐪𝐮𝐚𝐥 𝐭𝐨 𝐭𝐡𝐞 𝐧𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐫𝐨𝐰𝐬 𝐨𝐟 𝐦𝐚𝐭𝐫𝐢𝐱 𝐇 (𝟑)
( )
𝟑 𝟓 𝟑
>> [H;J]*H >> inv(H).\H’
[𝐻;
⏟ 𝐽] ∗𝐻 𝑖𝑛𝑣(𝐻)
⏟ .\ 𝐻′

0 4 2 0.0238 −0.0476 0.4286 0 6 3
6 20 1 (−0.0357 0.0714 −0.1429) (4 20 2)
3 2 0
1 6 3
⏟0.5714 −0.1429 0.2857 2 1 0
4 2 2 𝟎 −𝟏𝟐𝟔.𝟎𝟎𝟎𝟎 𝟕.𝟎𝟎𝟎𝟎
⏟2 1 0)
( (−𝟏𝟏𝟐.𝟎𝟎𝟎𝟎 𝟐𝟖𝟎.𝟎𝟎𝟎𝟎 −𝟏𝟒.𝟎𝟎𝟎)
𝟑𝟎 𝟖𝟒 𝟒 𝟑.𝟓𝟎𝟎𝟎 −𝟕.𝟎𝟎𝟎𝟎 𝟎
𝟏𝟐𝟑 𝟒𝟐𝟔 𝟑𝟐
𝟏𝟐 𝟓𝟐 𝟖
𝟒𝟓 𝟏𝟑𝟎 𝟖
𝟏𝟖 𝟔𝟎 𝟏𝟎
( 𝟔 𝟐𝟖 𝟓)
>> (diag(H)+diag(J))/2 >> (H^2)/J^H
(𝑑𝑖𝑎𝑔(𝐻)
⏟ + 𝑑𝑖𝑎𝑔(𝐽)
⏟ )/2 (𝐻^2)
⏟ / 𝐽^𝐻

𝟎 𝟏 30 84 4 𝐓𝐡𝐢𝐬 𝐨𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧 𝐜𝐚𝐧𝐧𝐨𝐭 𝐛𝐞 𝐩𝐞𝐫𝐟𝐨𝐫𝐦𝐞𝐝
(𝟐𝟎) (𝟐) (123 426 32) 𝐛𝐞𝐜𝐚𝐮𝐬𝐞 𝐭𝐡𝐞 𝐝𝐞𝐦𝐞𝐧𝐬𝐬𝐢𝐨𝐧 𝐟𝐨𝐫 𝐫𝐚𝐢𝐬𝐢𝐧𝐠
⏟ 𝟎 𝟎 12 52 8 𝐚 𝐦𝐚𝐭𝐫𝐢𝐱 𝐭𝐨 𝐚 𝐩𝐨𝐰𝐞𝐫.
𝟏
(𝟐𝟐)
T𝐡𝐞 𝐩𝐨𝐰𝐞𝐫 𝐬𝐡𝐨𝐮𝐥𝐝 𝐛𝐞 𝐚 𝐬𝐜𝐚𝐥𝐫𝐞
⏟ 𝟎
𝟎.𝟓𝟎𝟎𝟎
(𝟏𝟏.𝟎𝟎𝟎𝟎)
𝟎
>> min(H/J) + (1/2) >> prod(J)-max(H)
min ( 𝐻/𝐽
⏟ ) + ( 1/2
⏟) 𝑝𝑟𝑜𝑑(𝐽)
⏟ −⏟
max (𝐻)
0.7273 −0.0909 −0.1818 0.5000 (⏟
(3.0909 −4.1364 9.7273 )
8 12 0) (6 20 2)
⏟ 0.0909 −0.1364 1.7273 (𝟐 −𝟖 −𝟐)
⏟ (0.0909 −4.1364 −0.1818)
(−𝟎.𝟒𝟎𝟗𝟏 −𝟒.𝟔𝟑𝟔𝟒 −𝟎.𝟔𝟖𝟏𝟖)

>> [max(H); min(H)]*trace(J) >> prod(J).\max(H))*H

𝑝𝑟𝑜𝑑(𝐽)
⏟ .\ 𝑚𝑎𝑥(𝐻)
⏟ ∗𝐻
[ max(𝐻)
⏟ ; min(𝐻)
⏟ ] ∗ 𝑡𝑟𝑎𝑐𝑒(𝐽)
⏟ (⏟
8 12 0) (6 20 2)
⏟(6 20 2) (0 2 0) 3
𝟏𝟖 𝟔𝟎 𝟔

(0.7500 1.6667 𝐼𝑛𝑓)
( )
𝟎 𝟔 𝟎 (𝑰𝒏𝒇 𝑰𝒏𝒇 𝑵𝒂𝑵)

Inf represents infinity. It results from operations like division by


zero and overflow, which lead to results too large to represent
as conventional floating-point values.
NaN: operations return NaN when they have undefined numeric
results, such as 0/0 or 0*Inf.
>> prod(J)\max(H))*H

𝑝𝑟𝑜𝑑(𝐽)
⏟ \ 𝑚𝑎𝑥(𝐻)
⏟ ∗𝐻
(⏟
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

You might also like