ASSIGNMENT No.
06
Q-1. Create a function q mean.m that will calculate the mean of an array of
any user input numbers.
Syntax function mean_value = q_mean(numbers)
mean_value = sum(numbers) / length(numbers);
end
my_array = [1, 2, 3, 4, 5];
average = q_mean(my_array);
disp(average);
Result
EXPLANATION:
1. function mean_value = q_mean(numbers): This line defines the function
named q_mean. It takes an array called numbers as input and will return a
value called mean_value.
2. sum(numbers): This calculates the sum of all the elements in the numbers
array.
3. length(numbers): This finds the number of elements in the numbers array.
RISHABH SINGH 1
(24ME51D)
4. mean_value = ...: The calculated sum is divided by the number of elements
to get the mean, and this result is stored in the mean_value variable.
Q-2. The location of a point in a Cartesian plane can be expressed in either
the rectangular coordinates(x,y) or the polar coordinates (r,θ). The
relationships among these two sets of coordinate are given by the following
equations.
𝒙 = 𝒓 ∙ 𝐜𝐨𝐬𝜽
𝒚 = 𝒓 ∙ 𝐬𝐢𝐧𝜽
𝒓 = √𝒙𝟐 + 𝒚𝟐
𝒙
tanθ =
𝒚
ANSWER:
Understanding the Problem
We need to create two MATLAB functions:
rec2polar: Takes rectangular coordinates (x, y) as input and returns
polar coordinates (r, θ).
Syntax function [r, theta] = rec2polar(x, y)
% Convert rectangular coordinates (x, y) to polar
coordinates (r, theta).
% Inputs:
% x - The x-coordinate.
% y - The y-coordinate.
% Outputs:
% r - The radius.
% theta - The angle in degrees.
r = sqrt(x^2 + y^2);
theta_rad = atan2(y, x); % Use atan2 for correct
quadrant
theta=rad2deg(theta_rad);
%Convertradians to degrees
end
% Example: Rectangular to Polar
[r, theta] = rec2polar(3, 4);
disp(['r = ', num2str(r), ', theta = ', num2str(theta)]);
RISHABH SINGH 2
(24ME51D)
Result
polar2rect: Takes polar coordinates (r, θ) as input and returns
rectangular coordinates (x, y).
Syntax function [x, y] = polar2rect(r, theta)
% Convert polar coordinates (r, theta) to rectangular
coordinates (x, y).
% Inputs:
% r - The radius.
% theta - The angle in degrees.
% Outputs:
% x - The x-coordinate.
% y - The y-coordinate.
theta_rad = deg2rad(theta); % Convert degrees to radians
x = r * cos(theta_rad);
y = r * sin(theta_rad);
end
RISHABH SINGH 3
(24ME51D)
% Example: Polar to Rectangular
[x, y] = polar2rect(5, 53.13);
disp(['x = ', num2str(x), ', y = ', num2str(y)]);
Result
Explanation:
1. rec2polar:
r = sqrt(x^2 + y^2): Calculates the radius.
RISHABH SINGH 4
(24ME51D)
theta_rad = atan2(y, x): Calculates the angle in radians. atan2 is
important because it correctly determines the quadrant of the angle.
theta = rad2deg(theta_rad): Converts the angle from radians to degrees.
2. polar2rect:
theta_rad = deg2rad(theta): Converts the angle from degrees to radians.
x = r * cos(theta_rad): Calculates the x-coordinate.
y = r * sin(theta_rad): Calculates the y-coordinate.
Important Notes
atan2: It's crucial to use atan2(y, x) instead of atan(y/x) in rec2polar. atan
only returns angles in the range -90 to +90 degrees, while atan2 considers
the signs of both x and y to give you the correct angle in the full 360-
degree range.
Radians and Degrees: MATLAB's trigonometric functions (sin, cos, tan)
work with radians. We use rad2deg and deg2rad to convert between
radians and degrees as needed.
Q-3. Write a m-file that outputs a conversion table for Celsius and
Fahrenheit temperatures. The input of the function should be two numbers
Ti and Tf, specifying lower and upper range of the table in Celsius. The
output should be a two column matrix- the first column showing the
temperature in Celsius from Ti to Tf in the increments of 1 degree C and the
second column showing the corresponding temperature in Fahrenheit.
Create a column vector C from Ti to Tf with command C=[Ti:Tf]. Calculate
temperature in Fahrenheit using below mentioned formula and make the
final matrix with the command [C F].
𝟗
F= C + 32
𝟓
Understanding the Problem
We need to create a MATLAB function that generates a temperature conversion
table between Celsius and Fahrenheit. The function will:
RISHABH SINGH 5
(24ME51D)
1. Take two input values, Ti (initial temperature in Celsius) and Tf (final
temperature in Celsius).
2. Create a column vector C containing Celsius temperatures from Ti to Tf
with increments of 1 degree.
3. Calculate the corresponding Fahrenheit temperatures using the formula:
F = (9/5) * C + 32.
4. Create a two-column matrix where the first column is C (Celsius) and the
second column is F (Fahrenheit).
Syntax function temp_table = celsius_to_fahrenheit(Ti, Tf)
% Generate a Celsius to Fahrenheit conversion table.
% Inputs:
% Ti - Initial temperature in Celsius.
% Tf - Final temperature in Celsius.
% Output:
% temp_table - A two-column matrix [Celsius, Fahrenheit].
C = Ti:Tf; % Create a row vector of Celsius temperatures
C = C'; % Convert to a column vector
F = (9/5) * C + 32; % Calculate Fahrenheit temperatures
temp_table = [C, F]; % Create the final matrix
end
%Calling of the function
initial_temp = 0;
final_temp = 100;
conversion_table=celsius_to_fahrenheit(initial_temp,final_temp);
disp(conversion_table);
Result If you run the example code with initial_temp = 0 nad final_temp
= 10, the output will look something like this:
0 32.0000
1 33.8000
RISHABH SINGH 6
(24ME51D)
2 35.6000
3 37.4000
4 39.2000
5 41.0000
6 42.8000
7 44.6000
8 46.4000
9 48.2000
10 50.0000
Explanation:
1. function temp_table = celsius_to_fahrenheit(Ti, Tf): This line defines the
function named celsius_to_fahrenheit. It takes two input values, Ti and Tf,
and returns a matrix called temp_table.
2. C = Ti:Tf;: This creates a row vector C containing integers from Ti to Tf.
3. C = C';: The transpose operator ' converts the row vector C into a column
vector. This is important for creating the final matrix.
4. F = (9/5) * C + 32;: This calculates the Fahrenheit temperatures using the
given formula. Since C is a column vector, the calculation is performed
element-wise, resulting in a column vector F of the same size.
5. temp_table = [C, F];: This creates the final matrix temp_table by
combining the C and F column vectors side-by-side.
Q-4. Write a function that, given two positive integers N1 and N2, calculates
their greatest common divisor. The greatest common divisor can be
calculated easily by iteratively replacing the largest of the two numbers by
the difference of the two numbers until the smallest of the two numbers
reaches zero. When the smallest number becomes zero, the other gives the
greatest common divisor. You will need to use a while loop and an if-else
statement. The largest of two numbers can be obtained using the built-in
function MAX(A,B). The smallest of two numbers can be obtained using the
built- in function MIN(A,B).
RISHABH SINGH 7
(24ME51D)
Understanding the Problem
We need to write a MATLAB function that finds the Greatest Common Divisor
(GCD) of two positive integers, N1 and N2. Here's how the GCD is calculated:
Repeated Subtraction: We repeatedly subtract the smaller number from
the larger number until one of the numbers becomes zero.
GCD: The number that is not zero at the end is the GCD.
We'll use a while loop to repeat the subtraction process and an if-else statement
to determine which number to subtract. We'll also use max(A, B) and min(A, B)
to find the larger and smaller numbers.
Syntax function result = calculate_gcd(N1, N2)
% Calculate the greatest common divisor of N1 and N2.
% Inputs:
% N1 - The first positive integer.
% N2 - The second positive integer.
% Output:
% result - The greatest common divisor of N1 and N2.
while min(N1, N2) ~= 0 % Continue until the smaller number
is zero
if N1 > N2
N1 = N1 - N2; % Subtract N2 from N1 if N1 is larger
else
N2 = N2 - N1; % Subtract N1 from N2 if N2 is larger or equal
end
end
result = max(N1, N2); % The remaining non-zero
number is the GCD
end
% Calling of the function
num1 = 24;
num2 = 36;
gcd_value = calculate_gcd(num1, num2);
disp(['The GCD of ', num2str(num1), ' and ', num2str(num2), ' is:
', num2str(gcd_value)]);
RISHABH SINGH 8
(24ME51D)
Result
Explanation
1. function result = calculate_gcd(N1, N2): Defines the function named
calculate_gcd that takes N1 and N2 as inputs and returns the GCD as
result.
2. while min(N1, N2) ~= 0: This loop continues as long as the smaller of N1
and N2 is not zero.
3. if N1 > N2: If N1 is greater than N2, we subtract N2 from N1.
4. else: Otherwise (if N2 is greater than or equal to N1), we subtract N1 from
N2.
RISHABH SINGH 9
(24ME51D)
5. result = max(N1, N2): When the loop finishes, one of the numbers will be
zero. The other number will be the GCD. We use max to return the non-
zero number.
We call calculate_gcd(24, 36):
Initial: N1 = 24, N2 = 36
Loop 1: N1 < N2, so N2 = 36 - 24 = 12. N1 = 24, N2 = 12
Loop 2: N1 > N2, so N1 = 24 - 12 = 12. N1 = 12, N2 = 12
Loop 3: N1 = N2, so N2 = 12 - 12 = 0. N1 = 12, N2 = 0
Loop ends: min(12, 0) is 0.
Result: max(12, 0) is 12.
The GCD of 24 and 36 is 12.
RISHABH SINGH 10
(24ME51D)