close all
ts=0.001
J=1/147
q=22/147
sys=tf(1,[J,q,0])
dsys=c2d(sys,ts,'z')
[num,den]=tfdata(dsys,'v')
r_1=0r_2=0
u_1=0u_2=0
y_1=0y_2=0
error_1=0
ei=0
for k=1:1:1000
time(k)=k*ts
S=1
if S==1 %Step Signal
rin(k)=1.0
elseif S==2 %Sine Signal
rin(k)=1.0*sin(1*2*pi*k*ts)
end
yout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2
error(k)=rin(k)-yout(k)
derror(k)=(error(k)-error_1)/ts
ap=22bp=8.0cp=0.8
kp(k)=ap+bp*(1-sech(cp*error(k)))
ad=0.5bd=2.5cd=6.5dd=0.30
kd(k)=ad+bd/(1+cd*exp(dd*error(k)))
ai=1ci=1
ki(k)=ai*sech(ci*error(k))
ei=ei+error(k)*ts
u(k)=kp(k)*error(k)+kd(k)*derror(k)+ki(k)*ei
%Update Parameters
r_2=r_1r_1=rin(k)
u_2=u_1u_1=u(k)
y_2=y_1y_1=yout(k)
error_1=error(k)
end
figure(1)
plot(time,rin,'k',time,yout,'k')
xlabel('time(s)')ylabel('rin,yout')
figure(2)
plot(time,rin-yout,'k')ylabel('error')
xlabel('time(s)')ylabel('error')
figure(3)
plot(time,derror,'k')
xlabel('time(s)')ylabel('derror')
M=1
if M==1
figure(4)
subplot(311)
plot(error,kp,'k')xlabel('error')ylabel('kp')
subplot(312)
plot(error,kd,'k')xlabel('error')ylabel('kd')
ad+bd/(1+cd)
subplot(313)
plot(error,ki,'k')xlabel('error')ylabel('ki')
elseif M==2
figure(5)
subplot(311)
plot(time,kp,'k')xlabel('time(s)')ylabel('kp')
subplot(312)
plot(time,kd,'k')xlabel('time(s)')ylabel('kd')
subplot(313)
plot(time,ki,'k')xlabel('time(s)')ylabel('ki')
end
-----data_seperate.m-----------------------------------------------------------%%数据初始化
data='D:\work\数据离散化代码\discretization_data.xls'
k=4
%%数据读入
[data,~]=xlsread(data)
rows=size(data,1)
rules=[0,0.179,0.258,0.35,0.504]
width_data=zeros(rows,2)
width_data(:,1)=data
width_data(:,2)=arrayfun(@find_type,data)
%%等频离散化
frequent_data=zeros(rows,2)
frequent_data(:,1)=data
end_=-1
for i=1:k-1
start_=floor((i-1)*rows/k)+1
end_=floor(i*rows/k)
frequent_data(start_:end_,2)=i
end
frequent_data(end_+1:end,2)=k
%%聚类离散化
[idx,~]=kmeans(data,k)
cluster_data=zeros(rows,2)
cluster_data(:,1)=data
cluster_data(:,2)=idx
figure
cust_subplot(width_data,3,1,1,k)
cust_subplot(frequent_data,3,1,2,k)
cust_subplot(cluster_data,3,1,3,k)
disp('数据离散化完成!')
-------------cust-subplot.m------------------------------------------------
function cust_subplot(width_data,rows,cols,index,k)
%% 自定义画图
subplot(rows,cols,index)
%%定义图形颜色和数据点的格式
dot_str ={'k*','ko','ks','kd'}
hold on
for i=1:k
data_ = width_data((width_data(:,2)==i),1)
num = size(data_,1)
y= zeros(num,1)
%%y(:)表示y数组(或向量)中的每一个元素
y(:)=0.5*(i+1)
plot(data_,y,dot_str{1,i})
% plot(data_,y)
end
%title(title_)
hold off
end
——------------find_type.m---------------------------------------------------------------
function [ flag ] = find_type( data )
%% 根据规则,返回类别,1,2,3,4中的一个
rules = [0,0.179,0.258,0.35,0.504]
cols = size(rules,2)
for i=1:cols-1
if data>rules(i) &&data<=rules(i+1)
flag =i
return
end
end
flag =-1
end
可以运行出来的代码
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)