77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
from typing import Dict, Any
|
|
import sympy as sp
|
|
from Stochastisches_Modell import StochastischesModell
|
|
|
|
def iterative_ausgleichung(
|
|
A: sp.Matrix,
|
|
l: sp.Matrix,
|
|
modell: StochastischesModell,
|
|
max_iter: int = 100,
|
|
tol: float = 1e-3,
|
|
) -> Dict[str, Any]:
|
|
|
|
ergebnisse_iter = [] #Liste für Zwischenergebnisse
|
|
|
|
for it in range(max_iter):
|
|
Q_ll, P = modell.berechne_Qll_P() #Stochastisches Modell: Qll und P berechnen
|
|
|
|
N = A.T * P * A #Normalgleichungsmatrix N
|
|
Q_xx = N.inv() #Kofaktormatrix der Unbekannten Qxx
|
|
n = A.T * P * l #Absolutgliedvektor n
|
|
|
|
dx = N.LUsolve(n) #Zuschlagsvektor dx
|
|
|
|
v = l - A * dx #Residuenvektor v
|
|
|
|
Q_vv = modell.berechne_Qvv(A, P, Q_xx) #Kofaktormatrix der Verbesserungen Qvv
|
|
R = modell.berechne_R(Q_vv, P) #Redundanzmatrix R
|
|
r = modell.berechne_r(R) #Redundanzanteile als Vektor r
|
|
|
|
sigma_hat = modell.berechne_vks(v, P, r) #Varianzkomponentenschätzung durchführen
|
|
|
|
ergebnisse_iter.append({ #Zwischenergebnisse speichern in Liste
|
|
"iter": it + 1,
|
|
"Q_ll": Q_ll,
|
|
"P": P,
|
|
"N": N,
|
|
"Q_xx": Q_xx,
|
|
"dx": dx,
|
|
"v": v,
|
|
"Q_vv": Q_vv,
|
|
"R": R,
|
|
"r": r,
|
|
"sigma_hat": sigma_hat,
|
|
"sigma0_groups": dict(modell.sigma0_groups),
|
|
})
|
|
|
|
# --- Abbruchkriterium ---
|
|
if sigma_hat:
|
|
max_rel_change = 0.0
|
|
for g, new_val in sigma_hat.items():
|
|
old_val = modell.sigma0_groups.get(g, 1.0)
|
|
if old_val != 0:
|
|
rel = abs(new_val - old_val) / abs(old_val)
|
|
max_rel_change = max(max_rel_change, rel)
|
|
|
|
if max_rel_change < tol:
|
|
print(f"Konvergenz nach {it + 1} Iterationen erreicht (max. rel. Änderung = {max_rel_change:.2e}).")
|
|
modell.update_sigma0_von_vks(sigma_hat)
|
|
break
|
|
|
|
# Varianzfaktoren für nächste Iteration übernehmen
|
|
modell.update_sigma0_von_vks(sigma_hat)
|
|
|
|
return {
|
|
"dx": dx,
|
|
"v": v,
|
|
"Q_ll": Q_ll,
|
|
"P": P,
|
|
"N": N,
|
|
"Q_xx": Q_xx,
|
|
"Q_vv": Q_vv,
|
|
"R": R,
|
|
"r": r,
|
|
"sigma_hat": sigma_hat,
|
|
"sigma0_groups": dict(modell.sigma0_groups),
|
|
"history": ergebnisse_iter,
|
|
} |