function J= GA_PID(kpid,opt) Error: Function definition not supported in this context. Create functions in code file.
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% GA03.m
% kpid: pid 參數
% opt=1: 畫出系統響應圖
function J= GA_PID(kpid,opt)
% 將受控體轉為數位模型
ts=0.01;
sys=tf(39,[1,74,0]);
dsys=c2d(sys,ts,'z');
[num,den]=tfdata(dsys,'v');
% 初值設定
u_1=0.0;u_2=0.0;
y_1=0.0;y_2=0.0;
e_1=0;e_2=0;
x=[0,0,0];
w1=0.9;
w2=0.01;
w3=200;
w4=2;
J=0;
s=0;
tu=0;
for k=1:1:100
time(k)=k*ts;
r(k)=1;
u(k)=kpid(1)*x(1)+kpid(2)*x(2)+kpid(3)*x(3);
% u 限定在 -10 ~ 10
if u(k) >= 10
u(k)=10;
end
if u(k) <= -10
u(k)=-10;
end
y(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;
e(k)=r(k)-y(k);
% 算目標函數
J=J+w1*abs(e(k))+w2*u(k)*u(k);
ey(k)=y(k)-y_1;
if ey(k) < 0
J=J+w3*abs(ey(k));
end
% 算上升時間
if y(k) > 0.95 & y(k) < 1.05 & s==0
tu=time(k);
s=1;
end
% 增量式數位 PID
%x(1)=e(k)-e_1;
%x(2)=e(k);
%x(3)=e(k)-2*e_1+e_2;
% 一般數位 PID
x(1)=e(k);
x(2)=x(2)+e(k)*ts;
x(3)=(e(k)-e_1)/ts;
% 更新 u(k-2),u(k-1)
u_2=u_1;u_1=u(k);
% 更新 y(k-2),y(k-1)
y_2=y_1;y_1=y(k);
% 更新 e(k-2),e(k-1)
e_2=e_1;e_1=e(k);
end
% 算目標函數
J=J+w4*tu;
% 畫出響應圖
if opt==1
figure(1);
plot(time,r,'r',time,y,'b');
xlabel('Time(s)');ylabel('r,y');
end
0 commentaires
Réponses (1)
Geoff Hayes
le 23 Avr 2019
Lin - I observe that same error message if I copy your code and paste it into the MATLAB command window. The error message is telling you that you need to create a file named GA_PID.m and save the above code to that file. You would then call this function from the command line, passing in the correct inputs. See Declare function name, inputs, and outputs for more details.
7 commentaires
Geoff Hayes
le 24 Avr 2019
yes you need to define the input parameters to this function. Look at the signature and initial comments:
% GA03.m
% kpid: pid 參數
% opt=1: 畫出系統響應圖
function J= GA_PID(kpid,opt)
Presumably, it is telling you what kpid and opt should be...they need to be passed into your function so that it can make use of them. You may want to contact the author of the code to see how they should be defined.
Voir également
Catégories
En savoir plus sur White dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!