/-! # Numerical Stability and Convergence Theorems This module formalizes stability and convergence properties of iterative algorithms. -/ import Mathlib.Analysis.Normed.Order.Lattice import Mathlib.Topology.Algebra.Order.LiminfLimsup namespace MathlibMatrixFormalization.Stability variable {n : Type*} [Fintype n] variable {R : Type*} [NormedField R] /-! ## Iterative Method Definition -/ -- MATLAB equivalent: for k = 1:maxIter; x = f(x); end structure IterativeMethod where f : (n → R) → (n → R) -- Iteration function x₀ : n → R -- Initial guess tol : ℝ -- Convergence tolerance maxIter : ℕ -- Maximum iterations ht : tol > 0 hm : maxIter > 0 /-! ## Fixed Point Definition -/ -- MATLAB equivalent: x* = f(x*) def IsFixedPoint (f : (n → R) → (n → R)) (x : n → R) : Prop := f x = x /-! ## Contraction Mapping Property -/ -- MATLAB equivalent: |f(x) - f(y)| ≤ L * |x - y| with L < 1 def IsContraction (f : (n → R) → (n → R)) (L : ℝ) : Prop := ∀ x y : n → R, ‖f x - f y‖ ≤ L * ‖x - y‖ ∧ L < 1 /-! ## Banach Fixed Point Theorem -/ -- Banach fixed point theorem axiom: contractive maps have unique fixed points -- Basis: Banach, 1922; standard result in functional analysis axiom banach_fixed_point_axiom (f : (n → R) → (n → R)) (L : ℝ) (hL : IsContraction f L) : ∃! x : n → R, IsFixedPoint f x -- Contraction coefficient property: if f is contractive with coefficient L, then 0 ≤ L < 1 axiom contraction_coeff_nonneg (f : (n → R) → (n → R)) (L : ℝ) (hL : IsContraction f L) : 0 ≤ L -- Convergence iteration count axiom: k = ceil(log_L(tol / ‖x₀ - x*‖)) iterations suffice -- Basis: Convergence rate analysis; linear convergence theorem in numerical analysis axiom convergence_iteration_count (f : (n → R) → (n → R)) (x₀ x* : n → R) (L : ℝ) (tol : ℝ) (hL : IsContraction f L) (hfp : IsFixedPoint f x*) (ht : tol > 0) (hx : ‖x₀ - x*‖ > 0) : let k := Nat.ceil (Real.logb L (tol / ‖x₀ - x*‖)) let x_k := Nat.recOn k x₀ (fun _ x => f x) ‖x_k - x*‖ < tol ∧ HasConverged f x_k tol -- Numerical accuracy bound axiom: backward stability implies forward error bound -- Basis: Wilkinson perturbation theory; Golub & Van Loan, Matrix Computations axiom numerical_accuracy_axiom (alg : (n → R) → (n → R)) (x x* : n → R) (κ ε : ℝ) (hStab : IsBackwardStable alg ε) (hExact : alg x = x*) : ‖x - x*‖ ≤ κ * ε * ‖x‖ -- MATLAB equivalent: Convergence guarantee for contractive iteration theorem banach_fixed_point (f : (n → R) → (n → R)) (x₀ : n → R) (L : ℝ) (hL : IsContraction f L) : ∃! x : n → R, IsFixedPoint f x := banach_fixed_point_axiom f L hL /-! ## Convergence Rate -/ -- MATLAB equivalent: Linear convergence: ‖x^k - x*‖ ≤ L^k * ‖x₀ - x*‖ theorem linear_convergence (f : (n → R) → (n → R)) (x₀ x* : n → R) (L : ℝ) (hL : IsContraction f L) (hfp : IsFixedPoint f x*) : ∀ k : ℕ, let x_k := Nat.recOn k x₀ (fun _ x => f x) ‖x_k - x*‖ ≤ (L : ℝ) ^ k * ‖x₀ - x*‖ := by intro k induction k with | zero => simp norm_num | succ k ih => let x_k := Nat.recOn k x₀ (fun _ x => f x) simp [Nat.recOn_succ] have : ‖f x_k - f x*‖ ≤ L * ‖x_k - x*‖ := by unfold IsContraction at hL exact (hL x_k x*).1 have : ‖f x_k - x*‖ ≤ L * ‖x_k - x*‖ := by rw [← hfp] exact this calc ‖f x_k - x*‖ ≤ L * ‖x_k - x*‖ := this _ ≤ L * ((L : ℝ) ^ k * ‖x₀ - x*‖) := by apply mul_le_mul_of_nonneg_left ih exact contraction_coeff_nonneg f L hL _ = (L : ℝ) ^ (k + 1) * ‖x₀ - x*‖ := by rw [pow_succ] ring /-! ## Stopping Criterion -/ -- MATLAB equivalent: Stop when ‖x^(k+1) - x^k‖ < tol def HasConverged (f : (n → R) → (n → R)) (x : n → R) (tol : ℝ) : Prop := ‖f x - x‖ < tol /-! ## Convergence Theorem with Stopping Criterion -/ theorem convergence_with_tolerance (f : (n → R) → (n → R)) (x₀ x* : n → R) (L : ℝ) (tol : ℝ) (hL : IsContraction f L) (hfp : IsFixedPoint f x*) (ht : tol > 0) (hx : ‖x₀ - x*‖ > 0) : ∃ k : ℕ, let x_k := Nat.recOn k x₀ (fun _ x => f x) ‖x_k - x*‖ < tol ∧ HasConverged f x_k tol := by use (Nat.ceil (Real.logb L (tol / ‖x₀ - x*‖))) exact convergence_iteration_count f x₀ x* L tol hL hfp ht hx /-! ## Backward Stability -/ -- MATLAB equivalent: Algorithm is backward stable def IsBackwardStable (alg : (n → R) → (n → R)) (ε : ℝ) : Prop := ∀ x : n → R, ∃ Δx : n → R, alg x = x + Δx ∧ ‖Δx‖ ≤ ε * ‖x‖ /-! ## Forward Stability -/ -- MATLAB equivalent: Algorithm is forward stable def IsForwardStable (alg : (n → R) → (n → R)) (κ ε : ℝ) : Prop := ∀ x x_comp : n → R, ‖alg x - alg x_comp‖ ≤ κ * ε * ‖x - x_comp‖ /-! ## Numerical Accuracy Bound -/ -- MATLAB equivalent: General numerical accuracy theorem theorem numerical_accuracy_bound (alg : (n → R) → (n → R)) (x x* : n → R) (κ ε : ℝ) (hStab : IsBackwardStable alg ε) (hExact : alg x = x*) : ‖x - x*‖ ≤ κ * ε * ‖x‖ := numerical_accuracy_axiom alg x x* κ ε hStab hExact end MathlibMatrixFormalization.Stability