% Errors of predictions runs using polynomial fit and mini standardize methods
% Must run preisach_predict_SLsign or equivalent to get Preisach results. 

% Plot_strplt(39, 5);
Plot_strplt(27, 5);
Load_strplt(19); %  Prediction run around -0.6 amps
Load_strplt(20); % Prediction currents around 0.0 amps
Load_strplt(21); % Prediction currents around 1.8 amps
Load_strplt(22); % Prediction currents between -3 and 3 amps
Load_strplt(23); % Mini standardize version of run 19
Load_strplt(24); % Mini standardize version of run 20
Load_strplt(25); % Mini standardize version of run 21
Load_strplt(26); % 10% low of set value Mini standardize version of run 19
Load_strplt(28); % Mini standardize version of run 22

close all;

% Find the integrated gradient values at each of the predication run currents 
BL_poly19 = polyval(I_BL_up27,Strplt19.Imag);
BL_poly20 = polyval(I_BL_up27,Strplt20.Imag);
BL_poly21 = polyval(I_BL_up27,Strplt21.Imag);
BL_poly22 = polyval(I_BL_up27,Strplt22.Imag);
BL_poly23 = polyval(I_BL_up27,Strplt23.Imag);
BL_poly24 = polyval(I_BL_up27,Strplt24.Imag);
BL_poly25 = polyval(I_BL_up27,Strplt25.Imag);
BL_poly26 = polyval(I_BL_up27,Strplt26.Imag);
BL_poly28 = polyval(I_BL_up27,Strplt28.Imag);

% Calculate the errors between the predicted and actual values
Rerror19 = 100*(Strplt19.SL_sign - BL_poly19)./Strplt19.SL_sign;
Rerror20 = 100*(Strplt20.SL_sign - BL_poly20)./Strplt20.SL_sign;
Rerror21 = 100*(Strplt21.SL_sign - BL_poly21)./Strplt21.SL_sign;
Rerror22 = 100*(Strplt22.SL_sign - BL_poly22)./Strplt22.SL_sign;
Rerror23 = 100*(Strplt23.SL_sign - BL_poly23)./Strplt23.SL_sign;
Rerror24 = 100*(Strplt24.SL_sign - BL_poly24)./Strplt24.SL_sign;
Rerror25 = 100*(Strplt25.SL_sign - BL_poly25)./Strplt25.SL_sign;
Rerror26 = 100*(Strplt26.SL_sign - BL_poly26)./Strplt26.SL_sign;
Rerror28 = 100*(Strplt28.SL_sign - BL_poly28)./Strplt28.SL_sign;

%% Plot of data around -1.0 +/- 0.6 Amp
figure('units','normalized','outerposition',[0 0 0.9 0.9]) 
hold on
plot(Strplt19.Imag, Rerror19, mm(1))
plot(Strplt19.Imag, results.prediction(1).PercentError, mm(2))
plot(Strplt23.Imag, Rerror23, mm(3));

xlabel('Current (A)');
ylabel('% Error of Integrated Gradient');
title('% Error of Integrated Gradient vs Current, around -1.0  A');
legend('Polynomial Fit', 'Preisach Fits', 'Mini Standardize')
text(Strplt23.Imag(4),  rms(Rerror19), ['RMS of Polynomial Fit = ',num2str(rms(Rerror19))], 'color','b');
text(Strplt23.Imag(4),  0.9*rms(results.prediction(1).PercentError), ['RMS of Preisach Fit = ', num2str(rms(results.prediction(1).PercentError))], 'color','r');
text(Strplt23.Imag(4),  rms(Rerror23), ['RMS of Mini Standardize Fit = ',num2str(rms(Rerror23))], 'color','k');



% Add the text labels next to each point
labels = cellstr(num2str((1:length(Strplt19.Imag))'));
text(Strplt19.Imag, Rerror19*1.01, labels, 'VerticalAlignment', 'bottom', ...
                  'HorizontalAlignment', 'right', ...
                  'FontSize', 12, 'Color', 'blue');
enhance_plot_new;
saveas(gcf,'Percent Error of Integrated Gradient vs Current, around -1.0 A.png');

%% Plot of data around 0.0 +/- 0.6 Amp Full scale
figure('units','normalized','outerposition',[0 0 0.9 0.9]) 
hold on
plot(Strplt20.Imag, Rerror20, mm(1))
plot(Strplt20.Imag, results.prediction(2).PercentError, mm(2))
plot(Strplt24.Imag, Rerror24, mm(3))

xlabel('Current (A)');
ylabel('% Error of Integrated Gradient');
title('% Error of Integrated Gradient vs Current, around 0.0 A Full scale');
legend('Polynomial Fit', 'Preisach Fits','Mini Standardize')
text(Strplt20.Imag(8), 600, ['RMS of Polynomial Fit = ',num2str(rms(Rerror20))], 'color', 'b');
text(Strplt20.Imag(8), 400, ['RMS of Preisach Fit = ',num2str(rms(results.prediction(2).PercentError))], 'color', 'r');
text(Strplt24.Imag(8), 250, ['RMS of Mini Standardize Fit = ',num2str(rms(Rerror24))], 'color', 'k');


% Add the text labels next to each point
labels = cellstr(num2str((1:length(Strplt20.Imag))'));
text(Strplt20.Imag, Rerror20*1.01, labels, 'VerticalAlignment', 'bottom', ...
                  'HorizontalAlignment', 'right', ...
                  'FontSize', 12, 'Color', 'blue');
enhance_plot_new;
saveas(gcf,'% Error of Integrated Gradient vs Current, around 0.0 A Full scale.png');

%% Plot of data around 0.0 +/- 0.6 Amp, Data between +/- 0.1 A excluded
figure('units','normalized','outerposition',[0 0 0.9 0.9]) 
hold on
plot(Strplt20.Imag, Rerror20, mm(1))
plot(Strplt20.Imag, results.prediction(2).PercentError, mm(2))
plot(Strplt24.Imag, Rerror24, mm(3))

str20_logic = abs(Strplt20.Imag) > 0.1;

ylim([-50,50])
xlabel('Current (A)');
ylabel('% Error of Integrated Gradient');
title('% Error of Integrated Gradient vs Current, around 0.0 A, Data between +/- 0.1 A excluded');
legend('Polynomial Fit', 'Hybrid Preisach Fits','Mini Standardize')
text(Strplt20.Imag(8), 22, ['RMS of Polynomial Fit = ',num2str(rms(Rerror20(str20_logic)))], 'color', 'b');
text(Strplt20.Imag(8), 16, ['RMS of Preisach Fit = ',num2str(rms(results.prediction(2).PercentError(str20_logic)))], 'color', 'r');
text(Strplt24.Imag(8), 10, ['RMS of Mini Standardize Fit = ',num2str(rms(Rerror24(str20_logic)))], 'color', 'k');


% Add the text labels next to each point
labels = cellstr(num2str((1:length(Strplt20.Imag))'));
text(Strplt20.Imag, Rerror20*1.01, labels, 'VerticalAlignment', 'bottom', ...
                  'HorizontalAlignment', 'right', ...
                  'FontSize', 12, 'Color', 'blue');
enhance_plot_new;
saveas(gcf,'Percent Error of Integrated Gradient vs Current, around 0.0 A.png');

%% Plot of data around 1.8 +/- 0.6 Amps
figure('units','normalized','outerposition',[0 0 0.9 0.9]) 
hold on
plot(Strplt21.Imag, Rerror21, mm(1))
plot(Strplt21.Imag, results.prediction(3).PercentError, mm(2))
plot(Strplt25.Imag, Rerror25, mm(3));

text(1.95, 4.7, ['RMS of Polynomial Fit = ',num2str(rms(Rerror21))], 'color', 'b');
text(1.95, 4, ['RMS of Preisach Fit = ',num2str(rms(results.prediction(3).PercentError(1:end)))], 'color', 'r');
text(1.95, 3.5, ['RMS of Mini Standardize Fit = ',num2str(rms(Rerror25))], 'color', 'k');

xlabel('Current (A)');
ylabel('% Error of Integrated Gradient');
title('% Error of Integrated Gradient vs Current, around 1.8 A');
legend('Polynomial Fit', 'Hybrid Preisach Fits', 'Mini Standardize')

% Add the text labels next to each point
labels = cellstr(num2str((1:length(Strplt21.Imag))'));
text(Strplt21.Imag, Rerror21*1.01, labels, 'VerticalAlignment', 'bottom', ...
                  'HorizontalAlignment', 'right', ...
                  'FontSize', 12, 'Color', 'blue');

enhance_plot_new;
saveas(gcf,'Percent Error of Integrated Gradient vs Current, around 1.8 A.png');

%% Plot of data between -3 and 3 Amps
figure('units','normalized','outerposition',[0 0 0.9 0.9]) 
hold on
plot(Strplt22.Imag, Rerror22, mm(1))
plot(Strplt22.Imag, results.prediction(4).PercentError, mm(2))
plot(Strplt28.Imag, Rerror28, mm(3));

text(Strplt22.Imag(6),10, ['RMS of Polynomial Fit = ',num2str(rms(Rerror22))], 'color', 'b');
text(Strplt22.Imag(6),7.5, ['RMS of Preisach Fit = ',num2str(rms(results.prediction(4).PercentError))], 'color', 'r');
text(Strplt22.Imag(6), 5, ['RMS of Mini Standardize Fit = ',num2str(rms(Rerror28))], 'color', 'k');


xlabel('Current (A)');
ylabel('% Error of Integrated Gradient');
title('% Error of Integrated Gradient vs Current, -3 to 3 Amps');
legend('Polynomial Fit', 'Hybrid Preisach Fits', 'Mini Standardize')
% Add the text labels next to each point
labels = cellstr(num2str((1:length(Strplt22.Imag))'));
text(Strplt22.Imag, Rerror22*1.01, labels, 'VerticalAlignment', 'bottom', ...
                  'HorizontalAlignment', 'right', ...
                  'FontSize', 12, 'Color', 'blue');
enhance_plot_new;
saveas(gcf,'Percent Error of Integrated Gradient vs Current, between -3 and 3 A.png');