MATLAB Practice Set 2
MATLAB and Programming Environment
MPhil 2023
——————————————————————————————
——————————————————————————————
1. Distance between two points: Write a MATLAB program to find the
distance between two points (x1 , y1 ) and (x2 , y2 ). Use this program to
find the distance between the points (1, 0) and (5, 0).
2. Perimeter of an ellipse: The perimeter P of an ellipse with semi-axes
a and b is given approximately by
r
1 2
P = 2π (a + b2 )
2
Write a MATLAB program to find P for various inputs of a and b. Find
x2 y 2
the perimeter of the ellipse, + = 1.
4 9
3. Distance from a point in a plane: The distance d from a point
(x0 , y0 , z0 ) to a plane Ax + By + Cz + D = 0 is given by
|Ax0 + By0 + Cz0 + D|
d= √
A2 + B 2 + C 2
Write a MATLAB program to find d for various inputs of the parameters
A, B, C, x0 , y0 , z0 .
Find the perpendicular distance of origin to the plane x + y + z = 1.
4. Roots of Quadratic Equations: Write a program that will solve for
the roots of a quadratic equation.
Test the program using real input data from the following quadratic
equations.
x2 + 5x + 6 = 0 Roots are: −2, −3
x2 + 4x + 4 = 0 Roots are: −2, −2
x2 + 2x + 5 = 0 Roots are: −1 + 2i, −1 − 2i
1
5. Temperature Conversion: Celsius (C), Fahrenheit (F), Reaumur (R),
Kelvin (K) and Rankine (Ra) are five scales of measurement of temper-
ature. Each scale of temperature has an upper fixed point (boiling point
of water) and a lower fixed point (melting point of ice). The difference
between these fixed points is called Fundamental units, which is 100 for
Celsius, 180 for Fahrenheit, 80 for Reaumur, 100 for Kelvin and 180 for
Rankine. The relation between the scale is
C −0 F − 32 R−0 K − 273 Ra − 492
= = = =
100 180 80 100 180
(a) Design a MATLAB program that reads an input temperature in
degree Fahrenheit, converts it to an absolute temperature in Kelvin,
and write out the result. To do this
i. Input the following command to ask user to input the temper-
ature in Fahrenheit in screen:
temp_f = input(’Enter the temperature in degree Fahrenheit: ’);
ii. Convert to Kelvins using
temp_k = (5/9)*(temp_f - 32) + 273;
iii. Write out the result using
fprintf(’%6.2f degree Fahrenheit = %6.2f kelvins \n’,...
temp_f, temp_k);
(b) Write a function that outputs a conversion table for Celsius and
Fahrenheit temperatures for 1 degree increment in Celsius between
two temperatures ti and tf in celsius. For conversion table use
the command temp table = [C, F], where C = [ti : tf ]’ and F
= (9/5)*C + 32, and output will be named temp table.