mardi 11 août 2020

why my if statement does not change my k value

I am trying to develop a new P&O algorithm. where I compare 2 points before and after my current power value. in my old code, I was just comparing 2 points (the actual power value and the previous one)and my code was working perfectly fine but when I tried to change the code to compare 2 points after and 2 points before the actual power value, it does not work. my old code flow chart p&O algorithm flow chart

and here is my old code which is working perfectly.

function k_values=P_and_O(sc,Vi,k)
I_prev=Newton_pv(Vi(sc-1));  % The input current value in the previous cycle
I_actual=Newton_pv(Vi(sc));    % The input current value in the current cycle
P_prev=Vi(sc-1)*I_prev;      % To calculate the power value for the previous cycle
P_actual=Vi(sc)*I_actual;        % to calculate the power value for the current cycle
delta_V=Vi(sc)-Vi(sc-1);         % to calculate the difference between current voltage value and the previous one.
delta_P=P_actual-P_prev;           % to calculate the difference between current power value and the previous one.
%  if statment used to ecide whether to increase or decrease duty ratio(k) value
% depending on voltage and Power  difference values. 
if delta_P>0.001
   if delta_V>0 
   k=k-0.0001;
   else
   k=k+0.0001;
   end
elseif delta_P<(-0.001)
    if delta_V>0
    k=k+0.0001;
    else
    k=k-0.0001;   
    end
end
k_values=k;    % used to store new k in the output
end

` my new code which is not working

function k_values=P_and_O_NEW(sc,Vi,k)
I_actual=Newton_pv(Vi(sc));    % The input current value in the current cycle 
I_prev_1=Newton_pv(Vi(sc-1));
I_prev_2=Newton_pv(Vi(sc-2));  % The input current value in the previous cycle
P_actual=Vi(sc)*I_actual;        % to calculate the power value for the current cycle
P_prev_1=Vi(sc-1)*I_prev_1; 
P_prev_2=Vi(sc-2)*I_prev_2;      % To calculate the power value for the previous cycle
%I_next_1=Newton_pv(Vi(z+1));
%I_next_2=Newton_pv(Vi(z+2));
%P_next_1=Vi(z+1)*I_next_1; 
%P_next_2=Vi(z+2)*I_next_2; 
if sc <= length(Vi) - 2
 I_next_1=Newton_pv(Vi(sc+1));
 I_next_2=Newton_pv(Vi(sc+2));  
 P_next_1=Vi(sc+1)*I_next_1; 
 P_next_2=Vi(sc+2)*I_next_2;
 delta_V= Vi(sc+1)- Vi(sc) ;
 delta_V1=  Vi(sc+2)-Vi(sc);
 delta_P= P_next_1 - P_actual;
 delta_P1= P_next_2 - P_actual;
 if  delta_P>0.001
   if delta_V>0 
   k=k-0.0001;
   else
   k=k+0.0001;
 if delta_P1 >0.001
     if delta_V1>0 
   k=k-0.0001;
   else
   k=k+0.0001;
     end
 end
   end
   
else
  delta_V_1= Vi(sc)-Vi(sc-4 );
  delta_V_2= Vi(sc)-Vi(sc-1);
  delta_P_1= P_actual-P_prev_2;
  delta_P_2= P_actual-P_prev_1;

  % delta_P1= [P_actual-P_prev_2,P_actual-P_prev_1]
  %delta_V1=[Vi(z)-Vi(z-2) , Vi(z)-Vi(z-1)]
%delta_V=Vi(z)-Vi(z-1);         % to calculate the difference between current voltage value and the previous one.
%delta_P=P_actual-P_prev;           % to calculate the difference between current power value and the previous one.
%delta_V=[Vi(z)-Vi(z-2), Vi(z)-Vi(z-1), Vi(z)-Vi(z+1),Vi(z)-Vi(z+2)]
%delta_P=[P_actual-P_prev_2,P_actual-P_prev_1,P_actual-P_next_1,P_actual-P_next_2]
%  if statment used to ecide whether to increase or decrease duty ratio(k) value
% depending on voltage and Power  difference values. 


    if delta_P_1<(-0.001)
    if delta_V_1>0
    k=k+0.0001;
    else
    k=k-0.0001;
    if delta_P_2 <(-0.001)
        if delta_V_2>0
    k=k+0.0001;
    else
    k=k-0.0001;
        end
    end
    end
    end
    
end

end
k_values=k;    % used to store new k in the output
end

Aucun commentaire:

Enregistrer un commentaire