Skip to main content
Logo image

Exercises πŸ’» Coding Exercises

1. Programming with Scripts.

Answer the following questions to check your understanding of scripts.

2. Programming with Functions.

Answer the following questions to check your understanding of functions.

(a) Temperature Conversion.

Write a function named celsius_to_fahrenheit that converts a temperature from Celsius to Fahrenheit.
The conversion formula is: \(F = \frac{9}{5}C + 32\)
Inputs:
celsius (1x1) double β€” temperature in degrees Celsius
Outputs:
fahrenheit (1x1) double β€” temperature in degrees Fahrenheit
Test cases:
% Test 1: Freezing point of water
f1 = celsius_to_fahrenheit(0)
% Expected: f1 = 32

% Test 2: Boiling point of water
f2 = celsius_to_fahrenheit(100)
% Expected: f2 = 212

% Test 3: Room temperature
f3 = celsius_to_fahrenheit(20)
% Expected: f3 = 68

(b) Triangle Area.

Write a function named triangle_area that computes the area of a triangle given its base and height.
The area formula is: \(A = \frac{1}{2} \times \text{base} \times \text{height}\)
Inputs:
base (1x1) double β€” length of the triangle’s base
height (1x1) double β€” height of the triangle
Outputs:
area (1x1) double β€” area of the triangle
Test cases:
% Test 1: Simple triangle
a1 = triangle_area(6, 4)
% Expected: a1 = 12

% Test 2: Right triangle
a2 = triangle_area(5, 12)
% Expected: a2 = 30

% Test 3: Decimal values
a3 = triangle_area(7.5, 3.2)
% Expected: a3 = 12

(c) Sphere Properties.

Write a function named sphere_properties that computes both the surface area and volume of a sphere given its radius.
The formulas are:
Inputs:
radius (1x1) double β€” radius of the sphere
Outputs:
surface_area (1x1) double β€” surface area of the sphere
volume (1x1) double β€” volume of the sphere
Test cases:
% Test 1: Unit sphere
[A1, V1] = sphere_properties(1)
% Expected: A1 β‰ˆ 12.5664, V1 β‰ˆ 4.1888

% Test 2: Radius of 2
[A2, V2] = sphere_properties(2)
% Expected: A2 β‰ˆ 50.2655, V2 β‰ˆ 33.5103

% Test 3: Radius of 5
[A3, V3] = sphere_properties(5)
% Expected: A3 β‰ˆ 314.1593, V3 β‰ˆ 523.5988

(d) Cylinder Properties.

Write a function named cylinder_properties that computes the surface area, volume, and diagonal of a cylinder given its radius and height.
The formulas are:
Part of this problem is determining the correct formula for the diagonal of a cylinder. Hint: take out a piece of paper and sketch the cylinder, then draw the diagonal line from one edge of the base to the opposite edge of the top.
Inputs:
radius (1x1) double β€” radius of the cylinder
height (1x1) double β€” height of the cylinder
Outputs:
surface_area (1x1) double β€” total surface area of the cylinder
volume (1x1) double β€” volume of the cylinder
diagonal (1x1) double β€” diagonal length
Test cases:
% Test 1: Unit cylinder (radius = 1, height = 1)
[A1, V1, d1] = cylinder_properties(1, 1)
% Expected: A1 β‰ˆ 12.5664, V1 β‰ˆ 3.1416, d1 β‰ˆ 2.2361

% Test 2: Radius = 3, height = 5
[A2, V2, d2] = cylinder_properties(3, 5)
% Expected: A2 β‰ˆ 150.7964, V2 β‰ˆ 141.3717, d2 β‰ˆ 7.8102

% Test 3: Radius = 2.5, height = 8
[A3, V3, d3] = cylinder_properties(2.5, 8)
% Expected: A3 β‰ˆ 164.9336, V3 β‰ˆ 157.0796, d3 β‰ˆ 9.4340

(e) Compound Interest Calculator.

Write a function named compound_interest that calculates the future value of an investment with compound interest, and also returns the total interest earned and the effective annual rate.
The compound interest formula is: \(A = P\left(1 + \frac{r}{n}\right)^{nt}\)
where:
The effective annual rate (EAR) is: \(\text{EAR} = \left(1 + \frac{r}{n}\right)^n - 1\)
Inputs:
principal (1x1) double β€” initial investment amount
rate (1x1) double β€” annual interest rate (as decimal)
compounds_per_year (1x1) double β€” compounding frequency (e.g., 12 for monthly)
years (1x1) double β€” time period in years
Outputs:
final_amount (1x1) double β€” total amount after compound interest
interest_earned (1x1) double β€” total interest earned (final_amount - principal)
effective_rate (1x1) double β€” effective annual rate
Test cases:
% Test 1: $1000 at 5% annual rate, compounded monthly for 10 years
[final1, int1, effRate1] = compound_interest(1000, 0.05, 12, 10)
% Expected: final1 β‰ˆ 1647.01, int1 β‰ˆ 647.01, effRate1 β‰ˆ 0.0512

% Test 2: $5000 at 3% annual rate, compounded quarterly for 5 years
[final2, int2, effRate2] = compound_interest(5000, 0.03, 4, 5)
% Expected: final2 β‰ˆ 5805.92, int2 β‰ˆ 805.92, effRate2 β‰ˆ 0.03034

% Test 3: $10000 at 6% annual rate, compounded daily for 20 years
[final3, int3, effRate3] = compound_interest(10000, 0.06, 365, 20)
% Expected: final3 β‰ˆ 33197.90, int3 β‰ˆ 23197.90, effRate3 β‰ˆ 0.0618

(f) Triangle Perimeter and Area in 3D Space.

Write a function named triangle_3d that computes the perimeter and area of a triangle in 3D space given the coordinates of its three vertices. You may assume the three points do not lie on the same straight line. This ensures a valid triangle.
Hints:
  1. The distance formula between two points \((x_1, y_1, z_1)\) and \((x_2, y_2, z_2)\) is:
    \begin{equation*} d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2} \end{equation*}
  2. If the side lengths of the triangle are \(a\text{,}\) \(b\text{,}\) and \(c\text{,}\) then the area is given by Heron’s formula:
    \begin{equation*} A = \sqrt{s(s-a)(s-b)(s-c)} \end{equation*}
    where \(s = 0.5\left(a+b+c\right)\) (known as the semi-perimeter).
Inputs:
x1, y1, z1 (1x1) double β€” coordinates of first vertex
x2, y2, z2 (1x1) double β€” coordinates of second vertex
x3, y3, z3 (1x1) double β€” coordinates of third vertex
Outputs:
perimeter (1x1) double β€” perimeter of the triangle
area (1x1) double β€” area of the triangle
Note: Typically you would pass coordinates as vectors or matrices, but we will save that for later exercises.
Test cases:
% Test 1: Triangle in xy-plane (vertices at (0,0,0), (4,0,0), (0,3,0))
[p1, a1] = triangle_3d(0, 0, 0, 4, 0, 0, 0, 3, 0)
% Expected: p1 = 12, a1 = 6 (3-4-5 right triangle)

% Test 2: Equilateral triangle in space
[p2, a2] = triangle_3d(0, 0, 0, 1, 0, 0, 0.5, sqrt(3)/2, 0)
% Expected: p2 = 3, a2 β‰ˆ 0.4330

% Test 3: Triangle with vertices at (1,2,3), (4,5,6), (2,3,5)
[p3, a3] = triangle_3d(1, 2, 3, 4, 5, 6, 2, 3, 5)
% Expected: p3 β‰ˆ 10.6456, a3 β‰ˆ 2.1213