NAMSSN UI Chapter
Python Lab • Experiments & ProjectsUse Python to run genuine mathematical experiments: random walks, simple ODE solvers, and mini-projects built from UI course material and Integration Bee problems.
By the time you are on this page, you should be comfortable with the ideas from Foundations and analysis support. Now you will combine them into small, meaningful projects.
All code here can be run in the live Python pad or in your own notebook. For anything long, we recommend using a notebook so you can save your work.
Project theme 1 • Random walks and simple probability
Random walks show up in probability, statistical mechanics, and many models of real phenomena. You can start with a one-dimensional walk and build intuition.
1.1 — Basic random walk on the line
import random
def random_walk(steps):
position = 0
path = [position]
for _ in range(steps):
step = random.choice([-1, 1])
position += step
path.append(position)
return path
for trial in range(3):
path = random_walk(20)
print("Trial", trial+1, ":", path)
- • Increase
stepsto 100 or 200. - • Print only every 5th value to keep lines shorter.
- • Think about the probability that the position returns to 0.
1.2 — Statistical questions you can ask
Once you generate many walks, you can start asking questions such as:
- • “On average, how far from 0 are we after 50 steps?”
- • “What is the maximum position reached in a walk?”
- • “How often do we hit a given level, such as +5?”
def max_distance_after(steps, trials):
total = 0
for _ in range(trials):
path = random_walk(steps)
max_pos = max(abs(x) for x in path)
total += max_pos
return total / trials
print("Average max distance after 50 steps:",
max_distance_after(50, trials=200))
Project theme 2 • Simple ODE experiments
Many ODEs cannot be solved exactly in closed form, but we can still approximate their solutions numerically. Python gives you a way to see these approximate solutions as numeric tables.
2.1 — Euler method for y' = y
def euler_step(f, t, y, h):
return y + h * f(t, y)
def f(t, y):
return y # y' = y
def solve_euler(f, y0, h, T):
t = 0.0
y = y0
print("t =", t, " y =", y)
while t < T:
y = euler_step(f, t, y, h)
t = t + h
print("t =", round(t, 3), " y ≈", y)
solve_euler(f, y0=1.0, h=0.1, T=1.0)
- • Try
h = 0.05andh = 0.01and watch the approximate values. - • Compare with the exact solution
e^tat the samet-values.
2.2 — A slightly different ODE: y' = -2y
def f2(t, y):
return -2*y
solve_euler(f2, y0=1.0, h=0.1, T=1.0)
The same Euler code now describes exponential decay instead of growth. You can plug in other right-hand sides from your differential equations course.
Project theme 3 • Connecting to the ODE–Integration Bee
Python is also useful as a quiet assistant while you prepare for the ODE–Integration Bee and related events. You can:
- • Check numeric values of integrals that have closed forms.
- • Explore parametrised families of integrals.
- • Test conjectured identities numerically before attempting proofs.
3.1 — Testing an integral numerically
import math
def approx_integral(f, a, b, n):
h = (b - a) / n
s = 0.0
for k in range(n):
x = a + (k + 0.5)*h
s += f(x)
return s * h
def f(x):
return math.exp(-x**2)
for n in [50, 100, 200, 500]:
print("n =", n, "approx =", approx_integral(f, 0, 1, n))
You can compare these values with references or with other numeric methods from textbooks.
Design patterns for mini-projects
Many interesting projects follow a few simple patterns. Here are three you can adapt.
Pattern A — “Table of values and a conjecture”
- Pick a formula or sequence from a UI course.
- Use Python to generate many values.
- Look for a pattern in the numbers and write a clean conjecture.
- Try to prove or disprove it mathematically.
Pattern B — “Comparing two methods”
- Choose a quantity that can be computed in two ways (for example: two approximations to an integral).
- Write Python functions for each method.
- Compare their outputs for different parameter choices.
- Summarise which method is more accurate or efficient in your experiments.
Pattern C — “Randomness and expectation”
- Define a random experiment (random walk, coin tosses, simple game, etc.).
- Simulate it many times and record results.
- Compute averages and frequencies using Python.
- Compare with theoretical probabilities when you know them.
For tutors, lecturers, and the press team
You can encourage projects like these as part of:
- • seminar talks during NAMSSN events,
- • short project components in analysis or probability courses,
- • writeups in the Student Bulletin.
A finished project can contain: a clear question, short Python code, a table or summary of the results, and a short discussion of what the student learned. This approach builds both mathematical and computational maturity.
Navigation
- • Back to Python Lab main page.
- • Review basics in Foundations.
- • Deepen analysis skills with Sequences, series, integrals.