Lab 2 1
Lab 2 1
LAB REPORT:
2
Name Farid Uddin
Registration Number FA21-BEE-016/ATK
Class BEE-07
Subject Numerical Computation (NC)
Submitted To Dr. Babar Sattar
LAB TITLE
1|Page
EXPERIMENT NUMBER: 2
Introduction to Functions, Loops, Decision making and Plotting of Various
Signals in MATLAB.
LAB TASK # 1
Write a simple MATLAB code using function to calculate area of following?
• Circle
• Triangle
• Rectangle
MATLAB Code:
% r = radius of the circle % b = base of the triangle% h = height of the triangle % l = length of the rectangle
% Area of Circle
circle_area = pi * r^2;
% Area of Triangle
triangle_area = 0.5 * b * h;
% Area of Rectangle
rectangle_area = l * w;
end
Console Window
2|Page
EXPERIMENT NUMBER: 2
LAB TASK # 2
Write a simple MATLAB code in which dimension are taken from the user and compute area of a square, circle,
rectangle and triangle?
MATLAB Code:
square_area = side^2;
% Compute areas = length * width; triangle_area = 0.5 * base * height;
rectangle_area
circle_area
% Display=the
pi *results
radius^2;
LAB TASK # 3
Write a simple MATLAB code in which row and column and entities are entered by the user and show the result
(Multiplication)?
MATLAB Code:
for i = 1:rows
for j = 1:columns
end
4|Page
end
LAB TASK # 4
Write a simple MATLAB code for multiplication of two matrices using function?
MATLAB Code:
function result = multiply_matrices(A, B)
if size(A, 2) == size(B, 1)
result = A * B;
else
end
end
% Example usage
A = [1 2; 3 4];
B = [5 6; 7 8];
disp(result);
5|Page
EXPERIMENT NUMBER: 2
Console Window
LAB TASK # 5
Demonstrate the following:
a) Take an array matrix as input by user using input command and use Nested_for loop statement to find whether
this number is an Even number or an Odd number. Use display command/fprintf to display “The entry is Even” or
“The entry is Odd”, also count the number of Even and Odd entries in a given matrix entered by user.
b) Take a positive integer as input using input command and use if-else statement to find whether this number is a
prime number or not. Use display command to display “This number is prime” if the number is prime.
Part: a
MATLAB Code:
% Get matrix dimensions and entries from the user
even_count = 0;
odd_count = 0;
for i = 1:rows
for j = 1:cols
if mod(matrix(i, j), 2) == 0
6|Page
fprintf('The entry %d is Even\n', matrix(i, j));
even_count = even_count + 1;
EXPERIMENT NUMBER: 2
Console Window
Part: b
MATLAB Code:
num = input('Enter a positive integer: ');
if num <= 1
else
is_prime = true;
for i = 2:sqrt(num)
if mod(num, i) == 0
is_prime = false;
break;
end
end
if is_prime
else
7|Page
EXPERIMENT NUMBER: 2
LAB TASK # 6
Construct a vector A consisting of 9 elements. Use for loop to find the product of all the elements of the vector and
store the result in a variable 'z'. Subtract 12 from z and store the final result in "c".
MATLAB Code:
Console Window
8|Page
EXPERIMENT NUMBER: 2
LAB TASK # 7
Let “x” and “y” be two randomly generated row vectors. Prepare a new data array “z”, based on the following set of
rules:
If x = 0 and the value y lies within 52-336, then value of z will be 0. The value of z will be 1 if y is less than 52 or
more than 336.
If x = 1 and the value y lies within 38-176, then value of z will be 0. The value of z will be 1 if y is less than 38 or more
than 176.
MATLAB Code:
y = randi([1 400], 1, 10); % Random vector of length 10 within the range [1,
400]
z = zeros(size(x));
for i = 1:length(x)
if x(i) == 0
z(i) = 0;
else
z(i) = 1;
end
elseif x(i) == 1
z(i) = 0;
else
z(i) = 1;
end
end
end
disp('x:');
disp(x);
disp('y:');
9|Page
EXPERIMENT NUMBER: 2
Console Window
LAB TASK # 8
Use a while loop to print out the square of integers from 1 upto 20.
MATLAB Code:
n = 1; % Initialize the number
while n <= 20
fprintf('The square of %d is %d\n', n, n^2);
n = n + 1;
end
Console Window
10 | P a g e
EXPERIMENT NUMBER: 2
LAB TASK # 9
Write MATLAB programs to find the following with for loops.
MATLAB Code:
sumSquares = 0; % Task 9a
for n = 1:1000
sumSquares = sumSquares + n^2;
end
disp(['The sum of squares from 1 to 1000 is: ', num2str(sumSquares)]);
sumSeries = 0; % Task 9b:
sign = 1;
for d = 3:2:1003
sumSeries = sumSeries + sign * (1/d);
sign = -sign; % Alternate the sign for the next term
end
sumSeries = 1 + sumSeries;
disp(['The result of the series is: ', num2str(sumSeries)]);
Console Window
11 | P a g e
EXPERIMENT NUMBER: 2
12 | P a g e