LAB-1-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

EXPERIMENT NUMBER: 1

Department of Electrical and Computer Engineering

LAB REPORT:
1

Name Farid Uddin


Registration Number FA21-BEE-016/ATK
Class BEE-07
Subject Numerical Computation (NC)
Submitted To Dr. Babar Sattar

1|Page
EXPERIMENT NUMBER: 1
LAB TITLE

Introduction to MATLAB (Basic Commands) & Basic tools (Addition, Subtraction,


Transpose, Determinant of Matrices).
LAB TASK # 1

1. Prepare the following vectors:

a. A’ of even whole numbers between 19 and 63.

b. ‘B’ of odd whole numbers between 66 and 120.

Part: a & b
MATLAB Code:
% 1a. Even whole numbers between 19 and 63
A = 20:2:62;

% 1b. Odd whole numbers between 66 and 120


B = 67:2:119;

Console Window:

LAB TASK # 2

2|Page
EXPERIMENT NUMBER: 1
1. Let x = [3 -2 0 5 1 9 5 6], compute the following:

a. Add (Last digit of Reg No.) to each element of this vector.

b. Multiply the even indexed elements with 4.

c. Compute the square root of each element.

d. Compute the cube of each element.

Part: a, b, c & d
MATLAB Code:
% Given vector
x = [3 -2 0 5 1 9 5 6];

% Assuming the last digit of the registration number is '5' (replace with actual value)
last_digit_reg_no = ;

% 2a. Add the last digit of Reg. No. to each element of the vector
x_plus_reg_no = x + last_digit_reg_no;

% 2b. Multiply the even indexed elements with 4


% Even indexed elements are at positions 2, 4, 6, etc.
x_even_index_multiplied = x;
x_even_index_multiplied(2:2:end) = x(2:2:end) * 4;

% 2c. Compute the square root of each element (negative numbers will return NaN)
x_sqrt = sqrt(x);

% 2d. Compute the cube of each element


x_cubed = x.^3;

Console Window:
a The vector x is updated by adding the last digit of the registration number (assumed to be 5) to
each element. This results in each element of x being incremented by 5.
b The even-indexed elements of x (at positions 2, 4, 6, etc.) are multiplied by 4. This modifies
only the elements at those specific positions, while the others remain unchanged.
c The square root of each element in x is computed. For non-negative values, the square root is
returned as expected, while negative numbers result in NaN values since square roots of
negative numbers are not real.
d Each element of x is cubed, producing a new vector where each element is raised to the power
of 3.

3|Page
EXPERIMENT NUMBER: 1

LAB TASK # 3
1. Let x = [4 3 9 1] and y = [2 1 9 -3], compute the following:
a) Add the sum of the elements of x to y
b) Raise each element of y to the power specified by the corresponding element of x.
c) Multiply each element in x by the corresponding element in y and store the result in ‘c’.
d) Evaluate y*x’ and interpret the result.

Part: a, b, c & d
MATLAB Code:
% Given vectors
x = [4 3 9 1];
y = [2 1 9 -3];

% 3a. Add the sum of the elements of x to y


sum_x = sum(x);
y_plus_sum_x = y + sum_x;

% 3b. Raise each element of y to the power specified by the corresponding element of x
y_raised_to_x = y.^x;

% 3c. Multiply each element in x by the corresponding element in y and store the result
in 'c'
c = x .* y;

% 3d. Evaluate y*x' and interpret the result (dot product)


y_dot_x = y * x'; % Dot product of y and x

Console Window:

4|Page
EXPERIMENT NUMBER: 1

LAB TASK # 4
1. Find a short MATLAB expression to prepare to following matrix:

2. Given a vector t = [-3:0.2:2] write down the MATLAB expressions that will correctly calculate

a. ln(1 − 𝑡 + 𝑡3)
the following:

b. 𝑒𝑥𝑝[1 + sin(3 ∗ 𝑡2)]


c. cos(𝑡)2 + sin(2 ∗ 𝑡)−3

Part: 1 & 2
MATLAB Code:
% 1. Prepare matrix B
B = [1 2 3 4 5 6 7;
9 7 5 3 1 -1 -3;
4 8 16 32 64 128 256];
% Given vector t
t = -3:0.2:2;

% part 2 (a): ln(1 - t + t^3)


result_a = log(1 - t + t.^3);

% part 2 (b): exp(1 + sin(3 * t^2))


result_b = exp(1 + sin(3 * t.^2));

% part 2 (c): cos(t)^2 + sin(2 * t)^(-3)


result_c = cos(t).^2 + sin(2 * t).^(-3);

Console Window:

5|Page
EXPERIMENT NUMBER: 1

LAB
TASK # 5

Construct a vector v=[1 2 3 4 5 6 7 8 9 10], develop an algorithm such that the first element of the
vector is multiplied by length(v), second element by length(v)-1and similarly the last element i.e. 10 is
multiplied by length(v)-9. The final vector should be f=[10 18 24 28 30 30 28 24 18 10]. The algorithm
devised should only use the length of vector v to achieve vector f.

MATLAB Code:

% Given vector v
v = 1:10;

% Initialize vector f
f = zeros(1, length(v));

% Loop to multiply each element of v by the reverse of its position


for i = 1:length(v)
f(i) = v(i) * (length(v) - (i - 1));
end

Console Window:

In Task 5, each element of vector v is multiplied by its reversed position in the vector (e.g., the first
element is multiplied by 10, the second by 9, and so on), resulting in vector f.

6|Page
EXPERIMENT NUMBER: 1

LAB TASK # 6
1. Give a MATLAB expression that use only a single matrix multiplication with Matrix B
(obtained in problem 5) to obtain:

a. The difference of columns 2 and 3 of Matrix B

b. The last row of B

c. A version of B with rows 2 and 3 swapped


Part: a, b & c
MATLAB Code:
B = [1 2 3 4 5 6 7;
9 7 5 3 1 -1 -3;
4 8 16 32 64 128 256];
% (Part: a)

% Matrix to perform column difference (2nd column - 3rd column)


column_diff_matrix = [0 1 -1 0 0 0 0]';
% Perform matrix multiplication to get the difference of columns 2 and 3
diff_cols_2_3 = B * column_diff_matrix;

% (Part: b)
last_row_matrix = [0 0 1];
% Perform matrix multiplication to get the last row
last_row = last_row_matrix * B;

7|Page
EXPERIMENT NUMBER: 1
% (Part: c)
% Permutation matrix to swap rows 2 and 3
swap_rows_matrix = [1 0 0;
0 0 1;
0 1 0];

% Perform matrix multiplication to swap rows 2 and 3


B_swapped = swap_rows_matrix * B;

Console Window:

LAB TASK # 7
Given a matrix A, such that:

A=[1 x 3 4 ; 4 5 p 7 ; 3 2 5 m]
Note: In matrix A:
x= Last digit of your Reg No. p= 2nd last digit of your Reg No.
m= Average of your Reg No.
Perform the following operations and show their results in MATLAB:
a) Extract the 3rd column of matrix A and store it in vector B.
b) Extract the 1st and 3rd columns of matrix A and store them in matrix C.
c) Add the 1st and 3rd rows of matrix A together and store the result in vector D.
d) Change the value in the 2nd row and 3rd column of A to +7 (instead of -7) and call the result AA
(do not destroy/change the original A matrix).
e) Create a matrix that contains rows 1 and 3 from A, the second row of AA, and the result of part
(c).

8|Page
EXPERIMENT NUMBER: 1
Solution

Reg# = 016
Therefore,
X=6;
P=1;
M= (0+1+6)/3 = 2.33

Part: a, b, c, d & e
MATLAB Code:
% Define matrix A with x, p, and m from Reg No. 016
Co
x = 6; % Last digit of Reg No.
n p = 1; % Second last digit of Reg No. so
l m = 1; % Average of Reg No. digits e

A = [1 x 3 4;
4 5 p 7;
3 2 5 m];

% a) Extract the 3rd column of matrix A and store it in vector B


B = A(:, 3);

% b) Extract the 1st and 3rd columns of matrix A and store them in matrix C
C = A(:, [1 3]);

% c) Add the 1st and 3rd rows of matrix A together and store the result in vector D
D = A(1, :) + A(3, :);

% d) Change the value in the 2nd row and 3rd column of A to +7 and call the result AA
AA = A; % Create a copy of A
AA(2, 3) = 7; % Update the 2nd row, 3rd column

% e) Create a matrix that contains rows 1 and 3 from A, the second row of AA, and the
result of part (c)
result_matrix = [A(1, :); AA(2, :); A(3, :); D];

Window:

 Vector B contains the 3rd column of matrix A.


 Matrix C contains the 1st and 3rd columns of matrix A.
 Vector D is the result of adding the 1st and 3rd rows of A.
 Matrix AA is a modified version of A, where the 2nd row and 3rd column value is updated to 7.
 result_matrix contains rows 1 and 3 from A, the updated second row from AA, and the sum of
the 1st and 3rd rows of A.

9|Page
EXPERIMENT NUMBER: 1

10 | P a g e

You might also like