1
Instructor:
Welcome to BME 603/606�
Computational Methods for�Biological Modeling & Simulation
Assistant Professor of Biomedical Engineering�Affiliate, Data Science Institute�Affiliate, Biophysics Graduate Program�Affiliate, Center for Genomic Science Innovation�Affiliate, Wisconsin Institute for Translational Neuroengineering �University of Wisconsin-Madison
DHANANJAY ‘Jay’ BHASKAR
Course Website: https://bio-modeling-uw-madison-f26.github.io/
2
BME 603/606: Computational Methods for Biological Modeling & Simulation
Announcements / Reflection
3
BME 603/606: Computational Methods for Biological Modeling & Simulation
Interpolation Problem
Time (hr) Glucose (mg/dL)� 0 90� 2 115� 4 140� 6 100�
A patient's blood glucose is measured at discrete time points: t = 0, 2, 4, 6 hours
We want to estimate glucose at t = 3 hours.
Can we find a function, f(t), that passes exactly through the measured data?
4
BME 603/606: Computational Methods for Biological Modeling & Simulation
Polynomial interpolation
Given n+1 data points (x₀, y₀), (x₁, y₁), …, (xₙ, yₙ)
Find a polynomial p(x) of degree ≤ n�
such that: p(xᵢ) = yᵢ for all i = 0, 1, …, n
x
p(x)
p(xᵢ) = yᵢ exactly
5
BME 603/606: Computational Methods for Biological Modeling & Simulation
Polynomial interpolation
p(x) = c₀ · 1 + c₁ · x + c₂ · x² + … + cₙ · xⁿ
So, we want a function
Basis functions:
φ₀(x) = 1, φ₁(x) = x, φ₂(x) = x², …, φₙ(x) = xⁿ
Unknowns:
coefficients c₀, c₁, …, cₙ
with
That satisfies our data:
(x₀, y₀), (x₁, y₁), …, (xₙ, yₙ)
⟶ n+1 data points give us n+1 equations for n+1 unknowns
6
BME 603/606: Computational Methods for Biological Modeling & Simulation
The Vandermonde Matrix
Plugging each data point into p(xᵢ) = yᵢ gives the linear system Ac = y:
A is the Vandermonde matrix: rows are [1, xᵢ, xᵢ², …, xᵢⁿ]
Solve Ac = y using np.linalg.solve(A, y) to obtain coefficients c
7
BME 603/606: Computational Methods for Biological Modeling & Simulation
Python implementation
import numpy as np
import matplotlib.pyplot as plt
# Data: (time in hours, blood glucose in mg/dL)
t = np.array([0, 2, 4, 6], dtype=float)
y = np.array([90, 115, 140, 100], dtype=float)
n = len(t)
A = np.vander(t, N=n, increasing=True) # Build Vandermonde matrix: A[i,j] = t[i]^j
# Solve Ac = y
c = np.linalg.solve(A, y)
print("Coefficients:", c)
# Evaluate polynomial on a fine grid
t_fine = np.linspace(0, 6, 200)
p = sum(c[j] * t_fine**j for j in range(n)) # p(t)
plt.plot(t, y, 'ro', markersize=8, label='Data')
plt.plot(t_fine, p, 'b-', label='Interpolating polynomial')
plt.xlabel('Time (hr)'); plt.ylabel('Glucose (mg/dL)')
8
BME 603/606: Computational Methods for Biological Modeling & Simulation
Limitations of polynomial interpolation
Adding one new data point changes all n+1 coefficients c₀, c₁, …, cₙ
The entire system Ac = y must be rebuilt and re-solved from scratch.
This is expensive and inconvenient when data arrives sequentially:
e.g., a patient's glucose sensor sending readings every hour.
9
BME 603/606: Computational Methods for Biological Modeling & Simulation
Lagrange basis functions
Each basis function “remembers” one data point:
Key property:
Lⱼ(xᵢ) = 1 if i = j, and Lⱼ(xᵢ) = 0 if i ≠ j
The interpolating polynomial is simply: p(x) = y₀L₀(x) + y₁L₁(x) + … + yₙLₙ(x)
No matrix solve needed: coefficients are just the data values yⱼ
10
BME 603/606: Computational Methods for Biological Modeling & Simulation
Python implementation
def lagrange_interp(x_data, y_data, x):
n = len(x_data)
result = np.zeros_like(np.atleast_1d(x), dtype=float)
for j in range(n):
# Compute Lⱼ(x)
L_j = np.ones_like(result)
for i in range(n):
if i != j:
L_j *= (x - x_data[i]) / (x_data[j] - x_data[i])
result += y_data[j] * L_j # p(x)
return result
# Glucose example
t = np.array([0, 2, 4, 6], dtype=float)
y = np.array([90, 115, 140, 100], dtype=float)
# Estimate glucose at t = 3 hours
print(lagrange_interp(t, y, 3.0))
11
BME 603/606: Computational Methods for Biological Modeling & Simulation
Newton basis: online updates
A continuous glucose monitor sends a new reading every hour.
Can we update our interpolating polynomial without starting over?
Newton basis:
φ₀(x) = 1
φ₁(x) = (x − x₀)
φ₂(x) = (x − x₀)(x − x₁)
φₖ(x) = (x − x₀)(x − x₁) ··· (x − xₖ₋₁)
Each new point only adds ONE new term to p(x)
12
BME 603/606: Computational Methods for Biological Modeling & Simulation
Divided differences
Coefficients cₖ are called divided differences and computed recursively:
Each new data point adds only one new cₖ at the bottom of this table
�All other coefficients remain the same!
13
BME 603/606: Computational Methods for Biological Modeling & Simulation
Python implementation
def divided_differences(x, y):
"""Compute Newton divided difference coefficients."""
n = len(x)
c = y.copy().astype(float)
for k in range(1, n):
c[k:] = (c[k:] - c[k-1]) / (x[k:] - x[:n-k])
return c # c[0], c[1], ..., c[n-1]
def newton_interp(x_data, c, x):
result = c[-1]
for k in range(len(c)-2, -1, -1):
result = result * (x - x_data[k]) + c[k]
return result
t = np.array([0, 2, 4, 6], dtype=float)
y = np.array([90, 115, 140, 100], dtype=float)
c = divided_differences(t, y)
# New reading arrives at t=8, only recompute ONE new coefficient!
t2 = np.append(t, 8.0); y2 = np.append(y, 85.0)
c2 = divided_differences(t2, y2)
14
BME 603/606: Computational Methods for Biological Modeling & Simulation
When data is noisy – least squares
Real glucose sensors have measurement noise.��Interpolation would overfit: curve passes through every noisy point.
�Idea: compute best fit with many more data points
t (hr)
glucose
residuals
15
BME 603/606: Computational Methods for Biological Modeling & Simulation
When data is noisy – least squares
16
BME 603/606: Computational Methods for Biological Modeling & Simulation
Python implementation
rng = np.random.default_rng(42)
t = np.linspace(0, 8, 30) # 30 hourly readings
y = 90 + 20*t - 3*t**2 + rng.normal(0, 5, 30) # true trend + noise
# Design matrix for p(t) = c_0 + c_1*t + c_2*t^2
A = np.vstack([t**0, t**1, t**2]).T # shape (30, 3)
# --- Direct: normal equations (A^T A) c = A^T y ---
c_ne = np.linalg.solve(A.T @ A, A.T @ y)
# --- Or use np.linalg.lstsq (more numerically stable) ---
c, _, _, _ = np.linalg.lstsq(A, y, rcond=None) # same result
t_fine = np.linspace(0, 8, 200)
p = c[0] + c[1]*t_fine + c[2]*t_fine**2
plt.scatter(t, y, alpha=0.6, label='Noisy glucose readings')
plt.plot(t_fine, p, 'r-', lw=2.5, label='Least squares fit')
plt.xlabel('Time (hr)'); plt.ylabel('Glucose (mg/dL)')
plt.legend(); plt.show()
17
BME 603/606: Computational Methods for Biological Modeling & Simulation
In-Class Exercises
Please ask Colin to mark you for completion before end of class.