%% TSFS04: Elektriska drivsystem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TSFS04: Elektriska drivsystem. Script till Laboration 2.
% Laboration 2: skellettfil
% Datum: 111207
% Fyll i luckorna i koden där det står "Fyll i här", antingen med data eller 
% med beräkningar. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Uppgift 1: Plottning av varvtal vs moment %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

n_shunt=[];% Fyll i här
T_shunt=[];% Fyll i här
n_serie=[];% Fyll i här
T_serie=[];% Fyll i här
n_kompound=[];% Fyll i här
T_kompound=[];% Fyll i här

figure();   % Skapar ny figur
clf;        % Rensar figuren
hold on     % Används för att rita flera plottar i samma figur.

% Tips: Plotta med kommandot plot(...). 'help plot' ger info om möjliga färg/symbolval
%Fyll i här, plot 1
%Fyll i här, plot 2
%Fyll i här, plot 3
% Tips: För att skapa en legend används legend(...).
%Fyll i här
% Tips: För att sätta titel används title(...)
%Fyll i här


%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Uppgift 2: Polynomanpassning av magnetiseringsdatat %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Data från magnetiseringskurvan %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ea = []; %Fyll i här
n0 = NaN;%Fyll i här
n =  [];%Fyll i här
Ea0 = Ea.*n0./n; % Transformering till Ea0 motsvarande emk vid hastighet n0
If = []; %Fyll i här

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Data från övriga mätningar %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Fyll i här, med syfte att kunna beräkna Ea0_xxx nedan

Ea0_shunt    = []; %Fyll i här
Ea0_serie    = []; %Fyll i här
Ea0_kompound = []; %Fyll i här

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Polynomanpassning av magnetiseringskurvan %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Alternativ 1: Utgående från Ea0 = p1(If)
x = If;
y = Ea0;
gradtal1 = NaN; %Fyll i här 
p1 = polyfit(x,y,gradtal1); % Använd 'help polyfit' i Matlabterminalen
If_max = NaN; %Fyll i max förväntade ström.
% Gridning av polynomet. Används till linjär interpolation för att beräkna
% If_eff = If_eff(Ea0) senare
If_grid1 = 0:If_max/100:If_max;
Ea0_grid1 = polyval(p1,If_grid1); 

% Alternativ 2: Utgående från If = p2(Ea0)
x = Ea0;
y = If;
gradtal2 = NaN; %Fyll i här 
p2 = polyfit(x,y,gradtal2); % Använd 'help polyfit' i Matlabterminalen
Ea0_max = NaN; %Fyll i max förväntade EMK.
% Gridning av polynomet. Används till linjär interpolation för att beräkna
% If_eff = If_eff(Ea0) senare
Ea0_grid2 = 0:Ea0_max/100:Ea0_max; 
If_grid2 = polyval(p2,Ea0_grid2); 

% Generering av begärda figurer
figure(); clf;
plot(If_grid1,Ea0_grid1,'r--'); hold on
plot(If_grid2,Ea0_grid2,'k--'); 
%Fyll i här
%plot ... 
%plot ...
ylabel('Ankar emk E_{a0} [V]')
xlabel('Fältström genom shuntlindningen I_f [A]')
title('Fyll i här')
legend('Polynomanpassning alternativ 1','Polynomanpassning alternativ 2',%Fyll i här)

%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Uppgift 3: Skattning av parametrarna kAR och Ns/Nf %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Uträkning av shunt-ekvivalenta strömmarna If_eff för respektive uppkoppling
% Välj vilket griddata ni vill använda: 
% Ea0_grid1 - If_grid1 
% eller
% Ea0_grid2 - If_grid2
Ea0_grid = NaN % Fyll i här
If_grid  = NaN % Fyll i här
If_eff_shunt = interp1(Ea0_grid,If_grid, %Fyll i omräknade shuntspänningar);
If_eff_serie = %Fyll i här
If_eff_kompound = %Fyll i här

% Skattning av kAR och Ns/Nf. Kom ihåg att ladda ner estimateParam från
% kurshemsidan.
[kAR NsNf] = estimateParam(%Fyll i här); % Matlabterminal: 'help estimateParam'


% Räkna ut de modellerade ekvivalenta strömmarna för respektive uppkoppling
% TIPS: If_eff = If + Ns/Nf*Is - kAR*Ia.^2
% Fyll i här

% If_eff_shunt_mod = 
% If_eff_serie_mod = 
% If_eff_kompound_mod = 

figure(); clf;
plot(If_grid1,Ea0_grid1,'r--'); hold on
plot(If_grid2,Ea0_grid2,'k--');

% plot(If,Ea0, 'd' )
% plot(If_eff_shunt_mod,Ea0_shunt, 'x' )
% plot ...
% plot ...

% Fyll i här
xlabel('Fyll i här')
ylabel('Fyll i här')
title('Fyll i här')
legend('Fyll i här')
