can someone please explain this program
Mostrar comentarios más antiguos
%N=50,P=10,M=1,w1=1,w2=0
clc
clear all
P=10;%prediction horizon
M=1;%control horizon
N=50;%model length
w2=0;%control weight
w1= 1;%error weight
ysp=1;%output set point from 0
timesp=1;%time of set point chang
delt=0.1;%sampling time interval
tfinal=5;%final simulation time
%define time
tvec=0:delt:tfinal;
ksp=fix(timesp/delt);
kfinal=length(tvec);
%define set point vector
r=[zeros(1,ksp),ones(1,(kfinal-ksp)/2)*ysp,zeros(1,(kfinal-ksp)/4)*ysp,ones(1,(kfinalksp)/
4+1)*ysp];
z= tf('z',delt); plant = (.2713)/(z^3-0.8351*z^2)
%//////////////////define model here////////////////////////////////////
%assumption plant = model
model=plant;
% [numm,denm,tm]=tfdata(plant);
numm = get(model,'num'); numm = numm{:}; % Get numerator polynomial
denm = get(model,'den'); denm = denm{:}; % Get denominator polynomial
%define step response coefficient matrix
s=step(model,0:delt:N*delt);
%Calculation of Sp
for i=1:P
for j=1:N-2
if(i+j<=N-1)
Sp(i,j)=s(i+j+1);
else
Sp(1,j)=0;
end
end
end
%Calculation of Dynamic Matrix Sf
for i=1:P
for j=1:M
if i+1-j>0
Sf(i,j)=s(i+2-j);
else
Sf(i,j)=0;
end
end
end
Sf
%obtain W1 and W2 matrix
W1=w1*eye(P,P);
W2=w2*eye(M,M);
%obtain Kmat where Kmat=inv(Sf'*W1'*W1*Sf + W2'*W2)*Sf'*W1'*W1;
Kmat=inv(Sf'*W1'*W1*Sf + W2'*W2)*Sf'*W1'*W1;
%piant initial conditions
ndenm=length(denm)-1;
nnumm=length(numm)-1;
umpast=zeros(1,nnumm);
ympast=zeros(1,ndenm);
uinit=0;
yinit=0;
%initialize input vector
u=ones(1,min(P,kfinal))*uinit;
dist(1)=0;
y(1)=yinit;
dup=zeros(1,N-2);
for k=1:kfinal
[m,p]=size(Kmat);
for i=1:p
if k-N+i>0
uold(i)=u(k-N+i);
else
uold(i)=0;
end
end
dvec=dist(k)*ones(1,p);
rvec=r(k)*ones(p,1);
y_free=Sp*dup' + s(N)*uold'+dvec';
E=rvec-y_free;
delup(k)=Kmat(1,:)*E;
if k>1
u(k)=u(k-1)+delup(k);
else
u(k)=delup(k)+uinit;
end
%plant equations
umpast=[u(k),umpast(1,1:length(umpast)-1)];
y(k+1)=-denm(2:ndenm+1)*ympast'+numm(2:nnumm+1)*umpast';
ympast=[y(k+1),ympast(1:length(ympast)-1)];
%model prediction
if k-N+1>0
ymod(k+1)=Sf(1,1)*delup(k)+Sp(1,:)*dup'+s(N)*u(k-N+1);
else
ymod(k+1)=Sf(1,1)*delup(k)+Sp(1,:)*dup';
end
%disturbance compensation
dist(k+1)=y(k+1)-ymod(k+1);
dup=[delup(k),dup(1,1:N-3)];
end
%stairs plotting for input(zero order hold) and setpoint
[tt,uu]=stairs(tvec,u);
[ttr,rr]=stairs(tvec,r);
figure(1)
subplot(2,1,1)
plot(ttr,rr,'--',tvec,y(1:length(tvec)))
axis([0 kfinal*delt -2 2])
hold on
ylabel('y');
xlabel('time');
title('plant output(Output Temperature)');
subplot(2,1,2)
plot(tt,uu,'b')
axis([0 kfinal*delt -2 3])
hold on
ylabel('u');
xlabel('time');
title('Controller output(manipulated variable)');
figure(2)
plot(s,'ko')
xlabel('discrete time index, i');
ylabel('s(i)');
title('step response coefficients');
1 comentario
Jason Ross
el 14 de Mzo. de 2013
Edit: Applied "code" tag for readability.
Respuestas (1)
Image Analyst
el 14 de Mzo. de 2013
1 voto
We're in no better position than you. Probably worse since you may have access to the author - the one who would be in the best position to explain this code. About all I could do it to tell you what the comments are, though they're few and vague. Anything beyond that, as far as understanding the code, you could do as well as we could. So I'll let you do it. Do you have access to the author, in person, by email, or via the File Exchange, or by any means whatsoever?
This code is a perfect example of why I recommend that people use long descriptive variable names. Trying to understand something like this code, with its alphabet soup of variable names and lack of descriptive comments, is tedious and no fun. Not enough fun for me to want to explain it to you, so . . . good luck with it.
Categorías
Más información sobre General Applications en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!