Calculus Intro
Calculus is the mathematics of change. It answers two questions: how fast is something changing right now, and how much has built up over time? This course teaches both, with every idea checked in Python.
Two big ideas
Almost all of calculus grows from two questions that turn out to be opposites of each other:
| Idea | Question it answers | Everyday example | Main tool |
|---|---|---|---|
| Rate of change | How fast is this quantity changing at this instant? | The speedometer reading of a car | the derivative |
| Accumulation | How much has built up over an interval? | The distance on the trip counter | the integral |
If you know the speed at every moment, you can work out the total distance (accumulation). If you know the distance at every moment, you can work out the speed (rate of change). The Fundamental Theorem of Calculus says that these two operations undo each other. You will prove that to yourself with code in the definite integral lesson.
A first taste: speed from position
A ball dropped from a tower has fallen metres after seconds. What is its speed at exactly ? Speed is distance divided by time, but "at exactly one instant" means zero time and zero distance. Calculus gets around this by shrinking the time interval and watching where the average speed is heading:
def s(t):
return 4.9 * t**2 # metres fallen after t seconds
t = 2.0
for h in [1, 0.1, 0.01, 0.001, 0.0001]:
avg_speed = (s(t + h) - s(t)) / h
print(f"interval {h:<7} average speed = {avg_speed:.5f} m/s")interval 1 average speed = 24.50000 m/s
interval 0.1 average speed = 20.09000 m/s
interval 0.01 average speed = 19.64900 m/s
interval 0.001 average speed = 19.60490 m/s
interval 0.0001 average speed = 19.60049 m/s
The averages close in on 19.6 m/s. That limiting value is the derivative of at . The rule you will learn later, , gives straight away.
A first taste: distance from speed
Now go the other way. A car's speed is m/s. How far does it travel in the first 10 seconds? Chop the time into small steps, assume the speed is constant during each step, and add up for all of them:
def v(t):
return 3 * t # metres per second
for steps in [10, 100, 1000, 10000]:
dt = 10 / steps
distance = sum(v(i * dt) * dt for i in range(steps))
print(f"{steps:>6} steps: distance ≈ {distance:.3f} m") 10 steps: distance ≈ 135.000 m
100 steps: distance ≈ 148.500 m
1000 steps: distance ≈ 149.850 m
10000 steps: distance ≈ 149.985 m
The sums approach 150 m, the integral of from 0 to 10. More steps give a better answer, and the exact value is the limit as the step size goes to zero. This "chop, multiply, add" idea is called a Riemann sum.
Why programmers need calculus
You can write a lot of software without calculus, but it sits under many fields that programmers work in:
| Where | What calculus does there |
|---|---|
| Game physics | Position is the integral of velocity, velocity the integral of acceleration. Every physics step in a game loop is a tiny numerical integration (Game Physics). |
| Animation and UI | Easing curves such as ease-in-out are functions whose slope controls how smooth a motion feels. |
| Machine learning | Training means moving the model's weights against the gradient, a vector of derivatives, of the error (Machine Learning). |
| Neural networks and LLMs | Backpropagation is the chain rule applied through millions of layers and weights (Generative AI & LLMs). |
| Graphics | Bézier curves, surface normals and lighting integrals. |
| Finance and science | Continuous interest, growth and decay models, and differential equations. |
| Algorithms | Newton's method for roots, numerical integration and optimisation. |
Note: You will rarely differentiate by hand at work. Libraries do that for you. What you need is to understand what a derivative or integral means, so you can tell when the computer's answer makes sense.
How this course is organised
The course follows the chapter order of Debra Anne Ross's Master Math: Calculus, used here only as a guide to topic order. All explanations, examples and exercises are original.
- Functions: domain and range, function types, composition and inverses, transformations, exponentials and logs, trig and hyperbolic functions, polynomials, complex numbers and conic sections.
- The Derivative: limits, continuity, the differentiation rules, maxima and minima, optimisation, related rates, L'Hôpital's rule and Newton's method.
- The Integral: antiderivatives, Riemann sums, the Fundamental Theorem, area, volume, substitution, integration by parts and numerical integration.
- Series and Approximations: sequences, convergence and Taylor series, which is how computers evaluate
sinandexp. - Vectors, Matrices, Curves, Surfaces and Motion: the geometry needed for motion in 2D and 3D.
- Partial Derivatives: functions of several variables, gradients, Lagrange multipliers, Jacobians and Hessians.
- Vector Calculus: multiple integrals, line integrals, divergence and curl, and the big integral theorems.
- Introduction to Differential Equations: separable, linear and second-order equations, solved by hand and with Euler and RK4.
- Calculus in Computing: gradient descent, backpropagation, automatic differentiation, physics integration in game loops and easing curves.
- Practice: exercises, and a mini project that builds a tiny autodiff engine and trains a neuron.
Learning paths
You do not have to read everything in order. Pick the path that fits your goal (some of these courses are still planned; see the roadmap):
| Goal | Suggested path |
|---|---|
| School to university | Basic Math → Algebra → Pre-Calculus → this course → Calculus in Depth → Linear Algebra |
| Game developer | Basic Math → this course (chapters 1, 2, 5 and 8) → Linear Algebra → Game Math → Game Physics |
| AI / ML engineer | this course → Linear Algebra → Probability → Machine Learning → the Machine Learning track |
| Deep learning and LLMs | this course (chapter 6 and Calculus in Computing) → Linear Algebra → Generative AI & LLMs |
Tip: You should be comfortable with algebra (solving equations, working with exponents) and basic Python. If exponents or logarithms feel rusty, review Exponents and Logarithms first.
Exercises
Exercise 1: Rate or accumulation?
For each question, say whether it needs a derivative (rate of change) or an integral (accumulation): (a) how fast a download is progressing right now, in MB/s; (b) the total energy a server used over a day, given its power draw at every moment; (c) how steeply a hiking trail climbs at one point.
Answer
(a) derivative, (b) integral, (c) derivative.
Solution
- (a) "Right now" and a unit per second mean an instantaneous rate: a derivative of the amount downloaded.
- (b) Power is energy per second. Adding up over the whole day accumulates energy: an integral.
- (c) Steepness at one point is the slope of the height profile there: a derivative.
Exercise 2: Average speed by hand
A cyclist's position is metres. Work out the average speed between and , then guess the instantaneous speed at .
Answer
Average speed 12.2 m/s; the instantaneous speed at is 12 m/s.
Solution
Shrinking the interval (3.01 gives 12.02, 3.001 gives 12.002) points to 12 m/s. The derivative rule confirms it: .
Exercise 3: Shrink the interval in code
Change the falling-ball program so that it computes the speed of at with intervals 0.1, 0.01 and 0.001.
Answer
12.2, 12.02 and 12.002 m/s, approaching 12.
Solution
def s(t):
return 2 * t**2
for h in [0.1, 0.01, 0.001]:
print(h, round((s(3 + h) - s(3)) / h, 6))0.1 12.2
0.01 12.02
0.001 12.002
The extra term is exactly , because .
Exercise 4: Distance from a constant-plus-growing speed
A sprinter's speed is m/s. Estimate the distance covered in the first 4 seconds with a Riemann sum of 1000 steps, and check it against the area of the trapezoid under the graph.
Answer
About 15.992 m with 1000 left-endpoint steps; the exact distance is 16 m.
Solution
The graph of is a straight line from 2 (at ) to 6 (at ). The area under it is a trapezoid: .
steps = 1000
dt = 4 / steps
distance = sum((2 + i * dt) * dt for i in range(steps))
print(round(distance, 3))15.992
The left-endpoint sum is slightly too small because the speed is rising during each step. The error is .
Test yourself
Which calculus tool answers "how fast is the temperature rising at 3 pm?"
- The integral
- The derivative
- A conic section
- A complex number
In the falling-ball example, why do we shrink the interval instead of setting ?
- Python cannot multiply by zero
- With the average speed becomes , which has no value, so we look at the limit instead
- Smaller intervals make the ball fall faster
- Because h must always be an integer
Training a neural network moves the weights in the direction opposite to what?
- The gradient of the loss
- The integral of the inputs
- The largest weight
- The learning rate