控制系统数字仿真与CAD-第四次实验-附完整代码

版权声明:如需转载,请注明出处。 https://blog.csdn.net/weixin_39059031/article/details/85045150

完整代码

exc4.m

clear;
clear all;
clear close;
sa=2;sb=20;l=sb-sa;
e=1e-5;
x1=sa+(1-0.618)*l;
x2=sa+0.618*l;
q1=obj_asr(x1);
q2=obj_asr(x2);
count=0;
x_dot=[x1,x2];
while((sb-sa)/l>e)
    if(q1>q2)
        sa=x1;
        x1=x2;
        q1=q2;
        x2=sa+0.618*(sb-sa);
        x_dot=[x_dot,x2];
        q2=obj_asr(x2);
    else
        sb=x2;
        x2=x1;
        q2=q1;
        x1=sa+(1-0.618)*(sb- sa);
        x_dot=[x_dot,x1];
        q1=obj_asr(x1);
    end
end
Kn_opt=(x1+x2)/2;
q_min=obj_asr(Kn_opt);
opt_asr_plot(Kn_opt);

fun4.m

function [result]=fun4(Kn)
tn=0.03;
Ki=0.269;
ti=0.067;
Ks=76;
Ts=0.00167;
R=6.58;
Tl=0.018;
Tm=0.25;
Ce=0.031;
Alpha=0.00337;
Beta=0.4;
A=[0 0 1 1 0];
B=[tn ti Ts Tl Tm*Ce];
C=[Kn Ki Ks 1/R R];
D=[Kn*tn Ki*ti 0 0 0];
c=8;
r=1;
W=[0 0 0 0 -Alpha;
    1 0 0 -Beta 0;
    0 1 0 0 0;
    0 0 1 0 -Ce;
    0 0 0 1 0];
W0=[1 0 0 0 0 ]';
block_num=5;
h=0.001; 
t_end=0.5;
t=0:h:t_end;
for k=1:block_num
    if (A(k)==0) %求积分或积分比例环节各系数
        FI(k)=1;
        FIM(k)=h*C(k)/B(k);
        FIJ(k)=h*h*C(k)/B(k)/2; 
        FIC(k)=1;
        FID(k)=0;
        if(D(k)~=0) %若为积分比例,修正fai d
            FID(k)=D(k)/B(k);
        end
    else
        FI(k)=exp(-h*A(k)/B(k)); % 求惯性或惯性比例环节各系数 
        FIM(k)=(1-FI(k))*C(k)/A(k);
        FIJ(k)=h*C(k)/A(k)-FIM(k)*B(k)/A(k); 
        FIC(k)=1;
        FID(k)=0;
        if(D(k)~=0)
            FIC(k)=C(k)/D(k)-A(k)/B(k);
            FID(k)=D(k)/B(k);
        end
    end
end
Y0=[0 0 0 0 0]'; 
n=length(t);
Y=Y0;
X=zeros(block_num,1);
result=Y;
Uk=zeros(block_num,1);
Ub=Uk; 
for m=1:(n-1)
    Ub=Uk;
    Uk=W*Y+W0*r;
    Uf=2*Uk-Ub;
    Udot=(Uk-Ub)/h;
%     X=FI'.*X+FIM'.*Uk;
%     Y=FIC'.*X+FID'.*Uf;
    X=FI'.*X+FIM'.*Uk+FIJ'.*Udot;
    Y=FIC'.*X+FID'.*Uf;
    Y(1)=satur(Y(1),c);
    result=[result,Y];
end

end

obj_asr.m

function q=obj_asr(Kn)
result = fun4(Kn);
y_inf = 296.7350;
q=100*(max(result(5,:))-y_inf)/y_inf; 
end

opt_asr_plot.m

function q=opt_asr_plot(Kn)
result = fun4(Kn);
h=0.001; 
t_end=0.5;
t=0:h:t_end;
figure
subplot(4,1,1),plot(t,result(4,:)),grid,title('Id')
subplot(4,1,2),plot(t,result(3,:)),grid,title('Ud')
subplot(4,1,3),plot(t,result(2,:)),grid,title('ACR')
subplot(4,1,4),plot(t,result(1,:)),grid,title('ASR')
figure;
plot(t,result(5,:),'r');grid,title('n') 
end

satur.m

function [uo]=satur(ui,c)
if (abs(ui)<=c)
    uo=ui;
elseif (ui>c)
    uo=c;
else
    uo=-c;
end
end

猜你喜欢

转载自blog.csdn.net/weixin_39059031/article/details/85045150
今日推荐