Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Project: An Optics Toolkit in Python

Build a small, tested Python toolkit for reflection, refraction, thin lenses and diffraction gratings, then use it to model a two-lens telescope.

In this final project you will turn the formulas from the course into a small, reliable toolkit: a handful of functions that you can reuse for homework, lab reports and curiosity. You will also learn a habit that every scientist and engineer needs: testing code against answers you already know.

The code runs entirely in your browser. Nothing here connects to real optical hardware; the program is a calculator that applies the formulas you have learned.

Plan

Your toolkit will include:

  1. snell(n1, n2, theta1): refraction angle, or None for total internal reflection.
  2. critical_angle(n1, n2).
  3. thin_lens(u, f): image distance and magnification (also used for mirrors).
  4. grating_orders(lines_per_mm, wavelength_nm): all visible orders and their angles.
  5. Tests with assert that compare results with worked examples from earlier lessons.
flowchart LR
    A["Write functions"] --> B["Test against<br/>known examples"]
    B --> C["Use in a<br/>bigger model"]
    C --> D["Two-lens telescope"]

Build small tested pieces first, then combine them.

Part 1: the functions and their tests

Each assert below checks a number from a worked example in an earlier lesson. If a formula or sign were wrong, the program would stop with an error instead of silently giving a wrong answer.

Python Optics toolkit with tests

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.

If you change a formula (for example, delete the minus sign in m), run the program again and watch a test fail. That is the point of tests.

Part 2: a two-lens telescope

Now we use thin_lens twice. The image made by the first lens (the objective) becomes the object for the second lens (the eyepiece). The distance of that object from the second lens is the lens separation minus the first image distance.

Take an objective fโ‚ = 40 cm, an eyepiece fโ‚‚ = 5 cm, and a tree 20 m (2000 cm) away. If the first image is real and lands at distance vโ‚ behind the objective, then with lens separation s the eyepiece object distance is uโ‚‚ = s - vโ‚. If uโ‚‚ is slightly less than fโ‚‚, the final image is virtual, which is what you want to look at.

Python Two-lens system: model of a simple telescope

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.

Look at the output. When the separation is vโ‚ + fโ‚‚ (the second row), uโ‚‚ = fโ‚‚ and the final image is at infinity. That is the "normal adjustment" from the lesson on instruments, and the program prints it instead of dividing by zero. When uโ‚‚ < fโ‚‚ (the last two rows) the final image is virtual and closer; when uโ‚‚ > fโ‚‚ (the first row) the final image is real, which is no good for looking through an eyepiece. The angular magnification of the telescope in normal adjustment is fโ‚/fโ‚‚ = 8, which is a different quantity from the small lateral magnification m for a nearby tree. Do not mix them up.

Lateral magnification m = h'/h compares image size to object size. Angular magnification M compares the angle the image subtends at the eye to the angle the object would subtend. For distant objects M is the useful number.

Part 3: extend it

Choose one extension and try it yourself:

  • Add brewster(n1, n2) and a test that checks 53.1ยฐ for air to water (1.33).
  • Add a function that returns the lens power in dioptres from f in centimetres.
  • Write fringe_spacing(wavelength_nm, d_mm, D_m) and test it against the worked example from the double-slit lesson (650 nm, 0.25 mm, 3.0 m gives 7.8 mm).
  • Plot thin_lens image distance for u from 5 cm to 60 cm with f = 10 cm and mark where the image changes from virtual to real.
Python Starter for an extension: Brewster angle and fringe spacing

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.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why is it useful to test a function against a worked example you already know?

    Tests compare your code to known correct answers, so errors are caught immediately.
  2. 2. In a two-lens system, what is the object for the second lens?

    The first lens's image acts as the object for the second lens.
  3. 3. A telescope has an objective of f = 60 cm and an eyepiece of f = 3 cm in normal adjustment. What is the angular magnification?

    M = fo / fe = 60 / 3 = 20.

Summary

  • A toolkit of small, tested functions is more reliable than ad-hoc calculation.
  • Real-is-positive conventions let one function serve both mirrors and lenses.
  • Multi-lens systems use the image of one lens as the object of the next.
  • Angular magnification fโ‚’/fโ‚‘ and lateral magnification m are different quantities.

You have reached the end of the track. Return to the course overview to revisit any lesson.