Skip to main content
Contents
Calc
Dark Mode Prev Next Profile
\(\newcommand\DLGray{\color{Gray}}
\newcommand\DLO{\color{BurntOrange}}
\newcommand\DLRa{\color{WildStrawberry}}
\newcommand\DLGa{\color{Green}}
\newcommand\DLGb{\color{PineGreen}}
\newcommand\DLBa{\color{RoyalBlue}}
\newcommand\DLBb{\color{Cerulean}}
\newcommand\ds{\displaystyle}
\newcommand\ddx{\frac{d}{dx}}
\newcommand\os{\overset}
\newcommand\us{\underset}
\newcommand\ob{\overbrace}
\newcommand\obt{\overbracket}
\newcommand\ub{\underbrace}
\newcommand\ubt{\underbracket}
\newcommand\ul{\underline}
\newcommand\tikznode[3][]
{\tikz[remember picture,baseline=(#2.base)]
\node[minimum size=0pt,inner sep=0pt,#1](#2){#3};
}
\newcommand\del{\nabla}
\newcommand\R{\mathbb{R}}
\newcommand\C{\mathcal{C}}
\newcommand\N{\mathcal{N}}
\newcommand\eps{\varepsilon}
\renewcommand\epsilon{\varepsilon}
\renewcommand\subset{\subseteq}
\newcommand\norm[1]{\|{#1}\|}
\newcommand\matrixbrackets[4][1]{
\draw (#3,#2) -- (\fpeval{#3+0.2},#2);
\draw (#3,#1) -- (#3 ,#2);
\draw (#3,#1) -- (\fpeval{#3+0.2},#1);
\draw (#4,#2) -- (\fpeval{#4-0.2},#2);
\draw (#4,#1) -- (#4 ,#2);
\draw (#4,#1) -- (\fpeval{#4-0.2},#1);
}
\tikzstyle{circle node 0}=[fill=white, draw=black, shape=circle, inner sep=0pt]
\tikzstyle{circle node 2}=[fill=white, draw=black, shape=circle, inner sep=2pt]
\tikzstyle{hrect node}=[fill=white, draw=black, inner sep=2pt, outer sep=3pt]
\tikzstyle{vrect node}=[fill=white, draw=black, inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 0}=[inner sep=0pt, outer sep=0pt]
\tikzstyle{hidden node 2}=[fill=white, inner sep=2pt, outer sep=2pt]
\tikzstyle{rect node 1}=[fill=white, inner sep=2pt, outer sep=0pt]
\tikzstyle{rect node 2}=[fill=white, draw=black, inner sep=2pt, outer sep=0pt]
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
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 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 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:
Surface area:
\(A = 4\pi r^2\)
Volume:
\(V = \frac{4}{3}\pi r^3\)
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 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:
Surface area:
\(A = 2\pi r^2 + 2\pi r h\)
Volume:
\(V = \pi r^2 h\)
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 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:
\(P\) = principal (initial investment)
\(r\) = annual interest rate (as a decimal, e.g., 0.05 for 5%)
\(n\) = number of times interest is compounded per year
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 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:
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*}
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 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