On an island, the Saturday fish market sells mackerel by the kilogram. At a price of P dollars per kg:
- buyers want Q_d = a - b \cdot P kg (the demand line),
- boats offer Q_s = c + d \cdot P kg (the supply line).
Here a > 0, b > 0 and d > 0. The number c may be negative: then the boats offer nothing until the price reaches -c / d.
The market is in equilibrium at the price where the two lines cross, Q_d = Q_s; the quantity traded is the common value. Write equilibrium(a, b, c, d) that returns the pair (price, quantity).
A crossing only makes sense in this market if both numbers are at least 0. If the lines cross at a negative price (the boats bring more than buyers want even when fish is free) or at a negative quantity (the highest price any buyer pays is below the lowest price any boat accepts), return None. A crossing at a price of exactly 0, or a quantity of exactly 0, is still an equilibrium.
To see it, press Run with plot([a - b * p for p in ps], ps) and plot([c + d * p for p in ps], ps) for prices ps = list(range(0, 41)): the crossing is the equilibrium.
Examples
Input: a = 120, b = 3, c = -20, d = 4
Output: (20.0, 60.0)
Explanation: at 20 dollars buyers want 120 - 60 = 60 kg and boats offer -20 + 80 = 60 kg.
Input: a = 50, b = 2, c = -60, d = 1
Output: None
Explanation: the lines cross at a price of 36.67 dollars and a quantity of -23.3 kg:
buyers stop buying at 25 dollars, boats only sell from 60 dollars up.
Input: a = 40, b = 1, c = 40, d = 2
Output: (0.0, 40.0)
Constraints
- answers are compared with a tolerance of
1e-6
Goals
- Find the price at which the quantity demanded equals the quantity supplied
- Solve one linear equation by hand and turn it into a formula
- Recognise a market whose curves only meet at a negative price or quantity