PWR-HYBRID-3/reachability/reach_linear.m
Dane Sabo 02a675c152 reachability: first per-mode reach tube and barrier-cert attempt
Stand up reachability/ with a hand-rolled zonotope propagator for
linear closed-loop systems (reach_linear.m: axis-aligned box hull,
augmented-matrix integration for the disturbance convolution). Use it
in reach_operation.m to discharge the operation-mode safety obligation:
from a +/-0.1 K box on T_avg, under Q_sg in [85%, 100%]*P0, LQR keeps
T_c within 0.03 K of setpoint over 600 s. Safety band is +/-5 K, so
the obligation is satisfied with five orders of margin.

barrier_lyapunov.m attempts the analytic counterpart via a weighted
Lyapunov function. Sweeping the Qbar(T_c) weight, the best quadratic
barrier allows ~33 K deviation on the gamma level set — still outside
the 5 K safety band. This is a fundamental limitation of quadratic
barriers for anisotropic safety specs (thin-slab safe set in a
precursor-heavy state space). Documented in the file: next step for a
tight analytic certificate is SOS polynomial or polytopic barrier,
which need solvers we don't have locally yet.

reach_linear.m started out with a halfwidth-propagation bug (signed
A_step instead of |A_step|); fixed before commit after noticing the
reach envelope exactly matched the initial box on T_c.

Figures saved to docs/figures/. .mat result files gitignored — they
are regenerated in <1s.

Hacker-Split: first end-to-end per-mode reachability artifact.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 12:52:37 -04:00

94 lines
3.4 KiB
Matlab

function [T, R_lo, R_hi, center] = reach_linear(A_cl, B_w, x0_center, x0_halfwidth, w_lo, w_hi, tspan, dt)
% REACH_LINEAR Interval reach set for dx/dt = A_cl*x + B_w*w, w in [w_lo, w_hi].
%
% Hand-rolled zonotope propagator specialized to the case where:
% - The initial set is an axis-aligned box around x0_center with
% halfwidths x0_halfwidth.
% - The disturbance w is a scalar interval.
%
% The reach set at time t is:
% X(t) = {expm(A_cl*t) * x0 + integral_0^t expm(A_cl*(t-s))*B_w*w(s) ds :
% x0 in X0, w(s) in W}.
%
% Decompose:
% X(t) = expm(A_cl*t) * X0 (+) S_w(t),
% where S_w(t) is the reachable contribution of the disturbance.
% Both parts are zonotopes; we over-approximate each by a box at every
% time step via the matrix/integral absolute sum (interval hull).
%
% Inputs:
% A_cl - n x n closed-loop state matrix (Hurwitz assumed)
% B_w - n x 1 disturbance vector
% x0_center - n x 1 center of initial set
% x0_halfwidth - n x 1 halfwidths of initial set (>=0)
% w_lo, w_hi - scalar disturbance bounds
% tspan - [t0, tf]
% dt - time step for exporting the reach tube (s)
%
% Outputs:
% T - M x 1 time grid
% R_lo - n x M lower bounds of reach set
% R_hi - n x M upper bounds of reach set
% center- n x M center trajectory (nominal, w = w_mid)
n = size(A_cl, 1);
assert(size(A_cl,2) == n, 'A_cl must be square');
assert(numel(x0_center) == n && numel(x0_halfwidth) == n, 'x0 dim mismatch');
x0_center = x0_center(:);
x0_halfwidth = x0_halfwidth(:);
B_w = B_w(:);
w_mid = 0.5 * (w_lo + w_hi);
w_half = 0.5 * (w_hi - w_lo);
T = (tspan(1):dt:tspan(2)).';
M = numel(T);
R_lo = zeros(n, M);
R_hi = zeros(n, M);
center = zeros(n, M);
% Disturbance contribution:
% S_center(t) = int_0^t expm(A*(t-s))*B_w*w_mid ds (signed, centerline)
% S_half(t) = int_0^t |expm(A*(t-s))*B_w| * w_half ds (elementwise, halfwidth)
%
% The halfwidth uses elementwise |.| because under an axis-aligned
% box over-approximation, |linear-map| contributes absolute values.
% This is the interval-arithmetic analog of propagating a zonotope.
S_center = zeros(n, 1);
S_half = zeros(n, 1);
x_center_now = x0_center;
A_step = expm(A_cl * dt); % state transition per step
A_abs_step = abs(A_step); % for interval-halfwidth
% Integrated input gain per step:
% G_step = int_0^dt expm(A*s)*B_w ds
% Augmented-matrix trick: upper-right block of expm([A B_w; 0 0]*dt).
M_aug = expm([A_cl, B_w; zeros(1, n+1)] * dt);
G_step = M_aug(1:n, n+1); % n x 1
G_abs_step = abs(G_step);
halfwidth_now = x0_halfwidth;
% Initial state
center(:, 1) = x_center_now + S_center;
R_lo(:, 1) = center(:, 1) - halfwidth_now - S_half;
R_hi(:, 1) = center(:, 1) + halfwidth_now + S_half;
for k = 2:M
% State piece
x_center_now = A_step * x_center_now;
halfwidth_now = A_abs_step * halfwidth_now;
% Disturbance piece
S_center = A_step * S_center + G_step * w_mid;
S_half = A_abs_step * S_half + G_abs_step * w_half;
center(:, k) = x_center_now + S_center;
R_lo(:, k) = center(:, k) - halfwidth_now - S_half;
R_hi(:, k) = center(:, k) + halfwidth_now + S_half;
end
end