The code for P&O MPPT is as follows.
function [Y,Stage_out,Pold_out]=Perturb_and_Observe_MPPT(P_new,P_old,Delay,Yin,Stage_in)
c=0.05;
Pold_out=P_old;
Y=Yin;
Stage_out= Stage_in;
if Delay == 0
if Stage_in==0
if P_new>P_old
Y=Yin+c;
Pold_out=P_new;
elseif P_new<P_old
Y=Yin-c;
Stage_out=1;
Pold_out=P_new;
end
Pold_out=P_new;
end
if Stage_in==1
if P_new>P_old
Y=Yin-c;
Pold_out=P_new;
elseif P_new<P_old
Y=Yin+c;
Stage_out=0;
Pold_out=P_new;
end
Pold_out=P_new;
end
end