% example of ray tracing for a two lens system using symbolic algebra
% define lenses
syms m f1 f2
f2 = m*f1

% define distances between object and first lens, distance between first
% lens and second lens, and distance between second lens and image
d1 = f1;
d2 = f1+f2;
d3 = f2;

% these are the matrices that describe how a ray propogates
Md1 = [1 d1;0 1];
Md2 = [1 d2;0 1];
Md3 = [1 d3;0 1];
Mlens1 = [1 0;-1/f1 1];
Mlens2 = [1 0;-1/f2 1];

% rays are described by a two component vector made from the displacement from the
% optical axis and the angle with respect to the optical axis
% set this up for five rays that start out on the optical axis but have
% diffferent angles with respect to the optical axis
% make the position of the starting point a symbolic variable too
syms eta;
Rin = [eta*ones(1,5);-0.1:0.05:0.1]

% propogate the rays by matrix multiplication
R1 = Md1*Rin;   % after displacement of the distance to the first lens
R2 = Mlens1*R1; % after the first lens
R3 = Md2*R2;    % after displacement of the distance to the second lens
R4 = Mlens2*R3; % after  the second lens
Rout = Md3*R4  % after displacement from the second lens to the detector
% Rout looks complicated now but after "simplify" this nicely shows that the
% the final position of the ray is increased by a factor m and the final angle of
% the ray is reduced by a factor of m
simplify(Rout)

