%function [ ym um ] = computeMariginalRay(entPupilHeight,entPupilPosition,initialThick)
%computeMariginalRay:  computes paraxial marignal ray of a system

% pupentPupilHeigh: enterance pupil radius
% entPupilPosition: the z location of enterance pupil
%   Output
% ym,um,: mariginal ray height from the optical axis, y, and its slope (tangent of angle), u

%solved = 0;

%if initialThick==Inf % for object side afocal systems (object from infinity)        entPupilPosition
 %   ym = entPupilHeight;
  %  um = 0;  
%else    
 %   ym = 0;
  %  um = atan(entPupilHeight/entPupilPosition); %assume object surface at origin of the coordinate system
%end

%end

function [ ym um ] = computeMariginalRay(refIndex,thick,curv,clearAperture)
% send pseudo mariginal ray from oxial point at object
%
  if thick(1)==Inf % for object side afocal systems (object from infinity)     
    ytm0 = 0.1;
    utm0 = 0;  
  else    
    ytm0 = 0;
    utm0 = 0.1; %assume object surface at origin of the coordinate system
  end
  ytm = ytm0; 
  utm = utm0;
%compute clear aperture to height ration for each surface
  CAy(1) = (clearAperture(1))/(ytm);
  nsurf = length(curv) %number of surfaces including object and image
  for kk=1:1:nsurf-1 
      [ytm,utm]=yniTrace(ytm,utm,kk,kk+1, refIndex,thick,curv); 
      CAy(kk+1) = (clearAperture(kk+1))/(ytm);
  end
  %the surface with minimum value of clear aperture to height ratio is the stop
  [C,I] = min(CAy); 
  %scale the input trial ray by the factor of C so that it becames the real mariginal ray and passes the edge of aperture stop edge.
  ym = ytm0*C; 
  um = utm0*C;

end


