📄 design3.m
字号:
%**********************
%求使线性二次型性能指标J-->Jmin状态反馈向量K及其最优控制律
wind_demo9=figure('color',[1,1,1],'position',[0.1*winw,0.1*winh,0.8*winw,0.8*winh],...
'name','最优控制器设计:求使J-->Jmin状态反馈向量K,最优控制律U*(t),最优控制前后的系统极点和状态曲线','numbertitle','off','menubar','none',...
'resize','off','clipping','off');
color=[0.6;.75;.9];
uicontrol('style','frame','position',[0,0,1,1],...
'units','normalized','backgroundcolor',color);
pos=[0.03,0.93,0.3,0.03];color=[0.9,0.9,0.9];
showtext(pos,color,'当前系统状态空间和待输入Q和R矩阵:');
color=[.8;.8;0.8];
uicontrol('style','frame','position',[0.03,0.67,.75,.26],...
'units','normalized','backgroundcolor',color);
pos=[0.04,0.88,0.03,0.03];
showtext(pos,color,'AK:');
demo_edita=uicontrol(wind_demo9,'style','edit','pos',[0.07,0.88,0.705,0.05],...
'string',mat2str(ak),'back',[0.9,0.9,0.9],'units','normalized');
pos=[0.04,0.84,0.03,0.03];
showtext(pos,color,'BK:');
demo_editb=uicontrol(wind_demo9,'style','edit','pos',[0.07,0.84,0.705,0.04],...
'string',mat2str(bk),'back',[0.9,0.9,0.9],'units','normalized');
pos=[0.04,0.80,0.03,0.03];
showtext(pos,color,'CK:');
demo_editc=uicontrol(wind_demo9,'style','edit','pos',[0.07,0.80,0.705,0.04],...
'string',mat2str(ck),'back',[0.9,0.9,0.9],'units','normalized');
pos=[0.04,0.76,0.03,0.03];
showtext(pos,color,'DK:');
demo_editd=uicontrol(wind_demo9,'style','edit','pos',[0.07,0.76,0.705,0.04],...
'string',mat2str(dk),'back',[0.9,0.9,0.9],'units','normalized');
pos=[0.04,0.72,0.02,0.03];
showtext(pos,color,'Q:');
demo_editq=uicontrol(wind_demo9,'style','edit','pos',[0.07,0.72,0.705,0.04],...
'string','[ ]','back',[0.9,0.9,0.9],'units','normalized');
pos=[0.04,0.68,0.02,0.03];
showtext(pos,color,'R:');
demo_editr=uicontrol(wind_demo9,'style','edit','pos',[0.07,0.68,0.705,0.04],...
'string','[ ]','back',[0.9,0.9,0.9],'units','normalized');
%*************
color=[0.9,0.9,0.9];
pos=[0.03,0.63,0.18,0.03];
showtext(pos,color,'最优反馈增益矩阵K为:');
color=[.8;.8;0.8];
uicontrol('style','frame','position',[0.03,0.57,.75,.06],...
'units','normalized','backgroundcolor',color);
pos=[0.035,0.58,0.02,0.03];
showtext(pos,color,'K:');
%*************
color=[0.9,0.9,0.9];
pos=[0.03,0.53,0.15,0.03];
showtext(pos,color,'最优控制律U(t)为:');
color=[.8;.8;0.8];
uicontrol('style','frame','position',[0.03,0.47,.75,.06],...
'units','normalized','backgroundcolor',color);
pos=[0.035,0.48,0.045,0.03];
showtext(pos,color,'U*(t):');
%***************
color=[0.9,0.9,0.9];
pos=[0.03,0.43,0.14,0.03];
showtext(pos,color,'反馈前系统极点:');
color=[.8;.8;0.8];
uicontrol('style','frame','position',[0.03,0.37,.75,.06],...
'units','normalized','backgroundcolor',color);
pos=[0.035,0.38,0.03,0.03];
showtext(pos,color,'FP:');
%****************
color=[0.9,0.9,0.9];
pos=[0.03,0.33,0.14,0.03];
showtext(pos,color,'反馈后系统极点:');
color=[.8;.8;0.8];
uicontrol('style','frame','position',[0.03,0.27,.75,.06],...
'units','normalized','backgroundcolor',color);
pos=[0.035,0.28,0.03,0.03];
showtext(pos,color,'BP:');
%^^^^^^^^^^^^^^^^^^^^
color=[0.9,0.9,0.9];
pos=[0.03,0.23,0.2,0.03];
showtext(pos,color,'最优控制后系统状态空间:');
color=[.8;.8;0.8];
uicontrol('style','frame','position',[0.03,0.05,.75,.18],...
'units','normalized','backgroundcolor',color);
pos=[0.035,0.18,0.02,0.03];
showtext(pos,color,'A:');
pos=[0.035,0.14,0.02,0.03];
showtext(pos,color,'B:');
pos=[0.035,0.1,0.02,0.03];
showtext(pos,color,'C:');
pos=[0.035,0.06,0.02,0.03];
showtext(pos,color,'D:');
vvv=')';
%******************
color=[0,0.7,1];
str1=['set(demo9_1,''enable'',''on'');set(demo9_2,''enable'',''on'');set(demo9_3,''enable'',''on'');set(demo9_4,''enable'',''on'');',...
'demoq=eval(get(demo_editq,''string''));demor=eval(get(demo_editr,''string''));',...
'[k,p,e]=lqr(ak,bk,demoq,demor);',...
'af=ak-bk*k;bf=bk;cf=ck;df=dk;',...
'[numf,denf]=ss2tf(af,bf,cf,df,1);',...
'[nr,nc]=size(k);stru=[];',...
'for g=1:nc',...
' stru=[stru ''-'' mat2str2(k(g)) ''X('' mat2str2(g)vvv];',...
'end;',...
'uicontrol(wind_demo9,''style'',''edit'',''pos'',[0.06,0.58,0.715,0.04],''string'',mat2str(k),''back'',[0,0.7,1],''units'',''normalized'');',...
'uicontrol(wind_demo9,''style'',''edit'',''pos'',[0.085,0.48,0.69,0.04],''string'',stru,''back'',[0,0.7,1],''units'',''normalized'');'];
str2=['at=ak-bk*k;bt=bk;ct=ck;dt=dk;',...
'[z1,p1,k1]=ss2zp(ak,bk,ck,dk,1);',...
'[z2,p2,k2]=ss2zp(at,bt,ct,dt,1);',...
'uicontrol(wind_demo9,''style'',''edit'',''pos'',[0.07,0.38,0.705,0.04],''string'',mat2str(p1),''back'',[0,0.7,1],''units'',''normalized'');',...
'uicontrol(wind_demo9,''style'',''edit'',''pos'',[0.07,0.28,0.705,0.04],''string'',mat2str(p2),''back'',[0,0.7,1],''units'',''normalized'');'];
str3=['set(demo9_5,''enable'',''on'');',...
'uicontrol(wind_demo9,''style'',''edit'',''pos'',[0.06,0.18,0.715,0.04],',...
' ''string'',mat2str(af),''back'',[0,0.7,1],''units'',''normalized'');',...
'uicontrol(wind_demo9,''style'',''edit'',''pos'',[0.06,0.14,0.715,0.04],',...
' ''string'',mat2str(bf),''back'',[0,0.7,1],''units'',''normalized'');',...
'uicontrol(wind_demo9,''style'',''edit'',''pos'',[0.06,0.1,0.715,0.04],',...
' ''string'',mat2str(cf),''back'',[0,0.7,1],''units'',''normalized'');',...
'uicontrol(wind_demo9,''style'',''edit'',''pos'',[0.06,0.06,0.715,0.04],',...
' ''string'',mat2str(df),''back'',[0,0.7,1],''units'',''normalized'');'];
str4=['wbode=figure(''color'',[.1,.7,.8],''position'',[0.1*winw,0.1*winh,0.8*winw,0.8*winh],',...
' ''name'',''最优控制前后Bode图对比分析'',''numbertitle'',''off'',''menubar'',''figure'',''resize'',''off'');',...
'[mag,phase,w]=bode(bnum,bden);',...
'subplot(2,2,1);semilogx(w,20*log10(mag));',...
'grid;ylabel(''幅值/db'');title(''系统最优控制前Bode图之幅值图'');',...
'subplot(2,2,2);semilogx(w,phase);',...
'grid;ylabel(''相位/deg'');title(''系统最优控制前Bode图之相位图'');',...
'[mag,phase,w]=bode(numf,denf);',...
'subplot(2,2,3);semilogx(w,20*log10(mag));',...
'grid;xlabel(''频率'');ylabel(''幅值/db'');title(''系统最优控制后Bode图之幅值图'');',...
'subplot(2,2,4);semilogx(w,phase);',...
'grid;xlabel(''频率'');ylabel(''相位/deg'');title(''系统最优控制后Bode图之相位图'');'];
str5=['t=[0:0.01:20];',...
'wstep=figure(''color'',[.1,0.7,.8],''position'',[0.1*winw,0.1*winh,0.8*winw,0.8*winh],',...
' ''name'',''最优控制前后Step阶跃响应对比分析'',''numbertitle'',''off'',''menubar'',''figure'',''resize'',''off'');',...
'dcg=dcgain(a,b,c,d);',...
'[yc x]=step(a,b,c,d,1,t);',...
'yc1=yc/dcg;',...
'subplot(2,1,1);',...
'plot(t,x,''r'',t,yc1,''b'');',...
'grid;xlabel(''s/t'');ylabel(''Y(t)和X(t)'');title(''最优控制前蓝线:Y(t)阶跃响应;红线:X(t)状态曲线'');',...
'dcg=dcgain(af,bf,cf,df);',...
'[yc x]=step(af,bf,cf,df,1,t);',...
'yc1=yc/dcg;',...
'subplot(2,1,2);',...
'plot(t,x,''r'',t,yc1,''b'');',...
'grid;xlabel(''s/t'');ylabel(''Y(t)和X(t)'');title(''最优控制后蓝线:Y(t)阶跃响应;红线:X(t)状态曲线'');'];
str6=['at=a;bt=b;ct=c;dt=d;numt=bnum;dent=bden;numt1=num;dent1=den;numt2=ffnum;dent2=ffden;',...
'a=af;b=bf;c=cf;d=df;bnum=numf;bden=denf;[num,den]=cloop(bnum,bden,1);ffnum=1;ffden=1;',...
'[ff,pp]=uiputfile(''*.mat'',''保存模型数据'',0.3*winw,0.4*winh);eval([''save '' pp,ff '' a b c d num den bnum bden ffnum ffden '']);test=1;',...
'a=at;b=bt;c=ct;d=dt;bnum=numt;bden=dent;num=numt1;den=dent1;ffnum=numt2;ffden=dent2;'];
uicontrol(wind_demo9,'style','push','string','求K和U*(t)',...
'position',[0.79,0.85,0.2,0.06],'units','normalized',...
'callback',str1);
demo9_1=uicontrol(wind_demo9,'style','push','string','求FP和BP',...
'position',[0.79,0.75,0.2,0.06],'units','normalized','enable','off',...
'callback',str2);
demo9_2=uicontrol(wind_demo9,'style','push','string','求最优控制后状态空间',...
'position',[0.79,0.65,0.2,0.06],'units','normalized','enable','off',...
'callback',str3);
demo9_3=uicontrol(wind_demo9,'style','push','string','最优控制前后Bode图',...
'position',[0.79,0.55,0.2,0.06],'units','normalized','enable','off',...
'callback',str4);
demo9_4=uicontrol(wind_demo9,'style','push','string','最优控制前后Step图',...
'position',[0.79,0.45,0.2,0.06],'units','normalized','enable','off',...
'callback',str5);
demo9_5=uicontrol(wind_demo9,'style','push','string','保存最优控制后模型',...
'position',[0.79,0.35,0.2,0.06],'units','normalized','enable','off',...
'callback',str6);
uicontrol(wind_demo9,'style','push','string','结束',...
'position',[0.79,0.25,0.2,0.06],'units','normalized',...
'callback','close(wind_demo9)');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -