Q1.
% Generate a 5-by-5 matrix A containing random integer numbers from 0 to 100
A = randi ([0,100], 5, 5);
% Create A_even and A_odd matrices
A_even = zeros(size(A));
A_odd = zeros(size(A));
% Get the size of the matrix
[rows, cols] = size(A);
% % Loop through each element in matrix A
for i = 1:rows
for j = 1:cols
if mod(A(i, j), 2) == 0
A_even(i, j) = A(i, j); % If even, assign to A_even
else
A_odd(i, j) = A(i, j); % If odd, assign to A_odd
end
end
end
% Display the even and odd matrices
disp('Even Matrix A_even:');
disp(A_even);
disp('Odd Matrix A_odd:');
disp(A_odd);
% Verify that A = A_even + A_odd
A_check = A_even + A_odd;
Q2.
function [R, theta] = convert_to_polar(x, y)
% Convert a complex number from rectangular form (x + iy) to polar form (R ∠ θ)
% Input:
% x : real part
% y : imaginary part
% Output:
% R : magnitude (R)
% theta: angle in radians (θ)
% Calculate the magnitude R
R = sqrt(x^2 + y^2);
% Calculate the angle θ in radians using atan
theta = atan(y / x); % Use atan for angle calculation
% Display the results using disp
disp(['Magnitude (R): ', num2str(R)]);
disp(['Angle (θ in radians): ', num2str(theta)]);
end
Q3.
%Solving for subproblem a
A = [8, -2, 6, 7;
4, 8, 3, -8;
12, 10, 3, 0;
0, -1, 0, 1];
B = [8;
4;
0;
0];
C=inv(A)*B
x=C(1,1)
y=C(2,1)
z=C(3,1)
t=C(4,1)
%Solving for subproblem b
D= [2 1 0 0];
E= [6 -5 -3];
conv(D,E)
c=[12 -4 -11 -3 0 0]
if conv(D,E)==c
disp('the equation b is proved to be right')
else disp('the equation b is proved to be wrong')
end
%Solving for subproblem c
coefficients = [3, -6, 12, 0, 0];
solutions = roots(coefficients);
disp('The solutions for subproblem c are:');
disp(solutions);
Q4.
a=input('Insert a number: ');
b=round(a)
Q5.
% This script calculates the electricity bill based on consumption.
% Prompt the user to enter the number of units consumed
units = input('Enter the number of units consumed: ');
% Initialize bill amount
bill = 0;
% Calculate the bill based on the tariff structure
if units <= 100
bill = units * 0.10; % $0.10 per unit for the first 100 units
elseif units <= 200
bill = 100 * 0.10 + (units - 100) * 0.15; % $0.10 for the first 100 units, $0.15 for next 100
else
bill = 100 * 0.10 + 100 * 0.15 + (units - 200) * 0.20; % $0.10 for first 100, $0.15 for next 100, $0.20 for
above 200
end
% Display the total bill amount
disp(['Your total electricity bill is: $', num2str(bill)]);