pid controller S-Function

Status
Not open for further replies.

HAIDE

Junior Member level 3
Joined
Feb 9, 2012
Messages
27
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
GERMANY
Activity points
1,525
I see S-function like this on a paper, it is pid controller, in fact we have two inputs and four output that we use one of them but in this sfunction wrote this
sizes.NumOutputs=4+5*nn;
sizes.NumInputs=9+15*nn;
is it true in your mind?
Code:
[HTML]

function [sys,x0,str,ts]=kharproje1(t,x,u,flag,T,nn,K_pid,...
    eta_pid,xite,alfa,beta0,w0,hh)
switch flag,
    case 0
        [sys,x0,str,ts]=mdlinitializeSizes(T,nn);% 0:return parameters and initial Start condition the number of dimensions
    case 2
        sys=mdlUpdates(u);%1:the return state of the system derivative dx /dt
    case 3
        sys=mdlOutputs(t,x,u,T,nn,K_pid,eta_pid,...% 2:x (n +1) return to the discrete state
        xite,alfa,beta0,w0);
    case{1,4,9}%3: returnsOutput vector y
        %4 to update a discrete state Interval
        %
        sys=[];
    otherwise
        error (['Unhandled flag=',num2str(flag)]);
end

function [sys,x0,str,ts]=mdlinitializeSizes(T,nn)
sizes=simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=3;
sizes.NumOutputs=4+5*nn;
sizes.NumInputs=9+15*nn;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=zeros(3,1); 
str=[];
ts=[T 0];

function sys=mdlUpdates(u)
sys=[u(1)-u(2);u(1);u(1)+u(3)-2*u(2)];

function sys=mdlOutputs(t,x,u,T,nn,K_pid,eta_pid,...
    xite,alfa,beta0,w0)
ci_3=reshape(u(7:6+36*nn),3,nn);
ci_2=reshape(u(7+5*nn:6+8*nn),3,nn);
ci_1=reshape(u(7+10*nn:6+13*nn),3,nn);
bi_3=u(7+3*nn:6+4*nn);
bi_2=u(7+8*nn:6+9*nn);
bi_1=u(7+13*nn:6+14*nn);
w_3=u(7+4*nn:6+5*nn);
w_2=u(7+9*nn:6+10*nn);
w_1=u(7+14*nn:6+15*nn);
xx=u([6;4;5]);
if t==0
    ci_1=w0(1)*ones(3,nn);
    bi_1=w0(2)*ones(nn,1);
    w_1=w0(3)*ones(nn,1);
    K_pid0=K_pid;
else
    K_pid0=u(end-2:end);
end
for j=1:1:nn
          hh(j,1)=exp(-norm(xx-ci_1(:,j))^2/(2*bi_1(j)*bi_1(j)));
end
       dym=u(4)-w_1'*hh;
       w=w_1+xite*dym*hh+alfa*(w_1-w_2)+beta0*(w_2-w_3);
for j=1:1:nn
    d_bi(j,1)=xite*dym*w_1(j)*hh(j)*(bi_1(j)^(-3))*norm(xx-ci_1(:,j))^2;
    d_ci(:,j)=xite*dym*w_1(j)*hh(j)*(xx-ci_1(:,j))*(bi_1(j)^(-2));
end
bi=bi_1+ d_bi+alfa*(bi_1-bi_2)+beta0*(bi_2-bi_3);
ci=ci_1+d_ci+alfa*(ci_1-ci_2)+beta0*(ci_2-ci_3);
dJac=sum(w*hh*(-xx(1)+ci(1,:)')/bi^2)%jacobian
KK=K_pid0+u(1)*dJac*eta_pid*x;
sys=[u(6)+KK'*x;KK;ci(:);bi(:);w(:)];[/HTML]
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…