qq_32529409
qq_32529409
采纳率0%
2021-02-28 12:45

这是我改的灰狼算法求光伏mppt为啥总是报错说未定义变量啊我是按照底下粒子群改的

function [D,iteration1] = PGWO(Vpv,Ipv)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% JKD Power and Energy Solutions
%% Grey Wolf optimization based solar PV MPPT 
%% For mutiple shadding patterns
dim = 1;
m = 5;
persistent dc
persistent fitness
persistent D_new;
persistent alpha;
persistent alpha_pos Beta_pos ;
persistent iteration Beta_score Delta_score Delta_pos;
persistent Gmax;
persistent i
persistent tol
if(isempty(iteration))
    iteration=0;
    tol=100;
end
if(isempty(D_new))
    D_new = 0.5;
end
% if(isempty(Gbest))
%     Gbest = 0.5;
% end
if(isempty(Gmax))
    Gmax=0;
end
if(isempty(alpha))
    alpha=0;
end
if (isempty(Beta_pos))
    alpha_pos=zeros(1,dim);
    Beta_pos=zeros(1,dim);
    Delta_pos=zeros(1,dim);
    Beta_score=0;
    Delta_score=0;
end
if(isempty(alpha))
    alpha=0;
end
if (isempty(alpha_pos))
    alpha_pos=zeros(1,dim);
end

if(isempty(dc))
    dc=zeros(5,1);
    
    dc(1)=0.1;
    dc(2)=0.2;
    dc(3)=0.3;
    dc(4)=0.4;
    dc(5)=0.5;
%     dc(6)=0.6;
%     dc(7)=0.7;
%     dc(8)=0.8;
%     dc(9)=0.9;
%     dc(10)=1;
end
if (isempty(i))
    i=0;
    D = 0.5;
end
% D = 0.5;
iteration1 = 0;
if (iteration <100 )
        iteration1=iteration;
        
    if i<5
        i = i+1;
        Flag4ub=dc(i,:)>1;
        Flag4lb=dc(i,:)<0;
        dc(i)=(dc(i).*(~(Flag4ub+Flag4lb)))+1.*Flag4ub;               
        
        fitness=Vpv*Ipv;
        tol = abs(alpha-fitness);
        if fitness>alpha 
            alpha=fitness; % Update alpha
            alpha_pos=dc(i);
        end
        if fitness<alpha && fitness>Beta_score 
            Beta_score=fitness; % Update beta
            Beta_pos=dc(i);
        end
        
        if fitness<alpha && fitness<Beta_score && fitness>Delta_score 
            Delta_score=fitness; % Update delta
            Delta_pos=dc(i);
        end
        D_new =dc(i);
        D = dc(i);
        return;
        
    end
    iteration = iteration+1;
    i = 0;
   d_alpha=updateal(alpha_pos,dc);
   x1=updateX1(alpha_pos,d_alpha);
   d_beta=updatebe(beta_pos,dc);
   x2=updateX2(Beta_pos,d_beta);
   d_delta=updatede(Delta_pos,dc);
   x3=updateX3(Delta_pos,d_delta);
   dc=x1+x2+x3/3;
end
D = dc;
end
function D_alpha=updateal(Alpha_pos,DC)

             
            r2=rand(); % r2 is a random number in [0,1]
            
            
            C1=2*r2; % Equation (3.4)
            
            D_alpha=abs(C1*Alpha_pos-DC);
end        
  function X=updateX1(Alpha_pos,D_alpha)
  r1=rand();
  a=2-iteration*((2)/100);
  A1=2*a*r1-a;
  X1=Alpha_pos-A1*D_alpha;
  X=X1;
  end
  function D_beta=updatebe(beta_pos,DC)

             r2=rand();
            C2=2*r2; % Equation (3.4)
            
            D_beta=abs(C2*beta_pos-DC); % Equation (3.5)-part 2
           
end        
  function X=updateX2(beta_pos,D_beta)
  r1=rand();
  a=2-iteration*((2)/100);
  A2=2*a*r1-a;
  X2=beta_pos-A2*D_beta;
  X=X2;
  end
function D_delta=updatede(delta_pos,DC)
 r2=rand();
 C3=2*r2; % Equation (3.4)
        
 D_delta=abs(C3*delta_pos-DC);
end
 function X=updateX3(delta_pos,D_delta)
  r1=rand();
  a=2-iteration*((2)/100);
  A2=2*a*r1-a;
  X3=delta_pos-A2*D_delta;
  X=X3;
 end
 

 %update velocity and duty cycle
    v(1)=updatevelocity(v(1),pbest(1),dc(1),gbest)
    v(2)=updatevelocity(v(2),pbest(2),dc(2),gbest)
    v(3)=updatevelocity(v(3),pbest(3),dc(3),gbest)
    v(4)=updatevelocity(v(4),pbest(4),dc(4),gbest)
    %update duty cycle
    dc(1)=updateduty(dc(1),v(1))
    dc(2)=updateduty(dc(2),v(2))
    dc(3)=updateduty(dc(3),v(3))
    dc(4)=updateduty(dc(4),v(4))
    
    return;
    
else
    u
    y='hello'
    D=0.1
    
end


end
function vfinal=updatevelocity(velocity,pobest,d,gwbest)
% PSO Parameters
w=0.4;
%w=(1.1-(pobest/gwbest));            % Inertia Weight
             % Inertia Weight Damping Ratio
c1=1.2;         % Personal Learning Coefficient
c2=2;         % Global Learning Coefficient

% If you would like to use Constriction Coefficients for PSO,
% uncomment the following block and comment the above set of parameters.

% % Constriction Coefficients
% phi1=2.05;
% phi2=2.05;
% phi=phi1+phi2;
% chi=2/(phi-2+sqrt(phi^2-4*phi));
% w=chi;          % Inertia Weight
% wdamp=1;        % Inertia Weight Damping Ratio
% c1=chi*phi1;    % Personal Learning Coefficient
% c2=chi*phi2;    % Global Learning Coefficient
vfinal = (w*velocity)+(c1*rand(1)*(pobest-d))+(c2*rand(1)*(gwbest-d));
end
function dfinal=updateduty(d,velocity)
dup=d+velocity;
if(dup>1)
    dfinal=1;
elseif(dup<0)
    dfinal=0;
else
    dfinal=dup;
end
end

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • LeonLeungLzc L____z__超 13天前

    兄弟,问题解决了吗?

    点赞 评论 复制链接分享