% --- DATA -------------------------------------------------------
% throttle = 20,30,…,80 %
throttle = [20 30 40 50 60 70 80];

% rpmData: one row one measurement
rpmData = [
    530 670 760 820 860 890 910;
    520 670 750 820 860 890 910;
    520 670 760 820 860 880 910;
    530 670 750 810 860 880 910;
    520 670 760 820 860 880 910;
    530 670 750 810 860 880 910;
    520 670 760 820 860 880 910;
    520 670 760 810 850 890 910;
    520 670 760 820 860 890 910;
];

% --- CALC ---------------------------------------------------
meanRpm = mean(rpmData, 1);        
stdRpm  = std( rpmData, 0, 1);      

% linear fit
p1     = polyfit(throttle, meanRpm, 1);
rpmLin = polyval(p1, throttle);

% quadric fit
p2      = polyfit(throttle, meanRpm, 2);
rpmQuad = polyval(p2, throttle);

% R^2 for both models
SSres1 = sum((meanRpm - rpmLin ).^2);
SSres2 = sum((meanRpm - rpmQuad).^2);
SStot  = sum((meanRpm - mean(meanRpm)).^2);
R21    = 1 - SSres1/SStot;
R22    = 1 - SSres2/SStot;

% --- GRAPH -------------------------------------------------------
figure; hold on; grid on
errorbar(throttle, meanRpm, stdRpm, 'o', 'LineWidth',1.2);
plot(throttle, rpmLin, '--',  'LineWidth',1.5);
plot(throttle, rpmQuad,'-.',  'LineWidth',1.5);
xlabel('Throttle [%]');
ylabel('RPM');
title('Statická charakteristika Throttle');
legend('Průměr ±1·σ','Lineární fit','Kvadratický fit','Location','best');

txt = sprintf('R^2_{lin}=%.3f, R^2_{quad}=%.3f', R21, R22);
text(25, 600, txt, 'FontSize',10, 'BackgroundColor','w');

hold off
