ML Study Hub

Home / MLF / Week 2

Week 2 · Slopes, change and the steepest way up

Official syllabus line: Calculus
About 70 minQuiz 1 material (see exam radar)BSCS2004 · Harish Guruprasad Ramaswamy, Arun Rajkumar, Prashanth LA
Before you start: Week 1 (vectors and dot products). School-level algebra: expanding brackets, powers and roots.
Watch first: the animated story of this weekAbout 1-2 minutes of real 3D animation with a friendly voice. Pause any time.
Read the words of the video
  1. 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.
  2. 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.
  3. 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.
  4. 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

Think of it like this

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

Words you need
  • Function - a machine: put a number in, get a number out. f(x)=x2f(x)=x^2 turns 3 into 9.
  • Slope - how much the output rises when the input goes up by 1 (for a straight line).
  • Derivative f′(x)f'(x) - 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 ∇f\nabla f - the vector that collects all the partial derivatives.

Idea 1: The derivative is the slope at a point

For a straight line y=mx+cy=mx+c the slope is mm everywhere. For a curve the slope changes from place to place. Pick a point x=ax=a and a nearby point x=a+hx=a+h. The slope of the little line joining them is f(a+h)−f(a)h\frac{f(a+h)-f(a)}{h}. Shrink hh towards 0 and that settles to a number, the derivative:

f′(a)=lim⁡h→0f(a+h)−f(a)hf'(a)=\lim_{h\to0}\frac{f(a+h)-f(a)}{h}
Think of it like this

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

FunctionDerivative
constant cc00
xnx^nn xn−1n\,x^{n-1}
exe^xexe^x
ln⁡x\ln x1/x1/x
sin⁡x\sin x, cos⁡x\cos xcos⁡x\cos x, −sin⁡x-\sin x
c f(x)c\,f(x)c f′(x)c\,f'(x)
f+gf+gf′+g′f'+g'
f⋅gf\cdot g (product)f′g+fg′f'g+fg'
f(g(x))f(g(x)) (chain)f′(g(x))⋅g′(x)f'(g(x))\cdot g'(x)

The chain rule deserves a picture. If gg is "double it" and ff is "square it", then f(g(x))=(2x)2f(g(x))=(2x)^2. Changing xx a little changes gg by 2 times as much, and that change then gets multiplied by f′f'. Rates multiply along the chain: ddx(2x)2=2(2x)⋅2=8x\frac{d}{dx}(2x)^2=2(2x)\cdot2=8x.

Continuous and differentiable: two different promises

The idea
  • Continuous at aa: the left limit, the right limit and the value f(a)f(a) are all the same number. No jump, no hole, no blow-up.
  • Differentiable at aa: the slope from the left equals the slope from the right.

Differentiable ⇒\Rightarrow continuous, but not the other way round. The graph of ∣x∣|x| is continuous everywhere, yet at x=0x=0 it has a sharp V-corner: slope −1-1 on the left, +1+1 on the right, so no single slope. Not differentiable at 0.

Watch out - real exam trap

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 x=ax=a we can pretend:

f(x)  ≈  L(x)=f(a)+f′(a) (x−a)f(x)\;\approx\;L(x)=f(a)+f'(a)\,(x-a)
Think of it like this

You know a fact exactly at one spot (the value f(a)f(a)) and you know how fast things change there (the slope f′(a)f'(a)). Then a good guess for a spot a little way off is: start value + slope ×\times 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.

Solved example: 1. Estimating a square root without a calculator

Question. Estimate 4.2\sqrt{4.2} using the linear approximation of f(x)=xf(x)=\sqrt x at a=4a=4.

Solution.

  1. f(4)=2f(4)=2.
  2. f′(x)=12xf'(x)=\frac1{2\sqrt x}, so f′(4)=14f'(4)=\frac14.
  3. L(x)=2+14(x−4)L(x)=2+\frac14(x-4).
  4. L(4.2)=2+14(0.2)=2.05L(4.2)=2+\frac14(0.2)=2.05.

The true value is 2.0494…2.0494\ldots, 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 f(x,y)f(x,y), a surface over a flat floor.

A partial derivative asks: "if I walk only in the xx direction and keep yy frozen, what is the slope?" To compute it, treat the other variables as ordinary numbers.

fx=∂f∂x,fy=∂f∂yf_x=\frac{\partial f}{\partial x},\qquad f_y=\frac{\partial f}{\partial y}
Solved example: 2. Partial derivatives

Question. f(x,y)=x2y+3y2f(x,y)=x^2y+3y^2. Find fxf_x and fyf_y at (1,2)(1,2).

Solution. Freeze yy and differentiate in xx: fx=2xyf_x=2xy. Freeze xx and differentiate in yy: fy=x2+6yf_y=x^2+6y. At (1,2)(1,2): fx=2⋅1⋅2=4f_x=2\cdot1\cdot2=4, fy=1+12=13f_y=1+12=13.

Put the partials into one vector and you get the gradient:

∇f=(∂f∂x,  ∂f∂y)\nabla f=\left(\frac{\partial f}{\partial x},\;\frac{\partial f}{\partial y}\right)
Think of it like this

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:

L(x,y)=f(a,b)+fx(a,b) (x−a)+fy(a,b) (y−b)L(x,y)=f(a,b)+f_x(a,b)\,(x-a)+f_y(a,b)\,(y-b)

In vector form that is L(x)=f(a)+∇f(a)⋅(x−a)L(\mathbf x)=f(\mathbf a)+\nabla f(\mathbf a)\cdot(\mathbf x-\mathbf a), the same shape as the one-variable version with a dot product instead of a multiplication.

Solved example: 3. The best linear approximation (a favourite exam question)

Question. Find the best linear approximation of f(x,y)=x+yf(x,y)=\sqrt{x+y} around (4,5)(4,5).

Solution.

  1. Value: f(4,5)=9=3f(4,5)=\sqrt9=3.
  2. Partial derivatives: fx=12x+yf_x=\dfrac{1}{2\sqrt{x+y}} and, because yy enters the same way, fy=12x+yf_y=\dfrac1{2\sqrt{x+y}}. At (4,5)(4,5) both equal 12⋅3=16\dfrac1{2\cdot3}=\dfrac16.
  3. Plug in: L(x,y)=3+16(x−4)+16(y−5)L(x,y)=3+\dfrac16(x-4)+\dfrac16(y-5).

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 (a,b)(a,b) and (x,y)(x,y).

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 u\mathbf u for the direction (length 1, see week 1). The slope in that direction is

Duf=∇f⋅uD_{\mathbf u}f=\nabla f\cdot\mathbf u

Since ∇f⋅u=∥∇f∥cos⁡θ\nabla f\cdot\mathbf u=\|\nabla f\|\cos\theta:

  • it is biggest (=∥∇f∥=\|\nabla f\|) when u\mathbf u points along ∇f\nabla f (θ=0\theta=0): steepest climb;
  • it is most negative when u\mathbf u points against ∇f\nabla f: steepest descent;
  • it is zero when u⊥∇f\mathbf u\perp\nabla f: walking along a level contour, neither up nor down.
Solved example: 4. Directional derivative

Question. f(x,y)=x2yf(x,y)=x^2y. Find the rate of change at (1,2)(1,2) in the direction of v=(3,4)\mathbf v=(3,4).

Solution.

  1. ∇f=(2xy,  x2)\nabla f=(2xy,\;x^2), so at (1,2)(1,2): ∇f=(4,1)\nabla f=(4,1).
  2. Normalise the direction: ∥v∥=5\|\mathbf v\|=5, so u=(0.6, 0.8)\mathbf u=(0.6,\,0.8).
  3. Duf=4(0.6)+1(0.8)=2.4+0.8=3.2D_{\mathbf u}f=4(0.6)+1(0.8)=2.4+0.8=\mathbf{3.2}.

If you had skipped step 2 and used (3,4)(3,4) directly you would get 12+4=1612+4=16, 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 f′(x)=0f'(x)=0.

Solved example: 5. The fencing problem

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.

  1. Let the two sides that touch the river be xx each. The side parallel to the river is 120−2x120-2x.
  2. Area A(x)=x(120−2x)=120x−2x2A(x)=x(120-2x)=120x-2x^2.
  3. A′(x)=120−4x=0⇒x=30A'(x)=120-4x=0\Rightarrow x=30.
  4. Check it is a maximum: A′′(x)=−4<0A''(x)=-4<0 (the curve bends downwards).
  5. A(30)=30×60=1800 m2A(30)=30\times60=\mathbf{1800\ m^2}.

Same recipe every time: write the quantity in one variable, differentiate, set to zero, check the bend.

Your turn

Your turn: 1. Derivatives

Differentiate: (a) x3−5x+2x^3-5x+2, (b) e3xe^{3x}, (c) xln⁡xx\ln x.

Need a hint?

Use the table: power rule for (a), chain rule for (b), product rule for (c).

Show the full solution

(a) 3x2−53x^2-5. (b) Chain rule: outer eue^{u}, inner u=3xu=3x: e3x⋅3=3e3xe^{3x}\cdot3=3e^{3x}. (c) Product rule: 1⋅ln⁡x+x⋅1x=ln⁡x+11\cdot\ln x+x\cdot\frac1x=\ln x+1.

Your turn: 2. Linear approximation

Approximate ln⁡(1.1)\ln(1.1) using the linear approximation of f(x)=ln⁡xf(x)=\ln x at a=1a=1.

Need a hint?

f(1)=0f(1)=0 and f′(x)=1/xf'(x)=1/x.

Show the full solution

f′(1)=1f'(1)=1, so L(x)=0+1⋅(x−1)=x−1L(x)=0+1\cdot(x-1)=x-1. Then L(1.1)=0.1L(1.1)=0.1. (The true value is 0.09530.0953.)

Your turn: 3. Tangent plane

Find the linear approximation of f(x,y)=x2+xyf(x,y)=x^2+xy at (1,1)(1,1) and use it to estimate f(1.1, 0.9)f(1.1,\,0.9).

Need a hint?

f(1,1)=2f(1,1)=2. fx=2x+y=3f_x=2x+y=3, fy=x=1f_y=x=1 at (1,1)(1,1).

Show the full solution

L(x,y)=2+3(x−1)+1(y−1)L(x,y)=2+3(x-1)+1(y-1). At (1.1,0.9)(1.1,0.9): 2+3(0.1)+1(−0.1)=2+0.3−0.1=2.22+3(0.1)+1(-0.1)=2+0.3-0.1=\mathbf{2.2}. The true value is 1.21+0.99=2.21.21+0.99=2.2 exactly here.

Your turn: 4. Which way is steepest?

f(x,y)=x2+3y2f(x,y)=x^2+3y^2. At (2,1)(2,1), in which direction does ff increase fastest, and how fast?

Need a hint?

∇f=(2x, 6y)\nabla f=(2x,\,6y).

Show the full solution

∇f(2,1)=(4,6)\nabla f(2,1)=(4,6). Fastest increase is along (4,6)(4,6), i.e. the unit vector 152(4,6)\frac1{\sqrt{52}}(4,6), at rate ∥∇f∥=16+36=52≈7.21\|\nabla f\|=\sqrt{16+36}=\sqrt{52}\approx7.21.

Your turn: 5. Maximise

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

A=x(200−2x)A=x(200-2x), A′=200−4x=0⇒x=50A'=200-4x=0\Rightarrow x=50, A=50×100=5000 m2A=50\times100=\mathbf{5000\ m^2}.

Exam radar

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 L=ax+by+cL=ax+by+c at (2,3)(2,3), find aa" 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).

Watch out - real exam trap
  • Forgetting to normalise the direction vector in a directional derivative.
  • Not evaluating partials at the point before plugging into LL.
  • Saying "continuous, therefore differentiable". It is the other way round.
  • For x+y\sqrt{x+y}, remember the chain rule: the partial in xx is 12x+y\frac1{2\sqrt{x+y}}, not 12x\frac1{2\sqrt x}.
  • The gradient points uphill. Descent goes the opposite way.

Quick quiz

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.
Differentiable implies continuous. A sharp corner is exactly what stops a function being differentiable. The other two options do not follow.

For f(x,y) = x^2 y at (1,2), the gradient is...

  • (2, 1)
  • (4, 1)
  • (1, 4)
  • (2, 4)
f_x = 2xy = 4 and f_y = x^2 = 1 at (1,2).

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
The gradient points uphill fastest. The opposite direction is steepest descent. Perpendicular to it is a level direction with rate zero.

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.
The tangent line is f(a) + f'(a)(x - a), not f(a) + f'(a)x. |x| has a corner at 0, so it is not differentiable there.

Recap

Remember this
  • The derivative is the slope at one spot. Differentiable ⇒\Rightarrow continuous, not the reverse.
  • Linear approximation: L(x)=f(a)+f′(a)(x−a)L(x)=f(a)+f'(a)(x-a); two variables: L=f(a,b)+fx(x−a)+fy(y−b)L=f(a,b)+f_x(x-a)+f_y(y-b). Order of work: value, partials, plug in.
  • Gradient ∇f\nabla f = vector of partials, points uphill. Duf=∇f⋅uD_{\mathbf u}f=\nabla f\cdot\mathbf u with ∥u∥=1\|\mathbf u\|=1. Max rate =∥∇f∥=\|\nabla f\|.
  • Optimise by setting the derivative to zero and checking the bend.
Formulas to keep
  • Derivative: f′(a)=lim⁡h→0f(a+h)−f(a)hf'(a)=\lim_{h\to0}\frac{f(a+h)-f(a)}{h}
  • Power: (xn)′=nxn−1(x^n)'=nx^{n-1}; chain: (f(g))′=f′(g)g′(f(g))'=f'(g)g'; product: (fg)′=f′g+fg′(fg)'=f'g+fg'
  • Linear approximation: L(x)=f(a)+f′(a)(x−a)L(x)=f(a)+f'(a)(x-a)
  • Two variables: L(x,y)=f(a,b)+fx(a,b)(x−a)+fy(a,b)(y−b)L(x,y)=f(a,b)+f_x(a,b)(x-a)+f_y(a,b)(y-b)
  • Gradient: ∇f=(fx,fy)\nabla f=(f_x,f_y); directional derivative Duf=∇f⋅uD_{\mathbf u}f=\nabla f\cdot\mathbf u, ∥u∥=1\|\mathbf u\|=1
  • Steepest rate: ∥∇f∥\|\nabla f\| along ∇f\nabla f
Connected weeks (learn them together)

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