%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Lektion 8                              %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Uppgift 8.1
clear
clc

% a) 

P = 145e6;
Va = 13.8e3/sqrt(3);
Ea = Va;
Xs = 1.266;
nph = 3;
f = 60;
p = 73;

% a) 
Pmax = nph*Va*Ea/Xs;
delta = asin(P/Pmax)*180/pi

% b) 
X = 0.14;
Pmax = nph*Va*Ea/(Xs+X)

%% Uppgift 8.2 
clear 
clc

P = 1100e3;
Va = 4600/sqrt(3);
f = 60;
nph = 3;
p = 4;
Xs = 22.7;
If0 = 85;

% a) 
n = f*60/(p/2)

% b)
Ia = P/(nph*Va)

% c)
P1 = 1e6;
Ia1 = P1/(nph*Va);
Ea1 = Va-j*Xs*Ia1;
If1 = If0 * abs(Ea1)/Va

%% d)

n2 = 1300;
fe2 = f*n2/n

P2 = P1*(n2/n)^2.5

Va2 = Va*(n2/n);
Ea2a = abs(Ea1)*(n2/n);
Xs2 = Xs*(n2/n); 
delta = asin(P2*Xs2/(nph*Va2*Ea2a));
delta*180/pi
Ea2 = Ea2a*exp(-j*delta);
Ia2 = (Va2-Ea2)/(j*Xs2);
pfa = angle(Ia2)
pfa*180/pi
abs(Ia2)
pf = cos(pfa)

% plots
n3 = [600:1800];

P3 = P1*(n3/n).^2.5;
figure(9)
plot(n3,P3*1e-3,'Linewidth',2)
xlabel('varvtal [rpm]')
ylabel('effekt [kW]')
grid


Va3 = Va*(n3/n);
Ea3a = abs(Ea1)*(n3/n);
Xs3 = Xs*(n3/n); 
delta = asin(P3.*Xs3./(nph*Va3.*Ea3a));
Ea3 = Ea3a.*exp(-j*delta);
Ia3 = (Va3-Ea3)./(j*Xs3);
pfa3 = angle(Ia3);
pf3 = cos(pfa3);
figure(10)
plot(n3,pf3,'Linewidth',2)
xlabel('varvtal [rpm]')
ylabel('effektfaktor [-]')
grid

% f)
Ia4 = P2/(nph*Va2);
Ea4 = sqrt(Va2^2+(Xs2*Ia4)^2);
If4 = If1*Ea4/abs(Ea2)

% plot
Ia5 = P3./(nph*Va3);
Ea5 = sqrt(Va3.^2+(Xs3.*Ia5).^2);
If5 = If1*Ea5/abs(Ea2);
plot(n3,If5,'Linewidth',2)
xlabel('varvtal [rpm]')
ylabel('fältström [A]')
grid