how to solve error Cannot create variable 'e1' in workspace

11 visualizaciones (últimos 30 días)
Mustafa Sabah
Mustafa Sabah el 2 de Mzo. de 2019
Comentada: Walter Roberson el 5 de Mzo. de 2019
erorr in pso wind
Cannot create variable 'e1' in workspace
Error in Untitledpsowind (line 34)
current_fitness(i) = simulPID(current_position(:,i));
Caused by:
Error using Untitledpsowind>simulPID (line 143)
Attempt to add "e1" to a static workspace .
See Variables in Nested and Anonymous Functions.
code
% Tuning of PID controller using Particle Swarm Optimization %
%Initialization
clear
clc
n = 5 % Size of the swarm " no of birds "
bird_setp =20 % Maximum number of "birds steps"
dim = 6 % Dimension of the problemmk
c1 = 1.2 ; % PSO parameter C1
c2 = 1.2 ; % PSO parameter C2
w = 0.8 ; % pso momentum
fitness=0*ones(n,bird_setp);
%-----------------------------%
% initialize the parameter %
%-----------------------------%
R1 = rand(dim, n);
R2 = rand(dim, n);
current_fitness =0*ones(n,1);
%------------------------------------------------%
% Initializing swarm and velocities and position %
%------------------------------------------------%
current_position = 5*(rand (dim,n));
velocity = 0.3*randn(dim, n);
local_best_position = current_position ;
%-------------------------------------------%
% Evaluate initial population %
%-------------------------------------------%
for i = 1:n
current_fitness(i) = simulPID(current_position(:,i));
end
local_best_fitness = current_fitness;
[global_best_fitness,g] = min(local_best_fitness) ;
for i=1:n
globl_best_position(:,i) = local_best_position(:,g);
end
%-------------------%
% VELOCITY UPDATE %
%-------------------%
velocity = w *velocity + c1*(R1.*(local_best_position-current_position)) + c2*(R2.*(globl_best_position-current_position));
%------------------%
% SWARMUPDATE %
%------------------%
current_position = current_position + velocity;
%------------------------%
% evaluate anew swarm %
%------------------------%
%% Main Loop
iter = 0 % Iterations counter
while ( iter < bird_setp )
R1 = rand(dim, n) ;
R2 = rand(dim, n) ;
iter = iter + 1 ;
for i = 1:n,
[current_fitness(i)] = simulPID(current_position(:,i)) ;
%out(:,i)=j;
end
for i = 1 : n
if current_fitness(i) < local_best_fitness(i)
local_best_fitness(i) = current_fitness(i);
local_best_position(:,i) = current_position(:,i) ;
end
end
[current_global_best_fitness,g] = min(local_best_fitness) ;
% outt(iter,:)=out(:,g);
if current_global_best_fitness < global_best_fitness
global_best_fitness = current_global_best_fitness;
for i=1:n
globl_best_position(:,i) = local_best_position(:,g) ;
end
end
velocity = w *velocity + c1*(R1.*(local_best_position-current_position)) + c2*(R2.*(globl_best_position-current_position));
current_position=local_best_position;
current_position = current_position + velocity;
sprintf('Iteration No. = %3.0f , Fitness =%3.7f', iter,global_best_fitness )
sprintf('The value of Kp = %3.4f, Ki = %3.4f', globl_best_position(1),globl_best_position(2), globl_best_position(3),globl_best_position(4), globl_best_position(5),globl_best_position(6))
resul(:,iter)=globl_best_position(:,1);
end % end of while loop its mean the end of all step that the birds move it
% ss=outt';
sprintf('pitch angle control gain G1: [Kp Ki]')
Kp=resul(1,iter)
Ki=resul(2,iter)
sprintf('pitch angle control gain G2: [Kp Ki]')
Kp=resul(3,iter)
Ki=resul(4,iter)
sprintf('pitch angle control gain G3: [Kp Ki]')
Kp=resul(5,iter)
Ki=resul(6,iter)
%------------------------%
% plot %
%------------------------%
plot(resul,'DisplayName','resul','YDataSource','resul');figure(gcf)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The PSO gain tuning algorithm subroutine in MATLAB illustrated below
% The name of subroutine is simulPI %
function [Y, X] = simulPID(pid)
Kp = pid(1);
Ki = pid(2);
Kp = pid(3);
Ki = pid(4);
Kp = pid(5);
Ki = pid(6);
[~,~,yout] = sim('power_wind_scig_ahmed',[0 15], simset('solver','ode23tb','AbsTol',1e-6,'SrcWorkspace','Current','DstWorkspace','Current'));
Y=e1;
X=yout;
function [Y, X] = simulPID(pid)
[~,~,yout] = sim('power_wind_scig_ahmed',[0 15], simset('solver','ode23tb','AbsTol',1e-6,'SrcWorkspace','Current','DstWorkspace','Current'));
Y=e2;
X=yout;
function [Y, X] = simulPID(pid)
[~,~,yout] = sim('power_wind_scig_ahmed',[0 15], simset('solver','ode23tb','AbsTol',1e-6,'SrcWorkspace','Current','DstWorkspace','Current'));
Y=e3;
X=yout;
% Tuning of PID controller using Particle Swarm Optimization %
Initialization
clear
clc
n = 5 % Size of the swarm " no of birds "
bird_setp =20 % Maximum number of "birds steps"
dim = 6 % Dimension of the problemmk
c1 = 1.2 ; % PSO parameter C1
c2 = 1.2 ; % PSO parameter C2
w = 0.8 ; % pso momentum
fitness=0*ones(n,bird_setp);
%-----------------------------%
% initialize the parameter %
%-----------------------------%
R1 = rand(dim, n);
R2 = rand(dim, n);
current_fitness =0*ones(n,1);
%------------------------------------------------%
% Initializing swarm and velocities and position %
%------------------------------------------------%
current_position = 5*(rand (dim,n));
velocity = 0.3*randn(dim, n);
local_best_position = current_position ;
%-------------------------------------------%
% Evaluate initial population %
%-------------------------------------------%
for i = 1:n
current_fitness(i) = simulPID(current_position(:,i));
end
local_best_fitness = current_fitness;
[global_best_fitness,g] = min(local_best_fitness) ;
for i=1:n
globl_best_position(:,i) = local_best_position(:,g);
end
%-------------------%
% VELOCITY UPDATE %
%-------------------%
velocity = w *velocity + c1*(R1.*(local_best_position-current_position)) + c2*(R2.*(globl_best_position-current_position));
%------------------%
% SWARMUPDATE %
%------------------%
current_position = current_position + velocity;
%------------------------%
% evaluate anew swarm %
%------------------------%
%% Main Loop
iter = 0 % Iterations counter
while ( iter < bird_setp )
R1 = rand(dim, n) ;
R2 = rand(dim, n) ;
iter = iter + 1 ;
for i = 1:n,
[current_fitness(i)] = simulPID(current_position(:,i)) ;
%out(:,i)=j;
end
for i = 1 : n
if current_fitness(i) < local_best_fitness(i)
local_best_fitness(i) = current_fitness(i);
local_best_position(:,i) = current_position(:,i) ;
end
end
[current_global_best_fitness,g] = min(local_best_fitness) ;
% outt(iter,:)=out(:,g);
if current_global_best_fitness < global_best_fitness
global_best_fitness = current_global_best_fitness;
for i=1:n
globl_best_position(:,i) = local_best_position(:,g) ;
end
end
velocity = w *velocity + c1*(R1.*(local_best_position-current_position)) + c2*(R2.*(globl_best_position-current_position));
current_position=local_best_position;
current_position = current_position + velocity;
sprintf('Iteration No. = %3.0f , Fitness =%3.7f', iter,global_best_fitness )
sprintf('The value of Kp = %3.4f, Ki = %3.4f', globl_best_position(1),globl_best_position(2), globl_best_position(3),globl_best_position(4), globl_best_position(5),globl_best_position(6))
resul(:,iter)=globl_best_position(:,1);
end % end of while loop its mean the end of all step that the birds move it
% ss=outt';
sprintf('pitch angle control gain G1: [Kp Ki]')
Kp=resul(1,iter)
Ki=resul(2,iter)
sprintf('pitch angle control gain G2: [Kp Ki]')
Kp=resul(3,iter)
Ki=resul(4,iter)
sprintf('pitch angle control gain G3: [Kp Ki]')
Kp=resul(5,iter)
Ki=resul(6,iter)
%------------------------%
% plot %
%------------------------%
plot(resul,'DisplayName','resul','YDataSource','resul');figure(gcf)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The PSO gain tuning algorithm subroutine in MATLAB illustrated below
% The name of subroutine is simulPI %
function [Y, X] = simulPID(pid)
Kp = pid(1);
Ki = pid(2);
Kp = pid(3);
Ki = pid(4);
Kp = pid(5);
Ki = pid(6);
[~,~,yout] = sim('untitledPSO',[0 15], simset('solver','ode23tb','AbsTol',1e-6,'SrcWorkspace','Current','DstWorkspace','Current'));
Y=e1;
X=yout;
function [Y, X] = simulPID(pid)
[~,~,yout] = sim('untitledPSO',[0 15], simset('solver','ode23tb','AbsTol',1e-6,'SrcWorkspace','Current','DstWorkspace','Current'));
Y=e2;
X=yout;
function [Y, X] = simulPID(pid)
[~,~,yout] = sim('untitledPSO',[0 15], simset('solver','ode23tb','AbsTol',1e-6,'SrcWorkspace','Current','DstWorkspace','Current'));
Y=e3;
X=yout;

Respuestas (1)

Walter Roberson
Walter Roberson el 2 de Mzo. de 2019
Before each call to sim(), assign some value (any value) to each variable that your model outputs -- which appears to be at least e1, e2, and e3 .
Note: you cannot have more than one function with the same name.
  8 comentarios
Mustafa Sabah
Mustafa Sabah el 5 de Mzo. de 2019
Because of the different data within each turbine 1, 2 and 3 They represent e1, e2, e3.
If you run a project only You will notice that the data in the workspace e1 e2 e3 is different
If you use one function, another error appears
error
Unable to perform assignment because the left and right sides have a different number of elements.
Error in
current_fitness(i) = simulPID(current_position(:,i));
Walter Roberson
Walter Roberson el 5 de Mzo. de 2019
Two of your three functions cannot be called.
Do you want to optimize all three turbines simultaneously with one set of parameters? Do you want to optimize them independently?
If you want to optimize them simultaneously, then what tradeoff are you willing to accept? Minimize the total of the three? Minimize the differences (looking for equal values) ? If one particular parameter lowers e1 by 2 but increases e2 by 1, should that parameter be accepted?
I still need Vnom and Pnom values in order to be able to run your code.

Iniciar sesión para comentar.

Categorías

Más información sobre Particle Swarm en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by