Logistics and Agenda
Midterm 1:
Do it at home part
Midterm write up and code is due Sunday October 4th
11:59pm
You may use AI freely to write code, debug, and test.
You are responsible for the code you
submit.
We expect you to understand what the code is doing.
You may not use AI to write the report.
- Report should be done by you and only one per group is needed
In class part
- on Monday October 5th, with computers but no AI and no Internet
connection
- we will give you modified assignment which would be similar
- it will require modification or reuse of prepared code
Integration problem statement
Suppose we are given function
our goal is to find
Not all function can be easily integrated analytically in the elementary
enough form.
So we must use numerical methods.
The Riemann sum
Recall the Riemann integral definition our goal is to find
where
is the number of points,
is the distance between points.
Function under integral
The Rectangle (box) method
Riemann rule is almost direct recipe for the rectangle left end
method
where
and
We just need to remember about round off errors so
should not be too small or equivalently
should not be to big.
Rectangle left end method
Trapezoidal method
where
and
Trapezoid method
Let’s compare implementation of trapezoidal method
- machine generated with prompt “use python and write integration by
trapezoid method, it should take a, b, and Np - number of points”
see file trapezoid_ai.py
# AI generated code
def trapezoidal_ai(func, a, b, Np):
"""
Numerical integration using the trapezoidal rule.
Parameters:
func: function to integrate
a: lower limit of integration
b: upper limit of integration
Np: number of points (intervals = Np - 1)
Returns:
Approximation of the integral
"""
# Calculate step size
h = (b - a) / (Np - 1)
# Evaluate function at endpoints
integral = 0.5 * (func(a) + func(b))
# Sum the interior points
for i in range(1, Np - 1):
x = a + i * h
integral += func(x)
# Multiply by step size
integral *= h
return integral
see file trapezoid_human.py
# human generated code
import numpy as np
def trapezoidal_human(func, a, b, Np):
"""Numerical integration using the trapezoidal rule."""
h = (b - a) / (Np - 1)
x = np.linspace(a, b, Np)
y = func(x)
y[0] = y[0] / 2
y[-1] = y[-1] / 2
return np.sum(y) * h
This code also runs faster by factor of
30
Simpson method: approximation by parabolas.
Main idea take two adjacent points
,
,
and
and approximate the
by parabola on this
interval. Then the integral on this interval is approximated as
where
and
Note that N must be in special form N=2k+1, i.e. odd.
Simpson method
Integration error estimate
Rectangle method
Trapezoidal method
Simpson method
Here
represents big O
notation for so called “order of approximation”.
means that
,
where
is a constant
Library method for numerical integration
use quad from scipy.integrate module
import scipy.integrate as integrate
def f(x): return x*x
int_estimate, error_estimate = integrate.quad(f, 0, 1)
In [2]: int_estimate
Out[2]: 0.33333333333333337
In [3]: error_estimate
Out[3]: 3.700743415417189e-15