# Spherical lens ray-tracing simulation is not focusing rays

I am trying to make a very basic ray-tracing simulation of lens.

• A single ray origin trigger light photons of a supposed single frequency in all directions. (I do not consider chromatic aberration).
• Some lens emulated as a couple of spherical superficies with refraction index.

After detecting an intersection between a ray and the closest lens, I get the normal at this point to the lens and apply the following refraction formula wikipedia:

$$\frac{sin(\theta_1)}{sin(\theta_2)} = \frac{n_2}{n_1}$$

The formula is applied twice: a first time when entering the lens with (e.g. index $2.0$) and a second time when exiting the lens with the inverse index (e.g: $0.5$).

I checked the results numerically, but the rays do not focus: As you may observe, the focus "point" is closer for rays crossing the outer part of the lens, while is further for rays crossing the lens in the middle.

Some suppositions:

• The formula is too simplistic for achieving a correct focus point
• Lens are not exactly spherical
• I do some mistake somewhere

**Why are those rays not converging to the same focus point? **

Example of calculation:

• Ray: Origin=$(400,300)$ Direction=$(0,-1)$
• Lens: Circle center=$(400-50\sqrt{2}, 100)$ Radius=$100$
• Intersection: Position=$(400, 170.7107..)$ Normal=$(\sqrt{2}, \sqrt{2})$
• Refracted ray inside the lens: Origin=$(400,170.7107...)$ Direction=$(-0.41144, -0.91144)$ $\theta_1=-45$ $\theta_2=-20.7048$ $\frac{n_1}{n_2}=2$
• Intersection2: Position=$(323.027, 0.19627)$ Normal=$(0.062622, 0.99804)$
• Refracted ray outside the lens: Origin$(323.027, 0.19627)$, Direction=$(-0.75, -0.66144)$ $\theta_1=20.7048$ $\theta_2=45$ $\frac{n_1}{n_2}=0.5$ 