**[»] Tutorials**and

**[»] Optics**.

Today’s post is a bit of an *aparté* in the current series but I wanted to share the results with you because it illustrates some very nice methods that can be set in place.

We have previously discussed in details how to treat [»] paraxial focus of an optical system but we also mentioned that the various types of aberration will shift the actual *best* focus position either in front or behind the paraxial focus position (see our video [»] here). So, at some point, we will need to update our design to accommodate for the actual *best* image plane position.

I’m putting the term *best* in italic here because there are different ways to define what is a better focus. Common criteria are the best rms spot size, the best wavefront rms or the best contrast at a given spatial frequency. Here, we will use the criteria of the **best rms spot size**.

According to this criteria, an image will be considered at its best focus when the root-mean-square value of the radial interceptions of the rays with the image plane is minimal. It may look daunting when expressed like that but we have already met [»] rms spot size in the past. Put shortly, rays traced across the pupils from the same object point source will meet the image plane at different positions. The rms spot size is a measure of “how large” the spread on the image plane is. An illustration of the spot diagrams for three different field angles are given in Figure 1 with one color per field angle.

The rms spot size is computed from the interception coordinates *(x _{i}, y_{i})* of the

*N*different rays for a given field angle:

The rms spot size is usually computed relative to some center coordinates *(x _{c}, y_{c})* which is the same for all the rays in a given field. It can be the coordinates of the chief ray (the ray passing through the center of the STOP) or the centroid of the spot computed as

A typical (but somewhat naïve) approach to find the best focus position is then to compute the rms spot diagram for different image plane position and keep the image plane that gives the smallest rms value. I believe this is how Zemax is implement its Quick Focus button – a button that you will use very often when working on an optical design!

We can however do better!

Using the same raytracing logic as the one presented in [»] this post, we can compute by how much each ray moves for a defocus of the image plane *d*

where the starred coordinates are those after the shift of the image plane by a distance *d* and the *(v _{x},v_{y},v_{z})* are the directions of the rays.

The rms spot size after a shift of image plane *d* can therefore be computed as

with

the centered coordinates.

Note that *v _{x}/v_{z}* and

*v*are respectively the deviations (tangents) along

_{y}/v_{z}*x*and

*y*respectively.

From this expression, we can use the least-square theory to compute the value that minimize the error *ε ^{2}*:

and therefore

The best defocus position of the image plane is therefore obtained in a single-pass without any need of iterations.

The process can be used for on-axis rays, off-axis rays and even mix of different field coordinates or different wavelengths. The key aspect is to use the centered coordinates for the computation.

Just as before, the center coordinate can be either the chief ray or the centroid. When using the centroid, the direction is defined as the mean deviation *v _{x}/v_{z}* and

*v*computed just as you would do for the mean positions of the

_{y}/v_{z}*(x*.

_{i}, y_{i})The fun part comes now, with a performance analysis.

When validating the algorithm using a planoconvex lens in Zemax, I discovered that this formula actually gives better results than Zemax’s own version of the quick focus!

For a 100 mm focal length planoconvex lens with a 15 mm diameter aperture, Zemax found a best focus position at 96.230 mm yielding a rms spot size of 9.080 µm. The formula here-above predicted a best focus position at 96.196 mm (a 34 µm difference) and yielded a rms spot size of 8.879 µm in Zemax itself! The results are presented in Figure 2.

This doesn’t look much of a difference but it’s still a very unexpected results as it is possible, in Zemax software, to find a position that yields a better rms spot size than what the software quick focus feature is able to find.

I repeated the experiment using this time the merit function wizard to yield the minimal rms spot size to see if the quick focus used some sort of approximation and if a real, iterative, merit function would yield better results. The merit function generated is displayed in Figure 3 and generated a best focus of 96.230 mm – just like the quickfocus button.

I believe the non-optimal results given by Zemax is a direct consequence of the uniform pattern used in its computation. Indeed, we have seen in our [»] former post that using a repetitive pattern can yield inaccurate results and that it is better to use a pseudo-random distribution in the pupil space. Clearly here Zemax is using a pattern in its merit function and it probably also is when you click the quick focus button. In our algorithm, I used a Poisson distribution and the results of Figure 2 are given using a dithered pattern (a less performant pseudo-random technique than a Poisson distribution). That may explain very well the observed behavior.

In conclusion, the solution here-above is therefore a more accurate way of computing the best rms focus position for the image plane. It does not require an iterative solving procedure and work with any ray distribution you would like to use. It also illustrates how problems can be solved using more traditional methods such as the least-square!

I would like to give a big thanks to **Naif**, **Young**, **Samuel**, **James**, **Lilith**, **Eric**, **Hitesh**,** ****Jesse**, **Sivaraman**,** ****Sebastian**, **Jon**, **Andrew**, **Themulticaster**, **Cory**, **Karel**, **Alex**, **Tayaab** and **Marcel** who have supported this post through [∞] Patreon. I also take the occasion to invite you to donate through Patreon, even as little as $1. I cannot stress it more, you can really help me to post more content and make more experiments!

**You may also like:**

[»] #DevOptical Part 9: Geometrical Image Simulation

[»] #DevOptical Part 11: The Diffractive PSFs

[»] Understanding Least-Squares Fitting

[»] #DevOptical Part 12: The Paraxial Image Position Formula