|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
求牛人改
clear all;
close all;
a=newfis('fuzzpid');
a=addvar(a,'input','e',[-3,3]); %Parameter e
a=addmf(a,'input',1,'NB','zmf',[-3,-1]);
a=addmf(a,'input',1,'NM','trimf',[-3,-2,0]);
a=addmf(a,'input',1,'NS','trimf',[-3,-1,1]);
a=addmf(a,'input',1,'Z','trimf',[-2,0,2]);
a=addmf(a,'input',1,'PS','trimf',[-1,1,3]);
a=addmf(a,'input',1,'PM','trimf',[0,2,3]);
a=addmf(a,'input',1,'PB','smf',[1,3]);
a=addvar(a,'input','ec',[-3,3]); %Parameter ec
a=addmf(a,'input',2,'NB','zmf',[-3,-1]);
a=addmf(a,'input',2,'NM','trimf',[-3,-2,0]);
a=addmf(a,'input',2,'NS','trimf',[-3,-1,1]);
a=addmf(a,'input',2,'Z','trimf',[-2,0,2]);
a=addmf(a,'input',2,'PS','trimf',[-1,1,3]);
a=addmf(a,'input',2,'PM','trimf',[0,2,3]);
a=addmf(a,'input',2,'PB','smf',[1,3]);
a=addvar(a,'output','kp',[-0.3,0.3]); %Parameter kp
a=addmf(a,'output',1,'NB','zmf',[-0.3,-0.1]);
a=addmf(a,'output',1,'NM','trimf',[-0.3,-0.2,0]);
a=addmf(a,'output',1,'NS','trimf',[-0.3,-0.1,0.1]);
a=addmf(a,'output',1,'Z','trimf',[-0.2,0,0.2]);
a=addmf(a,'output',1,'PS','trimf',[-0.1,0.1,0.3]);
a=addmf(a,'output',1,'PM','trimf',[0,0.2,0.3]);
a=addmf(a,'output',1,'PB','smf',[0.1,0.3]);
a=addvar(a,'output','ki',[-0.06,0.06]); %Parameter ki
a=addmf(a,'output',2,'NB','zmf',[-0.06,-0.02]);
a=addmf(a,'output',2,'NM','trimf',[-0.06,-0.04,0]);
a=addmf(a,'output',2,'NS','trimf',[-0.06,-0.02,0.02]);
a=addmf(a,'output',2,'Z','trimf',[-0.04,0,0.04]);
a=addmf(a,'output',2,'PS','trimf',[-0.02,0.02,0.06]);
a=addmf(a,'output',2,'PM','trimf',[0,0.04,0.06]);
a=addmf(a,'output',2,'PB','smf',[0.02,0.06]);
a=addvar(a,'output','kd',[-3,3]); %Parameter kp
a=addmf(a,'output',3,'NB','zmf',[-3,-1]);
a=addmf(a,'output',3,'NM','trimf',[-3,-2,0]);
a=addmf(a,'output',3,'NS','trimf',[-3,-1,1]);
a=addmf(a,'output',3,'Z','trimf',[-2,0,2]);
a=addmf(a,'output',3,'PS','trimf',[-1,1,3]);
a=addmf(a,'output',3,'PM','trimf',[0,2,3]);
a=addmf(a,'output',3,'PB','smf',[1,3]);
rulelist=[1 1 7 1 5 1 1;
1 2 7 1 3 1 1;
1 3 6 2 1 1 1;
1 4 6 2 1 1 1;
1 5 5 3 1 1 1;
1 6 4 4 2 1 1;
1 7 4 4 5 1 1;
2 1 7 1 5 1 1;
2 2 7 1 3 1 1;
2 3 6 2 1 1 1;
2 4 5 3 2 1 1;
2 5 5 3 2 1 1;
2 6 4 4 3 1 1;
2 7 3 4 4 1 1;
3 1 6 1 4 1 1;
3 2 6 2 3 1 1;
3 3 6 3 2 1 1;
3 4 5 3 2 1 1;
3 5 4 4 3 1 1;
3 6 3 5 3 1 1;
3 7 3 5 4 1 1;
4 1 6 2 4 1 1;
4 2 6 2 3 1 1;
4 3 5 3 3 1 1;
4 4 4 4 3 1 1;
4 5 3 5 3 1 1;
4 6 2 6 3 1 1;
4 7 2 6 4 1 1;
5 1 5 2 4 1 1;
5 2 5 3 4 1 1;
5 3 4 4 4 1 1;
5 4 3 5 4 1 1;
5 5 3 5 4 1 1;
5 6 2 6 4 1 1;
5 7 2 7 4 1 1;
6 1 5 4 7 1 1;
6 2 4 4 5 1 1;
6 3 3 5 5 1 1;
6 4 2 5 5 1 1;
6 5 2 6 5 1 1;
6 6 2 7 5 1 1;
6 7 1 7 7 1 1;
7 1 4 4 7 1 1;
7 2 4 4 6 1 1;
7 3 2 5 6 1 1;
7 4 2 6 6 1 1;
7 5 2 6 5 1 1;
7 6 1 7 5 1 1;
7 7 1 7 7 1 1];
a=addrule(a,rulelist);
a=setfis(a,'DefuzzMethod','mom');
writefis(a,'fuzzpid');
a=readfis('fuzzpid');
[x,y]=meshgrid(-3:.2:3,-5:.2:5);
x1=x(:);y1=y(:);z1=evalfis([x1,y1],a);
kp=z1(:,1);ti=z1(:,2);td=z1(:,3);
plot3(x1,y1,kp);%why surf(x1,y1,kp)??
%model
ts=0.001;
sys=tf(5.235e005,[1,87.35,1.047e004,0]);
dsys=c2d(sys,ts,'tustin');
[num,den]=tfdata(dsys,'v');
error0=0;x0=0;
kp0=0.40;
kd0=1.0;
ki0=0.0;
%pid control
for k=1:1:500
rin(k)=1;
x(k)=x0+error(k);
k_pid=evalfis([e,de],a);
kp(k)=kp0+0.1*k_pid(1);
ki(k)=ki0+0.02*k_pid(2);
kd(k)=kd0+k_pid(3);
u(k)=kp(k)*error(k)+ki(k)*x(k)+kd(k)*(error(k)-error(k-1));
if k==300 % Adding disturbance(1.0v at time 0.3s)
u(k)=u(k)+1.0;
end
if u(k)>=10
u(k)=10;
end
if u(k)<=-10
u(k)=-10;
end
yout(k)=-den(2)*y_1-den(3)*y_2-den(4)*y_3+num(1)*u(k)+num(2)*u_1+num(3)*u_2+num(4)*u_3;
error(k)=rin(k)-yout(k);
e=error(k);
de=error(k)-error0;
kp0=kp(K);ki0=ki(k);kd0=kd(k);error0=de;x0=error0+error(k);
end
[ 本帖最后由 eight 于 2008-4-10 22:04 编辑 ] |
|