Write your own program for optical ray tracing - Here's mine in MatLab

Поделиться
HTML-код
  • Опубликовано: 30 сен 2024
  • Viewers have asked me to describe my homemade MatLab function that performs ray tracing on a refractive lens system. The program can sequentially analyze an arbitrary number of lens elements with an aperture stop located anywhere along the lens. Aberration coefficients are computed, along with Petzval radius and distortion percent. System properties (aperture metrics and field metrics) are also included in a system report file. All this in less than 300 lines of code. The relevant optical concepts are explained along the way.
    I made this video, at the request of viewers, in order to provide an example program to those who wish to write their own code, and hopefully write it better than I did. Please don't ask me to provide a copy of my code. The idea here is to provide an example to people who want to write their own program relying on their own programming skills and their own preferred programming language.
    Please consider clicking on "LIKE". I don't get paid for creating this content, and your positive feedback is the only encouragement that I receive to continue creating this kind of helpful material.
    References are made in this video to these two books:
    Introduction to Lens Design: With Practical ZEMAX Examples, by Joseph M. Geary
    Introduction to Lens Design, by José Sasián
    OpticStudio is a product of Zemax, an Ansys Company, www.zemax.com
    MatLab is a product of MathWorks, www.mathworks.com
    #lensdesign
    #opticaldesign

Комментарии • 17

  • @ycliu49.10.1
    @ycliu49.10.1 Год назад +3

    Hi Stephen, I have been working in optics field (lidar, fiber coupling, and silicon semiconductor package) more than 10 years.This video is really great. It helped me review the principles of optics once again. Thank you.

  • @michaelbanach3741
    @michaelbanach3741 Год назад +3

    Great video, thanks!

  • @marknicholson5508
    @marknicholson5508 Год назад +2

    Great job Stephen! I think I may know why your BFL doesn't quite match OpticStudio's. OpticStudio defines the back focal length as the distance along the Z axis from the last surface made of glass to the paraxial image surface for the object at infinite conjugates. If your object isn't at infinite conjugates, you still need to do a ray-trace from infinity to compute BFL. This lets you compare the BFL of two lenses irrespective of conjugate ratio, or at least, at a fixed conjugate ratio.
    Hope that helps. Also, you didn't say but make sure your code is using double precision and not floats. As you trace surface by surface, errors accumulate. You don't specifically address how to handle the accumulation of errors, but the first place to start is to use doubles.

    • @stephenremillard1
      @stephenremillard1  Год назад +4

      Thanks Mark. Good point about double precision. Double is essential, especially given the number of quantities that rely on the difference between similar numbers. Because double precision is the default data type in MatLab, I got a little lazy and the importance of mentioning it didn't cross my mind, as it should have. So, thank you for bringing it up.
      I compute the BFL using an in-coming parallel ray at the height of the first clear semi-diameter. I didn't show the benchmarking with the infinite conjugate configuration of the telephoto lens, but it gave the same BFL as OpticStudio. Same too for the two periscopic lens models: finite and infinite conjugate. Of the four benchmarking models that I had Zemax screen shots for, the finite conjugate telephoto was the only model that had this small difference in the BFL. Small, but important. Maybe there's some detail that I didn't capture with the screen shot. Hopefully I'll find out some day when I fire up OpticStudio again.
      Regards,
      -Steve

  • @optiondrone5468
    @optiondrone5468 Год назад +2

    Stephen this was an excellent video, do you think you can make more videos which will teach novice like me all the fundamentals about optical simulation and eventually progress to self made optical simulation code development? Thank you again for such an excellent video

    • @stephenremillard1
      @stephenremillard1  Год назад +3

      Sure. Here are some: ruclips.net/p/PLmfHzApbF5dbx_S_h9A13uLm4LVK0f6XK

    • @optiondrone5468
      @optiondrone5468 Год назад

      @@stephenremillard1 thank you so much for those video links, I'm looking forward to going through them hopefully at the end I'll be able to write my own optical simulation program. Thank you for your help.

  • @manarc-cs6uv
    @manarc-cs6uv 2 месяца назад

    @Stephen Remillard. Hi Stephen, I am a student in optics.Could you explain the equation at line and 142 of your code (@15:11); In this section I don't konw the value EFL why not mentioned here?Also,I couldn't find the new variable EFL you used in line 159 (@15:49) in the previous code ? Please tell me the reason.

    • @stephenremillard1
      @stephenremillard1  2 месяца назад

      The quantity that is computed in the loop from lines 136 to 138 isn't the complete total track length. It is only the distance up to the last glass vertex. The back image distance still needs to be added to it in order to have the quantity that is typically referred to as total track length. The effective focal length is calculated on Line 152 (@15:37).

    • @manarc-cs6uv
      @manarc-cs6uv 2 месяца назад

      @@stephenremillard1 Thank You for your expaining, The problem has been solved。 But have a new problem , when I using your all code for learning MATLAB and Optical fundamental konwledge , the speed for program is to slowly .I let this code run for 15 minutes, and the code still has not jumped out of 44 lines of code to find the loop of the chief ray.(@8:23)

    • @stephenremillard1
      @stephenremillard1  2 месяца назад

      @manarc-cs6uv It sounds like the loop termination condition is either being missed, or not reached. You might try changing the size of the variable incr. Increase it a factor of 10 or decrease it a factor of 10. Then try factors of 100 or more if that doesn't work. If it is taking too long to complete the loop, then incr is too small. If it never completes, then incr might be too large - which is why I had to give it a small value.

    • @stephenremillard1
      @stephenremillard1  2 месяца назад

      I'll also add that a better programmer than I can come up with a better way to execute that operation in the red box @8:23. A while loop isn't very efficient, and is perhaps evidence of my Fortran 77 upbringing.

    • @manarc-cs6uv
      @manarc-cs6uv 2 месяца назад

      ​@@stephenremillard1 I tried the variables of reduction and enlarging INCR again, but it still seemed to be unable to meet the condition in the WHILE loop Done == 1.

  • @lfvlima6295
    @lfvlima6295 Год назад

    great job. do you have any tutorial video for metamaterials ou phononic crystals?

  • @brisingreye5209
    @brisingreye5209 4 месяца назад

    @Stephen Remillard. Could you explain the equation at line and 49 of your code (@7:18)? How did you come to this equation?
    thanks in advance!

    • @stephenremillard1
      @stephenremillard1  4 месяца назад +2

      Take a look at 3:46. This is the equation at the top of the screen, (n'u'=nu-y*phi). In Line 49, yt(i) is the chief ray height at surface i. uprime is the chief ray angle after the designated surface. So, uprime(i-1) is the chief ray angle incident at surface i, since it is the chief ray angle after surface i-1. Also, the angles are replaced with tangent of the angles to improve the accuracy.

  • @HarshPanchal-vt6qe
    @HarshPanchal-vt6qe 6 месяцев назад

    sir can you share the code for this?