Figure 4.4:

Comparison of filtering and smoothing updates for the batch reactor system. Second column shows absolute estimate error.

Code for Figure 4.4

Text of the GNU GPL.

main.m


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
% Comparison of different prior update strategies for a nonlinear system.
% Also compares EKF.
mpc = import_mpctools();

Nx = 2;
Ny = 1;
Nt = 10; % MHE horizon.

k1 = 0.16;
k2 = 0.0064;

Delta = 0.1;

% Define system model.
ode = @(x) [-2*k1*x(1)^2 + 2*k2*x(2); ...
            k1*x(1)^2 - k2*x(2)];
measurement = @(x) x(1) + x(2);

sim = mpc.getCasadiIntegrator(ode, Delta, [Nx], {'x'}, 'funcname', 'sim');
f = mpc.getCasadiFunc(ode, [Nx], {'x'}, 'funcname', 'f', ...
                      'rk4', true(), 'Delta', Delta);
h = mpc.getCasadiFunc(measurement, [Nx], {'x'}, 'funcname', 'h');

% Simulate the system.
Nsim = 200;
xsim = NaN(Nx, Nsim + 1);
xsim(:,1) = [3; 1];
ysim = NaN(Ny, Nsim + 1);
for i = 1:(Nsim + 1)
    ysim(:,i) = measurement(xsim(:,i));
    if i <= Nsim
        xsim(:,i + 1) = full(sim(xsim(:,i)));
    end
end

% Choose MHE objective function.
Q = diag([1e-4, 1e-2]);
R = diag([1e-2]);
P0 = diag([1, 1]);

Qinv = mpc.spdinv(Q);
Rinv = mpc.spdinv(R);
P0inv = mpc.spdinv(P0);

l = mpc.getCasadiFunc(@(w, v) w'*Qinv*w + v'*Rinv*v, [Nx, Ny], {'w', 'v'}, ...
                      'funcname', 'l');

par = struct('x0bar', [0.1; 4.5], 'Pinv', P0inv); % Initial prior.

% Choose other parameters.
lb = struct('x', zeros(Nx, 1));
N = struct('x', Nx, 'y', Ny, 't', Nt);
kwargs = struct('f', f, 'h', h, 'l', l, 'N', N, 'lb', lb, 'par', par, ...
                'y', ysim(:,1:(N.t + 1)), 'verbosity', 0);

% Simulate different prior updates.
updates = {'filtering', 'smoothing'};
mhes = struct();
xs = struct('actual', xsim);
ys = struct('actual', ysim);
for k = 1:length(updates)
    key = updates{k};
    fprintf('Simulating MHE with %s update.\n', key);
    mhe = mpc.nmhe('priorupdate', key, '**', kwargs);
    mhe.fix_truncated_x = true();

    xmhe = NaN(Nx, Nsim + 1);
    ymhe = NaN(Ny, Nsim + 1);
    for i = 0:Nsim
        if i <= Nt
            mhe.truncatehorizon(i);
        else
            mhe.newmeasurement(ysim(:,i + 1));
        end

        mhe.solve();
        if ~isequal(mhe.status, 'Solve_Succeeded')
            warning('Solver failed (status %s)!', mhe.status);
            break
        end
        mhe.saveestimate();
        xmhe(:,i + 1) = mhe.history(1).xhat(:,end);
        ymhe(:,i + 1) = mhe.history(1).yhat(:,end);
    end

    mhes.(key) = mhe;
    xs.(key) = xmhe;
    ys.(key) = ymhe;
end

% Use EKF. Assumes G = I.
fprintf('Simulating EKF\n');
xekf = NaN(Nx, Nsim + 1);
xekf(:,1) = par.x0bar;
P = P0;
yekf = NaN(Ny, Nsim + 1);
Afunc = f.factory('A', {'x'}, {'jac:f:x'});
Cfunc = h.factory('C', {'x'}, {'jac:h:x'});
for i = 1:(Nsim + 1)
    % Measurement.
    e = ysim(:,i) - full(h(xekf(:,i)));

    % Correction
    C = full(Cfunc(xekf(:,i)));
    L = (P*C')/(C*P*C' + R); % EKF gain
    P = P - L*C*P;

    xekf(:,i) = xekf(:,i) + L*e;
    yekf(:,i) = full(h(xekf(:,i)));

    % Advance forecast.
    if i <= Nsim
        A = full(Afunc(xekf(:,i)));
        xekf(:,i + 1) = full(f(xekf(:,i)));
        P = A*P*A' + Q;
    end
end
xs.ekf = xekf;
ys.ekf = yekf;

% Make a plot.
fprintf('Plotting.\n');
keys = {'actual', 'ekf', 'filtering', 'smoothing'};
colors = {'k', 'r', 'b', 'g'};
plotstyle = struct('t', (0:Nsim)*Delta, 'fig', figure(), 'marker', '', ...
                   'xnames', {{'p_a', 'p_b', 'p_{tot}'}}, ...
                   'unames', {{'log_{10}|e_a|', 'log_{10}|e_b|'}});
for i = 1:length(keys)
    k = keys{i};
    x = [xs.(k); ys.(k)];
    e = log10(abs(xs.(k) - xs.actual));
    mpc.mpcplot(x, e, 'legend', k, 'color', colors{i}, ...
                '**', plotstyle);
end