% show that Snell's law satisifes Fermat's principle
% specify refractive indices and incident angle
n1 = 1.0;
n2 = 1.45;
I1 = pi/4;
% calculate exit angle using Snell's law and calculate positions for unit
% distances
I2=asin(n1/n2*sin(I1));
x1 = -sin(I1);
x2 = sin(I2);
y1 = cos(I1);
y2 = -cos(I2);
% calculate the physical distances for rays that intersect the interface 
% at position displaced by a distance x along the interface.
% x is a column vector with values centered around 0.
x=[-0.1:0.01:0.1];
d1 = sqrt((x1-x).^2+y1.^2);
d2 = sqrt((x2-x).^2+y2.^2);
% calculate the optical path length
optical_path = n1*d1+n2*d2;
%  make a basic plot
plot(x,optical_path,'o-')
%  label the plot and set the axis scales
xlabel('x (m)')
ylabel('optical path length (m)')
axis([-0.1 0.1 2.44 2.47])
