MATLAB: The code doesn’t get into the If statement

algorithmif statement

So my code doesn't get into my first if statment for some reason even though it's valid check the screen shot for Counter and Sc values
% function for Perturb and Observe MPPT algorithm
function k_values=P_and_O_Final(sc,Vc,k,RL)
counter=sc-4;
if counter>1 && counter<sc-4
I_next_1=(Vc(sc+1))/RL;
I_next_2=(Vc(sc+4))/RL;
P_next_1=Vc(sc+1)*I_next_1;
P_next_2=Vc(sc+4)*I_next_2;
I_actual=(Vc(sc))/RL; % The input current value in the current cycle
I_prev_1=(Vc(sc-1))/RL;
I_prev_2=(Vc(sc-4))/RL; % The input current value in the previous cycle
P_actual=Vc(sc)*I_actual; % to calculate the power value for the current cycle
P_prev_1=Vc(sc-1)*I_prev_1;
P_prev_2=Vc(sc-4)*I_prev_2; % To calculate the power value for the previous cycle
delta_V= Vc(sc+1)- Vc(sc) ;
delta_V1= Vc(sc+4)-Vc(sc);
delta_P= P_next_1 - P_actual;
delta_P1= P_next_2 - P_actual;
delta_V_1= Vc(sc)-Vc(sc-4 );
delta_V_2= Vc(sc)-Vc(sc-1);
delta_P_1= P_actual-P_prev_2;
delta_P_2= P_actual-P_prev_1;
if delta_P>0.0001
if delta_V>0
k=k-0.00002;
else
k=k+0.00002;
end
end
if delta_P1 >0.0001
if delta_V1>0
k=k-0.00002;
else
k=k+0.00002;
end
end
% 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.0001)
if delta_V_1>0
k=k+0.00002;
else
k=k-0.00002;
end
if delta_P_2 <(-0.0001)
if delta_V_2>0
k=k+0.00002;
else
k=k-0.00002;
end
end
end
end
k_values=k % used to store new k in the output
end

Best Answer

The problem seems to be here:
counter=sc-4;
if counter>1 && counter<sc-4
If you set "counter" to "sc-4" the following condition can't be statisfied
counter<sc-4
The problem is that you used the "&&" operator and it means that you need to satisfy both conditions.