%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Lektion 5                              %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Uppgift 5.2
clear
clc

% a)
P = [0 10 25]*1e3;
Ra = 0.14;
Va = 250;
n0 = 1200;
K = 150;
If = 1.67;
Ea = Va/2+sqrt(Va^2/4-Ra*P)
n = n0*Ea/(K*If)

%% b)
If1 = Ea/K

%% c) 
P = [0:0.1:25]*1e3;
Ra = 0.14;
Va = 250;
n0 = 1200;
Kf = 150/n0;
If = 1.67;
Ea = Va/2+sqrt(Va^2/4-Ra*P);
n = Ea/(Kf*If);
plot(P/1e3,n)
xlabel('Effekt [kW]')
ylabel('Hastighet [rpm]')

%% d)
If1 = Ea/(Kf*n0)
plot(P/1e3,If1)
xlabel('Effekt [kW]')
ylabel('Fältström [A]')

%% Uppgift 5.4
clc
clear

Y = [375 400 425 450 475]; % emk V
X = [227 260 301 350 402]; % A fältström

V0 = 600;
I0 = 250;
n0 = 600; 
n1 = 400;
Is = 460;
R = 0.125;

% Bestäm k från märkfall
Ea0 = V0-I0*R
Ea1 = n1/n0*Ea0
Ieff = spline(Y,X,Ea1)
k = (I0-Ieff)/I0^2

% Bestäm T för startström
Iseff = Is-k*Is^2
Eas1 = spline(X,Y,Iseff)
w1 = n1*2*pi/60
T = Eas1/w1*Is

%% Plot
xx = [227:0.1:402];

plot(xx,spline(X,Y,xx),'LineWidth',2)
xlabel('Seriefältströmmen I_{eff} [A]')
ylabel('Genererad emk E_{a0} [V]')
title('Magnetiseringskurva för n_0 = 400 rpm')
grid
