Optimize pid constants using PSO toolbox
7 次查看(过去 30 天)
显示 更早的评论
Hi! I'm trying to use the PSO toolbox to find very good values for the PID controller constants. The constants are the K and s is the Laplace domain variable. To do this I wrotte the code:
g = @(s)( 6.3223*s^2 + 18*s + 12.812 )/s;
PID = @(K)K(1) + K(2)/s + K(3)*s;
input = @(s)heaviside(s);
H = @(K)PID(K)*g(s);
error = @(K)input(s)/(H(K) + 1);
ITAE = @(K)int(abs(error(K))*t,0,inf);
nvar = 3;
rng default
[x,fval] = particleswarm(ITAE, nvar);
But when I run the code I get the error:
Error using particleswarm>makeState (line 700) Failure in initial objective function evaluation. PARTICLESWARM cannot continue.
Error in particleswarm>pswcore (line 170) state = makeState(nvars,lbMatrix,ubMatrix,objFcn,options);
Error in particleswarm (line 152) [x,fval,exitFlag,output] = pswcore(objFcn,nvars,lbRow,ubRow,output,options);
Error in Untitled (line 21) [x,fval] = particleswarm(ITAE, nvar);
Caused by: Undefined function or variable 's'.
I want to make an ajustment to any value of s, so I would got all K. But matlab is complaining about s.
0 个评论
回答(1 个)
John D'Errico
2017-1-22
编辑:John D'Errico
2017-1-22
Your problem is, you don't understand how function handles work. Actually, you have several problems in this code, but that is arguably your main problem.
Consider this line:
PID = @(K)K(1) + K(2)/s + K(3)*s;
You have created the function handle PID, telling it the unknown that can be varied is K. As fas as PID is concerned, s is a CONSTANT. Whatever value s has when PID is created, it is now fixed in the PID workspace.
The same thing happens here:
H = @(K)PID(K)*g(s);
H sees only K, not s as a variable. But again, you want s to be varied.
Here too:
error = @(K)input(s)/(H(K) + 1);
Worse yet, s is a function of the function input? You do know this is a bad idea, to define function names with the same name as an existing function? I had to look in your code to see that you did define a function handle called input. Again, VERY BAD programming style.
Next, you define
ITAE = @(K)int(abs(error(K))*t,0,inf);
Hmm. As far as ITAE is concerned, K is the only variable. But we know that s is a variable here. Worse, then you use int to do the integration. s and K are numeric variables, as they ABSOLUTELY MUST BE to perform an optimization. So using a symbolic numerical integration is not possible. Numerical variables are constants as far as a symbolic tool is concerned. So that integral from 0 to inf will be inf. Worse yet, you use an undefined variable t in that function.
And then, you call particle swarm, like this:
[x,fval] = particleswarm(ITAE, nvar);
You never tell it what are the variables. K? s? t? Where did t come from, anyway? Is it the the variable of integration, that int should magically know is symbolic? How does particleswarm know that K is actually a vector of length 3? Where do those variables live?
So multiple problems, stemming first from your failure to understand function handles. Also from your not understanding how symbolic, optimization and integration tools work. And finally, t is an undefined variable.
You should spend some time reading about function handles. Then learn to use optimization tools, integration tools. Learn that numerical tools do not work with symbolic variables.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle Swarm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!