% Problem 1: Design lenses using geometrical optics (Snells’ law). Assume parallel rays and show that they converge (if they converge) to a single point. You may use Snell’s law directly or the ray tracing matrices you learned on the tutorial.
%% Task 1: A large lens (10 mm) with a focal distance much larger than its diameter (f=10 cm)

o=40; %the object's distance from the lens
f=10; %focal length of the lens
s=abs((o*f)/(o-f))*2; %distance of the screen

nmin=1.0;
nmax=1.05;
d=1; 
r=d/2;  
x=linspace(-r,r,100);
n=nmin+(nmax-nmin)*(1-x.^2./r^2);

if(s==1/0)  
    s=2*f;
end

T_o=[1, o; 0, 1]; %translation matrix of the object
T_s=[1, s; 0, 1]; %translation matrix of the screen
L=[1, 0; (nmin-nmax)./r*nmax, nmin/nmax]; %transfer matrix of the lens

rays=linspace(-r, r, 5);
angle=0*rays; %the rays are parallel  

v_0=[rays; angle]; 
v_1=T_o*v_0; 
v_2=L*v_1;  
v_3=T_s*v_2; 

figure(1);

subplot(2,2,1);

for i = 1:length(angle)
    plot([-o,0,n(i*10)-1,s], [v_0(1,i),v_1(1,i),v_2(1,i),v_3(1,i)], 'r-');
    hold on;
end

grid on;
xlabel('x axis');
ylabel('y axis');
title('Large lens (10 mm), f=10 cm');

plot(n(:)-1,x,'k.-') %plot lens
plot([-f,f],[0,0],'k*','linewidth',1) 
plot([-o-f,s],[0,0],'k-') %plot focal point

hold off;
clear all;
%% Task 2: A large lens (10 mm) with a focal distance only 1.5 times its diameter (f=15 mm)

o=2; %the object's distance from the lens
f=1.5; %focal length of the lens
s=abs((o*f)/(o-f))*2; %distance of the screen 

nmin=1.0;
nmax=1.05;
d=1;
r=d/2;
x=linspace(-r,r,100);
n=nmin+(nmax-nmin)*(1-x.^2./r^2);

if(s==1/0)
    s=2*f;
end

T_o=[1, o; 0, 1]; %translation matrix of the object
T_s=[1, s; 0, 1]; %translation matrix of the screen
L=[1, 0; (nmin-nmax)./r*nmax, nmin/nmax]; %transfer matrix of the lens

rays=linspace(-r,r,10);
angle=0*rays;  

v_0=[rays; angle]; 
v_1=T_o*v_0; 
v_2=L*v_1;
v_3=T_s*v_2; 

subplot(2,2,2);

for i = 1:length(angle)
    plot([-o,0,n(i*5)-1,s+f], [v_0(1,i),v_1(1,i),v_2(1,i),v_3(1,i)], 'r-');
    hold on;
end

grid on;
xlabel('x axis');
ylabel('y axis');
title('Large lens (10 mm), f=15 mm');

plot(n(:)-1,x,'k.-') %plot lens
plot([-f,f],[0,0],'k*','linewidth',1) 
plot([-o-f,s],[0,0],'k-') %plot focal point

hold off;
clear all;
%% Task 3: A  small  lens  (20  µm)  with  a  focal  distance  much  larger  than  its  diameter (f=200 µm)

o=10; %the object's distance from the lens
f=20; %focal length of the lens
s=abs((o*f)/(o-f))*2;  % distance of the screen

nmin = 1.0;
nmax = 1.1;
d = 2;
r = d/2;
x = linspace(-r,r,100);
n = nmin + (nmax-nmin)*(1-x.^2./r^2);

if(s==1/0)  
    s=2*f;
end

T_o=[1, o; 0, 1]; %translation matrix of the object
T_s=[1, s; 0, 1]; %translation matrix of the screen
L=[1, 0; (nmin-nmax)./r*nmax, nmin/nmax]; %transfer matrix of the lens

rays=linspace(-r,r,10);
angle=0*rays; 

v_0=[rays; angle]; 
v_1=T_o*v_0; 
v_2=L*v_1; 
v_3=T_s*v_2;

subplot(2,2,3);

for i = 1:length(angle)
    plot([-o,0,n(i*5)-1,s], [v_0(1,i),v_1(1,i),v_2(1,i),v_3(1,i)], 'r-');
    hold on;
end

grid on;
xlabel('x axis');
ylabel('y axis');
title('Small lens (20 µm), f=200 µm');

plot(n(:)-1,x,'k.-') %plot lens
plot([-f,f],[0,0],'k*','linewidth',1) 
plot([-o-f,s],[0,0],'k-') %plot focal point

hold off;
clear all;
%% Task 4: A small lens (20 µm) with a focal distance only 1.5 times its diameter (f=30 µm)
 
o=6; %the object's distance from the lens
f=3; %focal length of the lens
s=abs((o*f)/(o-f))*2; %distance of the screen 

nmin = 1.0;
nmax = 1.1;
d = 2;  %diameter
r = d/2;  %radius
x = linspace(-r,r,100);
n = nmin + (nmax-nmin)*(1-x.^2./r^2);

if(s==1/0)  
    s=2*f;
end

T_o=[1, o; 0, 1]; %translation matrix of the object
T_s=[1, s; 0, 1]; %translation matrix of the screen
L=[1, 0; (nmin-nmax)./r*nmax, nmin/nmax]; %transfer matrix of the lens

rays=linspace(-r,r,5);
angle=0*rays; 

v_0=[rays; angle]; 
v_1=T_o*v_0;
v_2=L*v_1;
v_3=T_s*v_2;

subplot(2,2,4);

for i = 1:length(angle)
    plot([-o,0,n(i*5)-1,s+f], [v_0(1,i),v_1(1,i),v_2(1,i),v_3(1,i)], 'r-');
    hold on;
end

grid on;
xlabel('x axis');
ylabel('y axis');
title('Large lens (20 µm), f=30 µm');

plot(n(:)-1,x,'k.-') %plot lens
plot([-f,f],[0,0],'k*','linewidth',1) 
plot([-o-f,s],[0,0],'k-') %plot focal point

hold off;
clear all;