Week 2 · Slopes, change and the steepest way up
Read the words of the video
- Imagine riding a bike up a hill. Your friend asks how steep it is right now. You can't just say "I climbed 100 meters in 2 kilometers" because that's an average. They want the steepness at this exact spot. If you zoom in really close, the road looks like a straight line.
- This "slope at one spot" is called the derivative. It's super important in machine learning, guiding us to reduce mistakes, just like finding the fastest way downhill. We'll learn to calculate it, use it to make curvy functions appear straight near a point, and extend this idea to many inputs.
- To estimate square root 4.2 without a calculator, we use the known square root of 4, which is 2. The derivative of square root x at x equals 4 is one-fourth. Our estimate becomes 2 plus one-fourth times 0.2, giving us 2.05. This simple trick provides a very close answer.
- For exams, always normalise direction vectors and evaluate partial derivatives at the specific point. Remember, differentiable means continuous, but not the reverse. The gradient points uphill fastest! Master the derivative as the slope, linear approximation, and the gradient's direction. Keep practicing, you've got this!
The one-minute story
You are riding a bike up a hill. Your friend asks: "How steep is it right here?"
You cannot answer with "I climbed 100 metres in 2 kilometres", because that is an average over the whole ride. Your friend wants the steepness at this exact spot. If you zoom in on the road so closely that it looks like a straight line, the slope of that tiny straight piece is the answer.
That slope-at-one-spot is called the derivative. It is the single most useful idea in machine learning, because learning means "nudge the settings so the mistakes get smaller", and the derivative tells us which way is downhill.
This week has three jobs: understand the derivative; use it to replace a curvy function with a straight one close to a point (linear approximation); and then do it again when there are many inputs (partial derivatives, the gradient, and the directional derivative).
By the end of this week you can:
- find slopes with the standard derivative rules and spot where a function is continuous or differentiable;
- write the linear approximation (tangent line or tangent plane) of a function at a point;
- compute partial derivatives, the gradient and a directional derivative;
- find the best value of something by setting the derivative to zero (the fencing problem).
Words you need
- Function - a machine: put a number in, get a number out. turns 3 into 9.
- Slope - how much the output rises when the input goes up by 1 (for a straight line).
- Derivative - the slope of the curve at exactly one spot.
- Continuous - you can draw the graph without lifting your pencil.
- Differentiable - the graph is smooth enough at that spot to have one clear slope (no sharp corner).
- Partial derivative - the slope when you move only one input and freeze the others.
- Gradient - the vector that collects all the partial derivatives.
Idea 1: The derivative is the slope at a point
For a straight line the slope is everywhere. For a curve the slope changes from place to place. Pick a point and a nearby point . The slope of the little line joining them is . Shrink towards 0 and that settles to a number, the derivative:
Limit just means "what number are we creeping up on?" If you walk halfway to a wall, then half the remaining way, and so on, your distance to the wall is creeping up on 0 even though you never get there. We say the limit is 0.
The rules you actually need
| Function | Derivative |
|---|---|
| constant | |
| , | , |
| (product) | |
| (chain) |
The chain rule deserves a picture. If is "double it" and is "square it", then . Changing a little changes by 2 times as much, and that change then gets multiplied by . Rates multiply along the chain: .
Continuous and differentiable: two different promises
- Continuous at : the left limit, the right limit and the value are all the same number. No jump, no hole, no blow-up.
- Differentiable at : the slope from the left equals the slope from the right.
Differentiable continuous, but not the other way round. The graph of is continuous everywhere, yet at it has a sharp V-corner: slope on the left, on the right, so no single slope. Not differentiable at 0.
For a piecewise function, check two things at the joining point: do the values match (continuity), then do the slopes match (differentiability)? Papers love "how many points is this function discontinuous / non-differentiable?" Count each joint separately.
Idea 2: Linear approximation - replace the curve by its tangent
Zoom in far enough on any smooth curve and it looks like a straight line: the tangent line. So near we can pretend:
You know a fact exactly at one spot (the value ) and you know how fast things change there (the slope ). Then a good guess for a spot a little way off is: start value + slope distance moved. That is how you estimate that a car doing 60 km/h will be about 6 km further after 6 minutes, without solving the whole trip.
Question. Estimate using the linear approximation of at .
Solution.
- .
- , so .
- .
- .
The true value is , so the guess is off by less than 0.001. Not bad for arithmetic you can do in your head.
Idea 3: Many inputs - partial derivatives and the gradient
Real models have many inputs: a house price depends on area and bedrooms and age. So take , a surface over a flat floor.
A partial derivative asks: "if I walk only in the direction and keep frozen, what is the slope?" To compute it, treat the other variables as ordinary numbers.
Question. . Find and at .
Solution. Freeze and differentiate in : . Freeze and differentiate in : . At : , .
Put the partials into one vector and you get the gradient:
Stand on a hillside in thick fog. You cannot see the top, but you can feel the ground under your feet. The gradient is an arrow, drawn on the ground, that points in the direction the slope climbs fastest, and its length tells you how steep that is. Walk against the arrow and you go downhill fastest. That one sentence is the whole of gradient descent in week 7.
Linear approximation in two variables (the tangent plane)
The tangent line becomes a tangent plane:
In vector form that is , the same shape as the one-variable version with a dot product instead of a multiplication.
Question. Find the best linear approximation of around .
Solution.
- Value: .
- Partial derivatives: and, because enters the same way, . At both equal .
- Plug in: .
Always do the three steps in this order: value, partials, plug in. Most wrong answers come from forgetting to evaluate the partials at the point, or from mixing up and .
Idea 4: The directional derivative
The partials tell you the slope when you walk exactly east or exactly north. What if you walk north-east? Pick a unit vector for the direction (length 1, see week 1). The slope in that direction is
Since :
- it is biggest () when points along (): steepest climb;
- it is most negative when points against : steepest descent;
- it is zero when : walking along a level contour, neither up nor down.
Question. . Find the rate of change at in the direction of .
Solution.
- , so at : .
- Normalise the direction: , so .
- .
If you had skipped step 2 and used directly you would get , five times too big. The exam always offers that wrong number as an option.
Idea 5: Where is the best? Set the slope to zero
At the top of a hill or the bottom of a valley the ground is flat, so the slope is zero. To find best values, solve .
Question. A farmer has 120 m of fence and wants to enclose a rectangular field along a straight river. The river side needs no fence. What is the largest area?
Solution.
- Let the two sides that touch the river be each. The side parallel to the river is .
- Area .
- .
- Check it is a maximum: (the curve bends downwards).
- .
Same recipe every time: write the quantity in one variable, differentiate, set to zero, check the bend.
Your turn
Differentiate: (a) , (b) , (c) .
Need a hint?
Use the table: power rule for (a), chain rule for (b), product rule for (c).
Show the full solution
(a) . (b) Chain rule: outer , inner : . (c) Product rule: .
Approximate using the linear approximation of at .
Need a hint?
and .
Show the full solution
, so . Then . (The true value is .)
Find the linear approximation of at and use it to estimate .
Need a hint?
. , at .
Show the full solution
. At : . The true value is exactly here.
. At , in which direction does increase fastest, and how fast?
Need a hint?
.
Show the full solution
. Fastest increase is along , i.e. the unit vector , at rate .
A gardener has 200 m of fencing and builds a rectangular garden against a straight wall (no fence on the wall side). Find the maximum area.
Need a hint?
Same shape as the farmer problem, with 200 instead of 120.
Show the full solution
, , .
Exam radar
Of the 53 MLF Quiz 1 papers, the summaries mention linear approximation in 28, directional derivatives in 14, partial derivatives in 11, continuity and differentiability in 10 and limits in 7. That makes this the heaviest single topic in Quiz 1.
In the older End Term papers, "the linear approximation at , find " appears 21 times, and the fencing problem (with 120 m or 200 m) about 8 times.
Typical marks: 2 to 3 per question, mostly MCQ and one numeric (SA).
- Forgetting to normalise the direction vector in a directional derivative.
- Not evaluating partials at the point before plugging into .
- Saying "continuous, therefore differentiable". It is the other way round.
- For , remember the chain rule: the partial in is , not .
- The gradient points uphill. Descent goes the opposite way.
Quick quiz
A function is differentiable at a point. Which statement must be true?
- It is continuous at that point.
- Its graph has a sharp corner there.
- Its second derivative exists there.
- It is zero there.
For f(x,y) = x^2 y at (1,2), the gradient is...
- (2, 1)
- (4, 1)
- (1, 4)
- (2, 4)
The direction in which a function increases fastest at a point is...
- the direction of the second derivative
- perpendicular to the gradient
- the direction of the gradient
- the direction opposite to the gradient
Which of these are true? (select all) (select all that apply)
- The directional derivative uses a unit vector.
- The maximum directional derivative at a point equals the length of the gradient.
- The linear approximation of f at a is f(a) + f'(a) x.
- |x| is differentiable at 0.
Recap
- The derivative is the slope at one spot. Differentiable continuous, not the reverse.
- Linear approximation: ; two variables: . Order of work: value, partials, plug in.
- Gradient = vector of partials, points uphill. with . Max rate .
- Optimise by setting the derivative to zero and checking the bend.
- Derivative:
- Power: ; chain: ; product:
- Linear approximation:
- Two variables:
- Gradient: ; directional derivative ,
- Steepest rate: along
- MLT week 3 - K-means: grouping without labels: K-means rests on one calculus fact: the average is the number that makes squared distances smallest.
- MLT week 4 - MLE, Bayes, Gaussian mixtures and EM: Maximum likelihood means 'set the derivative to zero', which is MLF week 2 in action.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 38 real questions on this week All Quiz 1 papers