function [Vmpp,Impp,Pmax] = MPPT(Ipv,Vpv,G)
ind=find(Ipv<0,1,'first');
if(isempty(ind))
ind=length(Ipv);
end;
I=Ipv(1:ind(1)-1);
V=Vpv(1:ind(1)-1);
% I=Ipv;
% V=Vpv;
I(end)=0;
% Powe Calculations
P=(I.*V)*(G/100);
L=length(P);
MPP=L;
Pmax=P(L);
Impp=I(L);
Vmpp=V(L);
for i=2:L
P_prv=P(i-1);
P_curr=P(i);
D=P_curr-P_prv;
if(D<0)
Pmax=P_curr;
MPP=i;
break;
end;
end;
Vmpp=V(MPP);
Impp=I(MPP);
Pmax;
% figure(3)
subplot(2,2,3)
plot(V,I),title('IV Plot');
xlabel('Volatage(V)');
ylabel('Current(A)');
hold on
plot(Vmpp,Impp,'r*')
hold off;
% figure(4)
subplot(2,2,4)
plot(V,P),title('PV Plot');
xlabel('Volatage(V)');
ylabel('Power(W)');
hold on
plot(Vmpp,Pmax,'r*')
hold off;
%%
D=0.5;
Dt=zeros(1,L);
for i=2:L
dV=V(i)-V(i-1);
dI=I(i)-I(i-1);
if(dV==0)
if(dI==0)
Dt(i)=D;
else
if(dI>0)
D=D+0.005;
Dt(i)=D;
else
D=D-0.005;
Dt(i)=D;
end
end;
else
if((dI/dV)==(-(I(i)/V(i))))
Dt(i)=D;
else
if((dI/dV)>(-(I(i)/V(i))))
D=D+0.005;
Dt(i)=D;
else
D=D-0.005;
Dt(i)=D;
end;
end;
end;
end;
DC=Dt(MPP);