-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSTR.m
More file actions
83 lines (67 loc) · 2.05 KB
/
CSTR.m
File metadata and controls
83 lines (67 loc) · 2.05 KB
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
clear; clc;
% Symbols
syms x1 x2 u
x = [x1; x2];
% Parameters
R = 0.1; % control weight in running cost
Tf = 0.78; % final time
N = 1001; % time grid points
tGrid = linspace(0, Tf, N).';
% Dynamics f(x,u)
expTerm = exp(25*x1/(x1+2));
f1 = -2*(x1 + 0.25) + (x2 + 0.5)*expTerm - (x1 + 0.25)*u;
f2 = 0.5 - x2 - (x2 + 0.5)*expTerm;
f = [f1; f2];
% Running cost g(x, u)
g = x1^2 + x2^2 + R*u^2;
% Terminal cost Phi(x)
Phi = sym(0);
% Initial condition
x0 = [0.05; 0];
% Initial control guess U0
U0 = zeros(N, 1);
% Options
opts = struct();
opts.maxIters = 100;
opts.alpha = 1.0;
opts.beta = 0.5;
opts.c1 = 1e-4;
opts.tol = 1e-6;
opts.interp = 'linear';
opts.uLower = -2;
opts.uUpper = 2;
opts.odeOptions = odeset('RelTol',1e-7,'AbsTol',1e-9);
opts.verbose = true;
% Call solver
[sol, info] = optimalControlSolver(f, g, Phi, x, u, tGrid, x0, U0, opts);
%% Plot results
figure('Name','States','Color','w');
plot(sol.t, sol.X, 'LineWidth', 1.5); grid on;
xlabel('$t$', 'interpreter', 'latex');
ylabel('$x$', 'interpreter', 'latex');
title('States');
legend({'$x_1$','$x_2$'}, 'interpreter', 'latex');
figure('Name','Control Input','Color','w');
plot(sol.t, sol.U, 'LineWidth', 1.5); grid on;
xlabel('$t$', 'interpreter', 'latex');
ylabel('$u$', 'interpreter', 'latex');
title('Control Input');
figure('Name','Costates','Color','w');
plot(sol.t, sol.P, 'LineWidth', 1.5); grid on;
xlabel('$t$', 'interpreter', 'latex');
ylabel('$p$', 'interpreter', 'latex');
title('Costates');
legend({'$p_1$','$p_2$'}, 'interpreter', 'latex');
figure('Name','Optimization History','Color','w');
subplot(2,1,1); plot(1:numel(sol.J_hist), sol.J_hist, '-o', 'LineWidth', 1.5);
grid on;
xlabel('iteration');
ylabel('$J$', 'interpreter', 'latex');
title('Cost per iteration');
subplot(2,1,2);
plot(1:numel(sol.grad_norm_hist), sol.grad_norm_hist, '-o', 'LineWidth', 1.5);
grid on;
xlabel('iteration');
ylabel('$\|dH/du\|_F$', 'interpreter', 'latex');
title('Gradient norm per iteration');
fprintf('CSTR demo complete. Final cost J = %.6f, iterations = %d\n', sol.J, info.iters);