0% found this document useful (0 votes)
22 views12 pages

Lab 2 1

Uploaded by

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

Lab 2 1

Uploaded by

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

EXPERIMENT NUMBER: 2

Department of Electrical and Computer Engineering

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:

function areas = calculate_areas(r, b, h, l, w)

% Function to calculate the area of a circle, triangle, and rectangle

% r = radius of the circle % b = base of the triangle% h = height of the triangle % l = length of the rectangle

% w = width 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;

% Output the areas

areas = [circle_area, triangle_area, rectangle_area];

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:

% Get dimensions from the user

radius = input('Enter the radius of the circle: ');

side = input('Enter the side length of the square: ');

length = input('Enter the length of the rectangle: ');

width = input('Enter the width of the rectangle: ');

base = input('Enter the base of the triangle: ');

height = input('Enter the height of the triangle: ');

square_area = side^2;
% Compute areas = length * width; triangle_area = 0.5 * base * height;
rectangle_area
circle_area
% Display=the
pi *results
radius^2;

fprintf('Area of Circle: %.2f\n', circle_area);

fprintf('Area of Square: %.2f\n', square_area);

fprintf('Area of Rectangle: %.2f\n', rectangle_area);


3 | Pfprintf('Area
age of Triangle: %.2f\n', triangle_area);
EXPERIMENT NUMBER: 2
Console Window

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:

% Get number of rows and columns from the user

rows = input('Enter the number of rows: ');

columns = input('Enter the number of columns: ');

% Get matrix entries from the user

matrix = zeros(rows, columns);

for i = 1:rows

for j = 1:columns

matrix(i, j) = input(sprintf('Enter value for element (%d,%d): ', i, j));

end
4|Page
end

% Display the matrix entered

disp('The entered matrix is:');


EXPERIMENT NUMBER: 2
Console Window

LAB TASK # 4
Write a simple MATLAB code for multiplication of two matrices using function?

MATLAB Code:
function result = multiply_matrices(A, B)

% Function to multiply two matrices A and B

if size(A, 2) == size(B, 1)

result = A * B;

else

error('Matrix dimensions do not match for multiplication.');

end

end

% Example usage

A = [1 2; 3 4];

B = [5 6; 7 8];

result = multiply_matrices(A, B);

disp('The result of matrix multiplication is:');

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

rows = input('Enter the number of rows: ');

cols = input('Enter the number of columns: ');

even_count = 0;

odd_count = 0;

% Get matrix entries from the user

matrix = zeros(rows, cols);

for i = 1:rows

for j = 1:cols

matrix(i, j) = input(sprintf('Enter value for element (%d,%d): ', i, j));

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

fprintf('This number is not prime\n');

else

is_prime = true;

for i = 2:sqrt(num)

if mod(num, i) == 0

is_prime = false;

break;

end

end

if is_prime

fprintf('This number is prime\n');

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:

% Define vector A with 9 elements


A = [1 2 3 4 5 6 7 8 9];

% Initialize the product


z = 1;

% Loop through the vector and calculate the product


for i = 1:length(A)
z = z * A(i);
end

% Subtract 12 from the product


c = z - 12;

% Display the result


fprintf('The final result after subtracting 12 is: %d\n', c);

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:

x = randi([0 1], 1, 10); % Random binary vector of length 10

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

if y(i) >= 52 && y(i) <= 336

z(i) = 0;

else

z(i) = 1;

end

elseif x(i) == 1

if y(i) >= 38 && y(i) <= 176

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

You might also like