close all
clear all

Vemf = 40;

Rfets = .050;
Rw = .15;
Lw = .00087;

LineColorArray{1} = [ 0    0.5000         0];
LineColorArray{2} = [ 1.0000         0         0];
LineColorArray{3} = [ 0    0.7500    0.7500];
LineColorArray{4} = [ 0.7500         0    0.7500];

figure('Position',[1000 388 1509 1110],'PaperPositionMode','manual','PaperOrientation','landscape','PaperType','usletter','PaperPosition',[0 0 11 8.5]);
figure('Position',[1000 388 1509 1110],'PaperPositionMode','manual','PaperOrientation','landscape','PaperType','usletter','PaperPosition',[0 0 11 8.5]);

Vtarg = 300;
SpecificPower = [1 .5 .027 .011];%Watts per RPM

N = [50:50:6000];
for k = 1:length(SpecificPower)
   
if(k<=2)
Power = SpecificPower(k)*N;
leg_str{k} = ['Specific Power = ' num2str(SpecificPower(k)) ' W/RPM'];
    end

if(k>2)
Power = SpecificPower(k)*N.^1.5;
leg_str{k} = ['Power = ' num2str(SpecificPower(k)) '*N^{1.5}'];
end

Rload = Vtarg^2./Power;

Vg = 1.35*Vemf*N/1000;

Rreal = ones(1,length(N))*(Rw+Rfets);
Rcomplex = (Lw*2*pi*N*5/60);
Rabs = (Rcomplex.^2+Rreal.^2).^.5;

a = Vtarg*ones(1,length(N));
b = -Vg;
c = Rreal*Vtarg./Rload;
DbarReal = (-b+(b.^2-4.*a.*c).^.5)./(2*a);
idx = find(imag(DbarReal));

DbarReal = real(DbarReal);
DbarReal(idx) = NaN;

c = Rabs*Vtarg./Rload;
DbarAbs = (-b+(b.^2-4.*a.*c).^.5)./(2*a);
idx = find(imag(DbarAbs));

DbarAbs = real(DbarAbs);
DbarAbs(idx) = NaN;

VReal = Vg.*(1./DbarReal)./(1+Rreal./((DbarReal.^2).*Rload));
VAbs =  Vg.*(1./DbarAbs)./(1+Rabs./((DbarAbs.^2).*Rload));

EffReal = 1./(1+Rreal./((DbarReal.^2).*Rload));
EffAbs = 1./(1+Rabs./((DbarAbs.^2).*Rload));

figure(1)
subplot(2,1,1)
plot(N,DbarReal,'Color',LineColorArray{k},'LineWidth',2)
grid on;
title(['Winding Resistance Alone:  Rfets = ' num2str(Rfets) ' Ohms']);
xlabel('N (RPM)');
ylabel('Dbar');
hold all
subplot(2,1,2)
plot(N,DbarAbs,'Color',LineColorArray{k},'LineWidth',2)
title(['Including Impedance of Inductor:  Rfets = ' num2str(Rfets) ' Ohms']);
xlabel('N (RPM)');
ylabel('Dbar');
grid on;
hold all

figure(2)
subplot(2,1,1)
plot(N,EffReal,'Color',LineColorArray{k},'LineWidth',2)
grid on;
title(['Winding Resistance Alone:  Rfets = ' num2str(Rfets) ' Ohms']);
xlabel('N (RPM)');
ylabel('Efficiency');
hold all

subplot(2,1,2)
plot(N,EffAbs,'Color',LineColorArray{k},'LineWidth',2)
title(['Including Impedance of Inductor:  Rfets = ' num2str(Rfets) ' Ohms']);
xlabel('N (RPM)');
ylabel('Efficiency');
grid on;
hold all

end

figure(1)
subplot(2,1,1)
legend(leg_str,'Location','SouthEast');
subplot(2,1,2)
legend(leg_str,'Location','SouthEast');
figure(2)
subplot(2,1,1)
legend(leg_str,'Location','SouthEast');
subplot(2,1,2)
legend(leg_str,'Location','SouthEast');

print('-dpdf','OpenLoopEffEstimates');
