Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 39 ยท Getting Started

Calculus Tools in Python

Set up the Python tools used in this track: define functions, plot with Matplotlib, estimate slopes numerically and check answers symbolically with SymPy.

You can learn calculus with pencil and paper, and you should practice that way. But computers are excellent for checking answers, drawing graphs and exploring patterns. This lesson sets up the tools used throughout the track. Everything runs right here in your browser.

Functions in Python

A mathematical function becomes a Python function. Use ** for powers.

Python Define and evaluate functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Numbers with NumPy

NumPy works on whole arrays at once, which is perfect for plotting and tables. np.linspace(a, b, n) makes n evenly spaced points from a to b.

Python Plot with NumPy and Matplotlib

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Numerical slopes

You met the secant slope already. A slightly better estimate is the symmetric difference (f(x + h) - f(x - h)) / (2h), which uses a point on each side.

Python A numerical slope function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Numerical slopes are approximate. Tiny h values (like 1e-12) make rounding errors worse, so about 1e-5 is a good compromise.

Exact answers with SymPy

SymPy does algebra with exact symbols. We will use it to check our hand calculations: limits, derivatives and integrals.

Python SymPy for checking work

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Use these tools to check, not to replace, your thinking. Work a problem by hand first, then ask SymPy whether you are right. When they disagree, find out why: that is where learning happens.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you write x cubed in Python?

    Python uses ** for powers; ^ means bitwise XOR.
  2. 2. What does np.linspace(0, 1, 5) produce?

    It gives 0, 0.25, 0.5, 0.75, 1.
  3. 3. Why is a very tiny h such as 1e-15 a bad choice for numerical slopes?

    Subtracting nearly equal floating-point numbers loses precision.
  4. 4. SymPy is best used for

    It manipulates expressions exactly rather than with decimals.

Summary

  • Write math functions as Python functions; powers use **.
  • NumPy and Matplotlib make tables and graphs.
  • Numerical slope: (f(x+h) - f(x-h)) / (2h) with h near 1e-5.
  • SymPy checks exact answers.

Next: The Idea of a Limit