# Ray tracing - how to numerically integrate the equation of the ray

by carlos-carlos
Tags: equation, integrate, numerically, tracing
 Sci Advisor P: 2,470 Just to make sure, the equation you are actually integrating over is the following, correct? $$\frac{d}{ds}\frac{dr}{ds} = \nabla n - \left(\nabla n \cdot \frac{dr}{ds}\right)\frac{dr}{ds}$$ Note that dr/ds is just a unit vector you want as function of s, and the above equation maintains unit length of the vector. So the thing you should check in your code is if ||dr/ds|| = 1 all the way along the path. If not, you either have an error in the method or the code. Now, not knowing specifically the kind of function you have for n(r), it's a little hard to say how advanced a method you should be using, but if brute force forward Euler is not working for you, try one of the higher order Runge Kutta methods.