
(defun integrate (f a b &optional (n 1000))
"Approximate the integral of f from a to b using the trapezoidal rule with n subdivisions."
(let* ((h (/ (- b a) n))
(sum (+ (funcall f a) (funcall f b))))
(dotimes (i (- n 1) (/ (* h sum) 2))
(setq sum (+ sum (funcall f (+ a (* i h))))))))
; Example:
(integrate #'sin 0 pi)