Power System Lab Report
Power System Lab Report
Experiment No.: 02
Experiment Name:
a) Write a MATLAB program to find the average of best 3 CT marks.
b) Write a MATLAB program to show a matrix in ascending and
descending order without using ascend, descend or sort command.
Submitted By : Submitted To :
Objectives:
✓ To find best 3 of 4 CT marks and their average.
✓ To display a matrix in ascending and descending order without using built-in functions
like ascend, descend, or sort.
Theory: In MATLAB, selecting the best 3 CT marks from a set of data and calculating their
average involves logical comparisons and iterative processes. First, all the CT marks are stored in
an array, and a loop is used to manually find the largest CT mark in the array by comparing each
element. Once the largest CT mark is identified, it is stored in a separate array (to keep track of
the top CT marks) and removed from the original array to ensure the same value is not selected
again. This process is repeated three times to extract the top 3 CT marks from the array. After
obtaining the top 3 CT marks, their average is calculated by summing these CT marks and dividing
the result by 3, using the formula:
In MATLAB, to sort a matrix in ascending or descending order without using the built-in ascend,
descend, or sort functions, the first step is to convert the matrix into a 1D array. This is done
because sorting a 1D array is simpler than directly sorting a 2D matrix. After the matrix is flattened
into an array, a sorting algorithm such as Selection Sort or Bubble Sort is applied. These algorithms
work by comparing elements in the array and swapping them to place them in either ascending
(from smallest to largest) or descending (from largest to smallest) order. Once the array is sorted,
it is reshaped back into its original matrix form, restoring the matrix's structure with the sorted
values. This approach allows sorting a matrix manually, without using MATLAB's built-in sorting
functions.
Algorithm to Find CT Average: Algorithm to display matrix in ascending and
1. Take an array of numbers as input. descending order without using built-in
2. Create an empty array to store the top 3 functions:
numbers. 1. Input a 1D array.
3. Repeat 3 times: 2. Use nested for loops to compare each element
Find the largest number in the array. with the next.
Add it to the new array. 3. For ascending: Swap if the current element is
Remove it from the original array. greater than the next.
4. Calculate the average of the 3 numbers by 4. For descending: Swap if the current element is
summing them and dividing by 3. smaller than the next.
5. Display the top 3 numbers and their average. 5. Output the sorted array.
6. End.
6. End.
MATLAB Code: (Best 3 CT marks Average)
Output:
B= S (Sum of Best 3 values):
2001035 S=
2001036 49
2001037 50
2001038 56
E= 54
15 12 16 18 C (Rounded Avg):
14 16 18 16 C=
20 18 18 17 16
18 16 20 15 17
D (Sorted Data): 19
D= 18
18 16 15 12 Final Result (Roll + All 4 CT marks + Average)
18 16 16 14
20 18 18 17 Roll CT1 CT2 CT3 CT4 Avg
20 18 16 15 2001035 15 12 16 18 16
F (Best 3 values): 2001036 14 16 18 16 17
F= 2001037 20 18 18 17 19
18 16 15 2001038 18 16 20 15 18
18 16 16 >>
20 18 18
20 18 16
MATLAB Code: (Display a matrix in ascending and descending order without using built-in
functions)
1 clc
2 clear all 18 % Ascending order
3 A=[2 -5 7 6 4 -11 12] 19 for i = 1:n-1
4 n = length(A); 20 for j = 1:n-i
5 % Descending order 21 if A(j) > A(j+1)
6 for i = 1:n-1 22 temp = A(j);
7 for j = 1:n-i 23 A(j) = A(j+1);
8 if A(j) < A(j+1) 24 A(j+1) = temp;
9 temp = A(j); 25 end
10 A(j) = A(j+1); 26 end
11 A(j+1) = temp; 27 end
12 end 28 AscendingOrder = A;
13 end 29 disp('Ascending Order Without
14 end sort:');
15 DescendingOrder = A; 30 disp(AscendingOrder)
16 disp('Descending Order Without sort:');
17 disp(DescendingOrder)
Output:
A=
2 -5 7 6 4 -11 12
Descending Order Without sort:
12 7 6 4 2 -5 -11
Ascending Order Without sort:
-11 -5 2 4 6 7 12
>>