

%%

PSLvals = load('PSL_rotStageCalib.txt');


%%

calibFunc = @(x,xdata)x(1)*(sin(x(2)*(xdata-x(3))*pi/180)).^2+x(4);
x0 = [65, 2, -25, 0];
xdata = PSLvals(:,2); % measured angles
ydata = PSLvals(:,3); % measured powers
x = lsqcurvefit(calibFunc,x0,xdata,ydata)


% Can't have negative offset
if x(4) < 0
    x(4) = 0;
end

th = linspace(-90,90,1000);
CalcPow = x(1)*(sin(x(2)*(th-x(3))*pi/180)).^2+x(4);

%%
plot(AngPow(:,1), AngPow(:,2),'o',...
    th, CalcPow)

grid on
legend('Measured Values', ...
    [num2str(x(1),4) ' * sin^2(' num2str(x(2),3) '*(theta-' num2str(x(3),4) ')*pi/180) + ' num2str(x(4),3)],...
    'location','northeast')
xlabel('Rotation Stage Angle [deg]')
ylabel('Power into vacuum [W]')
title('PSL rotation stage calibration')
















