Formulas and physics
Equations written once and solved for whichever quantity is missing.
Physics homework: one formula, any unknown
Section titled “Physics homework: one formula, any unknown”The equations of motion are two equations in five quantities. Write them once and leave out whichever one you are asked for.
formula Motion(u, a, t):
v = u + a * t
s = u * t + a * t^2 / 2
Motion(u = 0 m/s, a = 9.81 m/s^2, t = 3 s)::v
Motion(u = 0 m/s, a = 9.81 m/s^2, t = 3 s)::s
Motion(s = 100 m, u = 0 m/s, t = 5 s)::a
Motion(s = 100 m, u = 0 m/s, t = 5 s)::a to ft/s^2 to 1 dp
The third call is a genuine back-solve: a is inside a * t^2 / 2, and the
units m and s come out as m/s^2 without being told.
Weigh the Earth
Section titled “Weigh the Earth”Constants are one import away, and they carry their units.
import "physics/constants"
earth radius = 6371 km
earth mass = g0 * earth radius^2 / G to 3 sf
earth mass to kg
1 g * c^2 to kWh to 0 dp
g0 is standard gravity, G the gravitational constant, and c the speed
of light, all from the CODATA 2022 module. The last line is the energy in a
gram of matter, in the unit your electricity bill uses.
A can of soup: solve geometry through a power
Section titled “A can of soup: solve geometry through a power”The volume of a cylinder involves the square of the radius. The solver knows the inverse of a square.
formula Can(radius, height):
volume = pi * radius^2 * height
Can(volume = 400 ml, height = 10 cm)::radius to cm to 2 dp
Can(radius = 3.3 cm, height = 10 cm)::volume to ml to 0 dp
400 ml and 10 cm are different units of different dimensions. smoot works
in base units and converts the answer at the end.
Build a box from a rectangle
Section titled “Build a box from a rectangle”A formula can contain another formula. The inner equations join the outer ones, so a back-solve reaches inside.
formula Sheet(w, h):
area = w * h
formula Crate(height):
base = Sheet(h = 60 cm)
volume = base::area * height
Crate(height = 40 cm, volume = 0.24 m^3)::base::w
Crate(height = 40 cm, base = Sheet(w = 1 m, h = 60 cm))::volume to l
The first call knows the volume and height and finds the base’s width, two
levels down. The second passes a ready-made Sheet in.