控制系统数字仿真技术
1. c 语言仿真程序及仿真结果
#include #include #include #include #include
doubleoutputy[3][1501],st,dt; doublelp,vn; voidmainrun(); voiddispcurve(); void main() { }
voidmainrun() {
doublex11,x21,x31,x41,x51,x10,x20,x30,x40,x50,r,e1,e2,u10,u11,u20,u21,y1,y0;
inti,j,k,m,n,c;
x10=0;x20=0;x30=0;x40=0;x50=0;r=10;r=10;e1=0;e2=0;u10=0;u20=0;y0=0; outputy[0][0]=0;outputy[1][0]=0;outputy[1][0]=0; double f1[13]={0}; double f2[3]={0}; double f3[2]={0}; double f4[2]={0};
double kp1,ti1,td1,kp2,ti2,td2; double a1,a2;
kp1=1.2;ti1=80;td1=5;kp2=0.8;ti2=40;td2=0; a1=exp(-dt/10);a2=exp(-dt/25); st=1000;dt=1; lp=st/dt; mainrun(); vn=3; dispcurve();
{
e1=r+0.5*y0;
u11=u10+(kp1+kp1*dt/ti1+kp1*td1/dt)*e1-(kp1+2*kp1*td1/dt)*f3[1]+(kp1*td1/dt)*f3[0];
e2=u11+0.5*x20;
u21=u20+(kp2+kp2*dt/ti2+kp2*td2/dt)*e2-(kp2+2*kp2*td2/dt)*f4[1]+(kp2*td2/dt)*f4[0];
}
for (c=0;c
printf("%14.4f",outputy[0][c]); /*if (c/4==0)*/
x11=a1*x10+(1-a1)*u20; x21=a1*x20-0.8*(1-a1)*f2[0]; x31=a2*x30-0.85*(1-a2)*u20; x41=a2*x40+(1-a2)*x30; x51=a2*x50+(1-a2)*x40; y1=a2*y0+(1-a2)*f1[0];
outputy[0][i]=y1;outputy[1][i]=u21;outputy[2][i]=u11; for(j=0;j
f1[j]=f1[j+1]; f1[12]=x51; for(k=0;k
f2[k]=f2[k+1]; f2[2]=x11; for(m=0;m
f3[m]=f3[m+1]; f3[1]=e1; for(n=0;n
f4[n]=f4[n+1]; f4[1]=e2;
y0=y1;u10=u11;u20=u21;x10=x11;x20=x21;x30=x31;x40=x41;x50=x51;
}
}
voiddispcurve() {
int drive=DETECT; int mode=2; inti,j;
doubleym,kt,ky; char s[100],c; doublex,y,xe,ye; doublety;
initgraph(&drive,&mode," ");
ym=fabs(outputy[0][0]); /*给最大显示值赋初值*/ for(j=0;j
for(i=0;i
/*循环得到最大值*/ /*图像显示的范围*/
if(fabs(outputy[i][j])>ym) ym=fabs(outputy[i][j])/2;
}
x=40; xe=620;
y=40; ye=440; kt=(xe-x)/(lp*dt); ky=(ye-y)/ym/2.0; ty=y+(ye-y)/2; line(x,ty,xe+10,ty); line(xe,ty+3,xe+10,ty); line(xe,ty-3,xe+10,ty); line(x,y-10,x,ye+10); line(x-3,y,x,y-10);
/*画Y 轴和Y 轴的箭头*/
/*画X 轴和X 轴的箭头*/
for(j=1;j
line(x+j*(xe-x)/10.0,ty-3,x+j*(xe-x)/10.0,ty+3); /*均分X 轴*/ for(j=1;j
line(x-3,j*(ye-y)/10.0,x+3,j*(ye-y)/10.0);
/*均分Y 轴*/
outtextxy(xe,ty+5,"t"); outtextxy(x-10,y-10,"y"); for(j=1;j
outtextxy(x-10+j*(xe-x)/10.0,ty+5,s);
}
for(j=-5;j
outtextxy(0,240-j*(ye-y)/10.0,s); }
for(i=0;i
lineto(x+j*dt*kt,ty-ky*outputy[i][j]); }
do { c=getchar(); }
while(c!='q'&& c!='Q'); closegraph();
} /*显示X 轴坐标*/
/*显示Y 轴坐标*/
/*画输出曲线*/
仿真结果:
2. matlab 中simulink 仿真
控制系统数字仿真技术
1. c 语言仿真程序及仿真结果
#include #include #include #include #include
doubleoutputy[3][1501],st,dt; doublelp,vn; voidmainrun(); voiddispcurve(); void main() { }
voidmainrun() {
doublex11,x21,x31,x41,x51,x10,x20,x30,x40,x50,r,e1,e2,u10,u11,u20,u21,y1,y0;
inti,j,k,m,n,c;
x10=0;x20=0;x30=0;x40=0;x50=0;r=10;r=10;e1=0;e2=0;u10=0;u20=0;y0=0; outputy[0][0]=0;outputy[1][0]=0;outputy[1][0]=0; double f1[13]={0}; double f2[3]={0}; double f3[2]={0}; double f4[2]={0};
double kp1,ti1,td1,kp2,ti2,td2; double a1,a2;
kp1=1.2;ti1=80;td1=5;kp2=0.8;ti2=40;td2=0; a1=exp(-dt/10);a2=exp(-dt/25); st=1000;dt=1; lp=st/dt; mainrun(); vn=3; dispcurve();
{
e1=r+0.5*y0;
u11=u10+(kp1+kp1*dt/ti1+kp1*td1/dt)*e1-(kp1+2*kp1*td1/dt)*f3[1]+(kp1*td1/dt)*f3[0];
e2=u11+0.5*x20;
u21=u20+(kp2+kp2*dt/ti2+kp2*td2/dt)*e2-(kp2+2*kp2*td2/dt)*f4[1]+(kp2*td2/dt)*f4[0];
}
for (c=0;c
printf("%14.4f",outputy[0][c]); /*if (c/4==0)*/
x11=a1*x10+(1-a1)*u20; x21=a1*x20-0.8*(1-a1)*f2[0]; x31=a2*x30-0.85*(1-a2)*u20; x41=a2*x40+(1-a2)*x30; x51=a2*x50+(1-a2)*x40; y1=a2*y0+(1-a2)*f1[0];
outputy[0][i]=y1;outputy[1][i]=u21;outputy[2][i]=u11; for(j=0;j
f1[j]=f1[j+1]; f1[12]=x51; for(k=0;k
f2[k]=f2[k+1]; f2[2]=x11; for(m=0;m
f3[m]=f3[m+1]; f3[1]=e1; for(n=0;n
f4[n]=f4[n+1]; f4[1]=e2;
y0=y1;u10=u11;u20=u21;x10=x11;x20=x21;x30=x31;x40=x41;x50=x51;
}
}
voiddispcurve() {
int drive=DETECT; int mode=2; inti,j;
doubleym,kt,ky; char s[100],c; doublex,y,xe,ye; doublety;
initgraph(&drive,&mode," ");
ym=fabs(outputy[0][0]); /*给最大显示值赋初值*/ for(j=0;j
for(i=0;i
/*循环得到最大值*/ /*图像显示的范围*/
if(fabs(outputy[i][j])>ym) ym=fabs(outputy[i][j])/2;
}
x=40; xe=620;
y=40; ye=440; kt=(xe-x)/(lp*dt); ky=(ye-y)/ym/2.0; ty=y+(ye-y)/2; line(x,ty,xe+10,ty); line(xe,ty+3,xe+10,ty); line(xe,ty-3,xe+10,ty); line(x,y-10,x,ye+10); line(x-3,y,x,y-10);
/*画Y 轴和Y 轴的箭头*/
/*画X 轴和X 轴的箭头*/
for(j=1;j
line(x+j*(xe-x)/10.0,ty-3,x+j*(xe-x)/10.0,ty+3); /*均分X 轴*/ for(j=1;j
line(x-3,j*(ye-y)/10.0,x+3,j*(ye-y)/10.0);
/*均分Y 轴*/
outtextxy(xe,ty+5,"t"); outtextxy(x-10,y-10,"y"); for(j=1;j
outtextxy(x-10+j*(xe-x)/10.0,ty+5,s);
}
for(j=-5;j
outtextxy(0,240-j*(ye-y)/10.0,s); }
for(i=0;i
lineto(x+j*dt*kt,ty-ky*outputy[i][j]); }
do { c=getchar(); }
while(c!='q'&& c!='Q'); closegraph();
} /*显示X 轴坐标*/
/*显示Y 轴坐标*/
/*画输出曲线*/
仿真结果:
2. matlab 中simulink 仿真