Numerical Methods Discrete Mathematics Lab Report 1
Numerical Methods Discrete Mathematics Lab Report 1
Theory:
MATLAB (Matrix Laboratory) is a versatile high-level programming language and interactive environment
widely used for numerical computation, data analysis, and algorithm development. It offers a vast array of
built-in functions and specialized toolboxes tailored to different applications, such as signal processing, control
systems, machine learning, and image processing. One of MATLAB’s key strengths lies in its intuitive handling
of matrices and arrays, which simplifies complex mathematical operations and data manipulation. Its powerful
visualization tools also enable users to easily create detailed plots, graphs, and charts, making it a valuable
resource for engineers, scientists, and researchers.
In addition to its core functionalities, MATLAB supports integration with other programming languages like
C, C++, Python, and Java, allowing for greater flexibility and interoperability. It is widely adopted in both
academic and industrial settings for tasks ranging from research simulations to the development of real-world
systems and applications. Moreover, MATLAB’s extensive documentation, user community, and rich ecosys-
tem of resources make it accessible for users of all experience levels.
Problem Statements:
1. Performing basic array operation (addition, subtraction, multiplication and division)
3. Performing basic matrix operation (addition, subtraction, multiplication and division) using loops.
Software Used:
1. MATLAB (Version R2024a)
Problem 1
Solving Procedure:
1. The first and second arrays were entered.
2. Addition, subtraction, element-wise multiplication, and element-wise division of the arrays were per-
formed.
Code:
Performing basic array operation (addition, subtraction, multiplication and division)
1 A = input('Enter the first array: ');
2 B = input('Enter the second array: ');
3
4 Addition = A + B;
5 Subtraction = A - B;
6 Multiply = A .* B;
7 Division = A ./ B;
8
9 fprintf('Addition: ');
10 fprintf('%g ', Addition);
1
11 fprintf('\n');
12
13 fprintf('Subtraction: ');
14 fprintf('%g ', Subtraction);
15 fprintf('\n');
16
17 fprintf('Multiplication: ');
18 fprintf('%g ', Multiply);
19 fprintf('\n');
20
21 fprintf('Division: ');
22 fprintf('%g ', Division);
23 fprintf('\n');
Output:
1 Enter the first array: [1, 2, 3, 4, 5]
2 Enter the second array: [6, 7, 8, 9, 10]
3 Addition: 7 9 11 13 15
4 Subtraction: -5 -5 -5 -5 -5
5 Multiplication: 6 14 24 36 50
6 Division: 0.166667 0.285714 0.375 0.444444 0.5
Discussion:
The MATLAB code efficiently performs element-wise arithmetic operations—addition, subtraction, multipli-
cation, and division—on two user-input arrays. Using built-in operators and ‘fprintf‘, it outputs the results
neatly on the same line. Element-wise operators (‘.*‘ and ‘./‘) highlight MATLAB’s ability to handle vector-
ized operations, making the code both simple and effective for tasks like data analysis or simulation. Overall,
it provides a straightforward method for performing and displaying basic array computations.
Problem 2
Solving Procedure:
1. The first and second matrices were entered, and their dimensions were checked for compatibility.
2. Matrix addition, subtraction, and element-wise division were performed and displayed if the matrices
had the same dimensions.
3. Matrix multiplication was performed and displayed if the number of columns in the first matrix matched
the number of rows in the second matrix, otherwise an error was reported.
Code:
Performing basic matrix operation (addition, subtraction, multiplication and division)
1 A = input('Enter the first matrix: ');
2 B = input('Enter the second matrix: ');
3
4 [m1, n1] = size(A);
5 [m2, n2] = size(B);
6
7 if m1 ~= m2 || n1 ~= n2
8 fprintf('Error: Matrices must be of the same dimensions.\n');
9 else
10 Addition = A + B;
11 Subtraction = A - B;
2
12 Division = A ./ B;
13
14 fprintf('Matrix Addition:\n');
15 fprintf('%g ', Addition);
16 fprintf('\n');
17
18 fprintf('Matrix Subtraction:\n');
19 fprintf('%g ', Subtraction);
20 fprintf('\n');
21
22 fprintf('Element-wise Division:\n');
23 fprintf('%g ', Division);
24 fprintf('\n');
25 end
26
27 if n1 == m2
28 Multiplication = A * B;
29 fprintf('Matrix Multiplication:\n');
30 fprintf('%g ', Multiplication);
31 fprintf('\n');
32 else
33 fprintf('Error: Matrices must have compatible dimensions.\n');
34 end
Output:
1 Enter the first matrix: [1,2;4,5]
2 Enter the second matrix: [5,10;8,15]
3 Matrix Addition:
4 6 12
5 12 20
6 Matrix Subtraction:
7 -4 -8
8 -4 -10
9 Element-wise Division:
10 0.2000 0.2000
11 0.5333 0.3333
12 Matrix Multiplication:
13 17 34
14 38 76
Discussion:
The code prompts the user to enter two matrices and checks if they have the same dimensions for element-wise
operations. If the matrices are compatible, it performs and displays matrix addition, subtraction, and element-
wise division. Additionally, it checks if the matrices have compatible dimensions for multiplication; if so,
it performs and displays matrix multiplication. If the matrices do not meet the required conditions for these
operations, appropriate error messages are shown.
Problem 3
Solving Procedure:
1. Dimensions and elements for both matrices were entered.
2. Matrices were multiplied if the number of columns in the first matrix equaled the number of rows in the
second matrix; otherwise, it was reported that multiplication was not possible.
3
3. Element-wise division was performed if both matrices had the same dimensions; otherwise, it was re-
ported that division was not possible.
Code:
Performing basic matrix operation (addition, subtraction, multiplication and division) using loops
1 row1 = input('Enter the number of rows for the first matrix: ');
2 column1 = input('Enter the number of columns for the first matrix: ');
3
15 row2 = input('Enter the number of rows for the second matrix: ');
16 column2 = input('Enter the number of columns for the second matrix: ');
17
18 mat2 = zeros(row2, column2);
19
20 for i = 1:row2
21 for j = 1:column2
22 mat2(i, j) = input(sprintf('Enter the element at (%d, %d): ', i, j));
23 end
24 end
25
26 fprintf('The second entered matrix is:\n');
27 disp(mat2);
28
29 if column1 == row2
30 matrix_multiply = mati * mat2;
31 fprintf('The matrix multiplication of the two matrices is:\n');
32 disp(matrix_multiply);
33 else
34 fprintf('Matrix multiplication not possible.\n');
35 end
36
37 if row1 == row2 && column1 == column2
38 elementwise_divide = mati ./ mat2;
39 fprintf('The element-wise division is:\n');
40 disp(elementwise_divide);
41 else
42 fprintf('Element-wise division not possible.\n');
43 end
Output:
1 Enter the number of rows for the first matrix: 2
2 Enter the number of columns for the first matrix: 2
3 Enter the element at (1, 1): 1
4 Enter the element at (1, 2): 2
5 Enter the element at (2, 1): 3
6 Enter the element at (2, 2): 4
7 The first entered matrix is:
8 1 2
4
9 3 4
10
11 Enter the number of rows for the second matrix: 2
12 Enter the number of columns for the second matrix: 2
13 Enter the element at (1, 1): 3
14 Enter the element at (1, 2): 4
15 Enter the element at (2, 1): 5
16 Enter the element at (2, 2): 6
17 The second entered matrix is:
18 3 4
19 5 6
20 The matrix multiplication of the two matrices is:
21 7 10
22 15 22
23 The element-wise division is:
24 0.3333 0.6667
25 0.6000 0.6667
Discussion:
The code prompts the user to enter the dimensions and elements for two matrices, initializes them accordingly,
and then displays both matrices. It performs matrix multiplication if the number of columns in the first matrix
matches the number of rows in the second matrix, and displays the result. Similarly, it performs element-wise
division if both matrices have identical dimensions and displays the result. If either operation is not feasible
due to dimension mismatch, appropriate messages are shown indicating that the operations are not possible.
References:
1. MATLAB Documentation. ”Matrix Operations.” Accessed at: [https://www.mathworks.com/help/matlab
/matrices.html]