clear
n1 = 1;
n2 = 1.5;
D = 10;
R = D./2;
d = 10;
lopt = n1.*sqrt(d.^2 + R.^2);
Y = linspace(0, R, 1000);
X = linspace(0, d, 1000);
for ii = 1:length(Y)
LoptTemp = zeros(length(Y), length(Y));
L1(:,ii) = sqrt( (d-X).^2 + Y(ii).^2)*n1;
L2(:,ii) = X*n2;
LoptTemp(:,ii) = L1(:,ii) + L2(:,ii);
diffii(:,ii) = abs(LoptTemp(:,ii) - lopt);
[difference minIndex(ii)] = min(diffii(:,ii));
x_fit(ii) = X(minIndex(ii));
end
figure
plot(x_fit, Y, 'black');
hold on
plot(x_fit.*-1, Y, 'black');
hold on
plot(x_fit, -Y, 'black');
hold on
plot(x_fit.*-1, -Y, 'black');
hold on