-
Notifications
You must be signed in to change notification settings - Fork 0
/
PID_PSO.m
134 lines (106 loc) · 2.37 KB
/
PID_PSO.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
clc;
clear;
close all;
ns=[1];
ds=[1 2 3];
G=tf(ns,ds);
Gf=feedback(G,1);
step(Gf);
hold on
% Param PSO
c=2; w=0.7; particles=20; iteration=50; Var=3;
% search space
a=0;
b=30;
% optimization steps
c_cf=0;
% Initialization
for m=1:particles
for n=1:Var
v(m,n)=0;
x(m,n)=a+rand*(b-a);
xp(m,n)=x(m,n);
end
%Model param
kp=x(m,1);
ki=x(m,2);
kd=x(m,3);
%Model de simulation
Gc=pid(kp,ki,kd);
Gcf=feedback(Gc*G,1);
y=step(Gcf);
% TIAE (fonction on=bjectif)
ff1=0;
sim1=size(y);
for m1=1:sim1
ff1=ff1+((abs(y(m1)-1))*m1);
end
ITAE(m)=ff1;
end
% find best value
[Best_performance,location]=min(ITAE);
fg=Best_performance;
xg(1)=x(location,1);
xg(2)=x(location,2);
xg(3)=x(location,3);
for i=1:iteration
for m=1:particles
for n=1:Var
v(m,n)=(w*v(m,n))+(c*rand*(xp(m,n)-x(m,n)))+(c*rand*(xg(n)-x(m,n)));
x(m,n)=x(m,n)+v(m,n);
end
% chek bound
for n=1:Var
if x(m,n)<a
x(m,n)=a;
end
if x(m,n)>b
x(m,n)=b;
end
end
%Model param
kp=x(m,1);
ki=x(m,2);
kd=x(m,3);
%Model de simulation
Gc=pid(kp,ki,kd);
Gcf=feedback(Gc*G,1);
y=step(Gcf);
% TIAE (fonction on=bjectif)
ff1=0;
sim1=size(y);
for m1=1:sim1
ff1=ff1+((abs(y(m1)-1))*m1);
end
ITAEp(m)=ff1;
% compare Local
if ITAEp(m)<ITAE(m)
ITAE(m)=ITAEp(m);
xp(m,1)=x(m,1);
xp(m,2)=x(m,2);
xp(m,3)=x(m,3);
end
end
[B_fg,location]=min(ITAE);
% compare global
if B_fg<fg
fg=B_fg;
xg(1)=xp(location,1);
xg(2)=xp(location,2);
xg(3)=xp(location,3);
end
c_cf=c_cf+1;
best_cf_ac(c_cf)=fg;
end
Min_ITAE=fg;
kp=xg(1)
ki=xg(2)
kd=xg(3)
Gc=pid(kp,ki,kd);
Gcf=feedback(Gc*G,1);
y=step(Gcf);
t_cf=1:c_cf;
figure
plot(t_cf,best_cf_ac,'r--','LineWidth',2),xlabel('iteration'),ylabel('cost function(TIAE)')
legend('ITAE for PSO-PID')
title('ITAE with each iteration')