-
Notifications
You must be signed in to change notification settings - Fork 1
/
heat_sim.m
100 lines (92 loc) · 2.15 KB
/
heat_sim.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
%% Clear
clear
close
%% Set model
% Calculation area
lx = 0.1;
ly = 0.1;
% Number of meshes
nx = 50;
ny = 50;
% Number of input
nu = 2;
% Mesh width
dx = lx/nx;
dy = ly/ny;
% Thermal diffusion coefficient
kappa = 13.9 * 10^-6;
% Set matrices
A = zeros(nx*ny);
B = zeros(nx*ny, nu);
for iy = 1:ny
for ix = 1:nx
id = ix + (iy-1)*nx;
iw = id - 1;
ie = id + 1;
is = id - nx;
in = id + nx;
% Set boundary condition (Neumann)
if(ix ~= 1)
A(id, iw) = 1/(dx^2);
A(id, id) = A(id, id) - A(id, iw);
end
if(ix ~= nx)
A(id, ie) = 1/(dx^2);
A(id, id) = A(id, id) - A(id, ie);
end
if(iy ~= 1)
A(id, is) = 1/(dy^2);
A(id, id) = A(id, id) - A(id, is);
end
if(iy ~= ny)
A(id, in) = 1/(dy^2);
A(id, id) = A(id, id) - A(id, in);
end
% Set input ports
if(ismember(iy, [1:5]))
if(ismember(ix, [6:20]))
B(id, 1) = 1;
elseif(ismember(ix, [31:45]))
B(id, 2) = 1;
end
end
end
end
A = kappa * A;
% Set matrices of servo system
I = eye(nx*ny);
As = [A zeros(nx*ny, 1);-(1/(nx*ny))*ones(1,nx*ny) 0];
Bs = [B;zeros(1, nu)];
Rs = [zeros(nx*ny, 1);1];
Zs = [Bs Rs];
%% Design stabilization controller
F = lqr(As, Bs, diag([0.01*ones(1, nx*ny) 1]), eye(nu));
%% Control simulation
% Initial temperature
T0 = 250;
% Reference temperature
Tr = 300;
% Initial value setting
x0 = T0 * ones(nx*ny, 1) + 1*randn(nx*ny, 1);
% Reference value setting
r = Tr;
% Initial value of servo system
xs0 = [x0;r-mean(x0)];
% Solve ODE
[t, xs] = ode45(@(t, x) heat_ode(x, As, Bs, Rs, F, r), [0:1/30:300], xs0);
% Pick temperature
x = xs(:, 1:nx*ny);
%% Visualization
for i = 1:10:length(t)
T = reshape(x(i,:), [nx, ny])';
imagesc(T);
title(['t = ' num2str(t(i))]);
caxis([200 450])
colorbar
colormap hot
ax = gca;
ax.FontSize = 18;
drawnow;
F(i) = getframe(gcf);
pause(0.01)
end