-
Notifications
You must be signed in to change notification settings - Fork 0
/
dryer_ex.m
124 lines (94 loc) · 2.47 KB
/
dryer_ex.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
%% Dryer Example (System Identification Toolbox needed)
% Load the data - input is current, output is temperature-
load dryer2;
% Scale the data
u2 = u2 - mean(u2);
y2 = y2 - mean(y2);
% Parameters
Ts = 0.08;
x = -length(u2)+1:length(u2)-1;
% Cross-Correlation for delay
Ryu = xcorr(y2,u2);
figure(1)
stem(x,Ryu,'filled')
title('Cross-correlation between y and u')
xlabel('Time shift(samples)'), ylabel('Correlation')
figure(2)
stem(x,Ryu,'filled')
title('Cross-correlation between y and u')
xlabel('Time shift(samples)'), ylabel('Correlation')
xlim([0,10])
% Split the data to train and test
ue = u2(1:length(u2)/2);
ye = y2(1:length(y2)/2);
uv = u2(length(u2)/2+1:end);
yv = y2(length(u2)/2+1:end);
% Encapsulate splitted input and output respect to sampling rate
ze = iddata(ye,ue,Ts);
zv = iddata(yv,uv,Ts);
% Set names of parameters
ze.InputName = 'Current';
zv.InputName = 'Current';
ze.InputUnit = 'A';
zv.InputUnit = 'A';
ze.OutputName = 'Temperature';
zv.OutputName = 'Temperature';
ze.OutputUnit = '\circC';
zv.OutputUnit = '\circC';
ze.TimeUnit = 'sec';
zv.TimeUnit = 'sec';
% Plot the data
figure(3)
plot(ze)
% Step Response
figure(4)
step(impulseest(ze),2)
xlim([-1 2])
% Estimate time delay
nk = delayest(ze);
% Bode Diagram
Ge = spa(ze);
figure(5)
bode(Ge)
% Estimate Frequency Response
z = iddata(y2,u2,Ts);
[m,p,w] = bode(spa(z));
w = squeeze(w);
m = squeeze(m);
figure(6)
semilogx(w,20*log10(m))
title('Estimated Frequecny Response')
xlabel('Frequency(Hz)')
ylabel('Magnitude(dB)')
% Generate a matrix for ARX model (combinations)
NN = struc(1:5,1:5,nk);
% Loss function as a vector
V = arxstruc(ze,zv,NN);
% Select best combination
theStruc = selstruc(V);
% Estimate parameters of ARX model
Marx = arx(ze,theStruc);
present(Marx)
% Find polynomial roots
marxPoles = roots(Marx.a);
% Compare the original data and estimated
figure(7)
compare(zv,Marx)
% Compute residuals
figure(8)
resid(zv,Marx)
% Compare original and predicted data
yp = predict(Marx,zv,10);
figure(9)
plot(Ts*(1:length(ue)),[zv.OutputData,yp.OutputData])
title('Comparing system output to predicted output')
xlabel('Time(seconds)')
ylabel('Temperature(C)')
legend({'Actual data';'Predicted data'})
% Compute the prediction error
err = pe(Marx,zv);
figure(10)
h = bodeplot(spa(err,[],logspace(-2,2,200)));
showConfidence(h,3)
title('Power Spectrum for Temperature')
%% end.