data = load('conversion_eff.txt');

x = data(:,1);
y = data(:,2);
%z = data(:,3);

%%%%% FIT PARAM %%%%%
T = 0.1; %input cplr trans
P1 = x;
P2 = y;
r = P2./P1;
%ENL=0.224e-3/(0.333)^2; %sheila's number
ENL = 0.0055; %=0.0055, 2.5 times better than Sheila's number, based on outputs at 100mW
L = 0.015; %0.015 linear loss, I should be able to calculate this from Finesse and T. Not sure how.
%L=1-0.9985+0.02; %sheila's number
%e = [ ( 4.*T.*sqrt(ENL.*P1) ) ./ ( 2-sqrt(1-T).*(2-L-sqrt(e.*ENL.*P1) )).^2].^2;

syms eta;
for i=1:length(P1);
    ans=solve(eta-4*T*sqrt(ENL*P1(i))/(2-sqrt(1-T)*(2-L-sqrt(eta*ENL*P1(i))))^2, eta);
    ETA(i) = double(vpa(ans(1)));
end

%eqn = ([ ( 4.*T.*sqrt(ENL.*P1) ) ./ ( 2-sqrt(1-T).*(2-L-sqrt(e.*ENL.*P1) )).^2].^2)-sqrt(e) == 0 ;
%sol = solve(eqn);
%sole = fsolve(eqn,e);

%m=3.53;
%c=25.8;
%x1= linspace(-2,2.5, 5);
%y1 = m*x+c;

figure(1)
plot(x,y./x, '*r', 'LineWidth',2)
xlim([0,0.5])
%title('AOM1','FontSize', 30,'FontWeight','normal') 
xlabel('1064 Input power (Watts)', 'FontSize', 30)
ylabel('Conversion Efficiency','FontSize', 30)
hold on 
plot(x,ETA, '--', 'LineWidth',2);
legend({'Measurement','Fit (Enl = 0.0055, L = 0.014)'},'FontSize',18,'Location','northwest');
hold off

%ylim([0,2])
%legend({''},'FontSize',18,'Location','northwest');