У меня есть функция, определенная в одном из функциональных блоков simulink.В этой функции я определил некоторые постоянные переменные и хочу изменить их значения на основе определенных условий.Значения этих постоянных переменных не меняются.Вот мой код,
function [er,mem,phi_hat_dot,hdot,ldot,x_hat,x_til1,...
x_til2,f_hat,g_hat,x_hat_dot,h_bar,ns]= ident(x,u,h,...
l,phi_hat,gain)
persistent i Z x_tj; % initialized persistent values
if isempty(i)
i=0;
end
if isempty(Z)
Z=zeros(5,15);
end
if isempty(x_tj)
%si=size(Z);
x_tj=zeros(length(x),15);
end
Gamma=gain(1);a=gain(2);A=gain(3); % gains
x1=x(1);x2=x(2); % state variable
z=[x1,x2,x1*(x1^2+x2^2),x2*(x1^2+x2^2),u]';
hdot=-a*h+z;
ldot=-A*l+x;
ns=1+h'*h+l'*l;
x_bar=x/ns;h_bar=h/ns;l_bar=l/ns;
x_hat=phi_hat*h_bar+a*l_bar;
e=x_hat-x_bar;
%%%%%%%%%Memory Stack%%%%%%%
while i<15 %This is the place where I am altering persistent variables
Z(:,i+1)=h_bar;
x_tj(:,i+1)=x_bar;
i=i+1;
end
mem=x_tj;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%Error Stack%%%%%%%%%%%
%Gamma=102;
si=size(Z);
er=zeros(length(x),si(2));
if i>=15
for j=1:si(2)
er(:,j)=x_hat-x_tj(:,j);
end
phi_hat_dot=-Gamma*e*h_bar'-Gamma*er*Z';
else
phi_hat_dot=-Gamma*e*h_bar';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%Update Law%%%%%%%%%%%
x_til1=x-x_hat;
x_til2=x_bar-x_hat;
f_hat=phi_hat(:,1:4)*z(1:4);
g_hat=phi_hat(:,5)*1;
x_hat_dot=f_hat+g_hat*u;