Simulation of a RAINBOW

             Copyright, B. Sinclair, 
             University of St. Andrews. 
             Used with permission.
Objective: To simulate the generation of a rainbow using a probability experiment employing the Monte Carlo technique.  (For a general description of the Monte Carlo technique go to the Elementary Probability Demo Collection.)

Level: High school or precalculus, or even probability courses for math majors. This demo could also be used in an introductory physics or science course.

Prerequisites: Basic probability concepts; knowledge of equally likely chance and the chance of winning a lottery. Some basic notions from physics regarding angles of reflection and refraction; that is, elementary optics. Basic trigonometry.

Platform: A computer which has a function that generates random numbers and a color graphics display. There is an accompanying JAVA applet which performs simulations discussed in this demo: Rainbow.html. 

Instructor's Notes:

General Background: We have been fascinated by the phenomena of a rainbow throughout time. It was Sir Isaac Newton's experiment with light passing through a prism that revealed a band of colors varying from red to violet. The colors of the band are violet, indigo, blue, green, yellow, orange, and red. (A mnemonic that is often used to recall these colors is VIBGYOR or Richard Of York Gained Battle In Vain. An alternate form, ROY G. BIV is also used.)

Newton concluded that white light is really a mixture of colored lights, and that each color bends differently as it passes through a prism. Thus we see the band of colored light which is called the visible spectrum. The light we can see is but a portion of the huge spectrum of energy called electromagnetic radiation. 


A Rainbow: A rainbow is formed by light from the sun hitting a raindrop. When this occurs the light is refracted (the change of direction of light in passing from one medium to another) into the colors of the spectrum and then reflected (the return of the light from striking a surface) off the back of the raindrop. For more details see Figure 1 and the description below it.

                                                           Figure 1.
A light ray hits the drop from direction SA. As the beam enters the drop it is refracted (bent) and then strikes the back wall of the drop at B. It is reflected off the back wall of the drop towards C. As it emerges from the drop at C it is refracted (bent) again. (Not all rays hitting a drop undergo 'total internal reflection'; see the next figure.)
Each color of the spectrum is refracted at a slightly different angle depending upon it wavelength. For instance violet light is refracted more than red light. As the spectrum of light emerges from the drop an observer on the ground will see only one color depending upon the angle of observation. 
Copyright Rebecca Paton. Used with permission.
(Rebecca has a very nice site devoted to aspects of rainbows.)
The rainbow we normally see is called the primary rainbow and is produced by one internal reflection within the drop. When conditions are just right a fainter larger secondary rainbow with colors in reverse order appears. (See photo at the top of the page.) This is the result of rays undergoing a second reflection within the drop. It is possible for light to be reflected more than twice within the drop, giving rise to higher order rainbows, but these are rarely seen. (See the comments below and the National Geographic citation.)
                                 Copyright Alana Ackerman. Used with permission

In order to develop a mathematical model to simulate a rainbow we need further information about the angles of reflection for the various colors of light within the visible spectrum. This requires familiarity with the laws of reflection and refraction as well as the speed of light in various mediums. For detailed information on these topics see reference [2] and [3]. The following paragraph is a nontechnical summary of the information we need in order to produce an elementary simulation of a rainbow.

The velocity of light is dependent upon the medium through which it passes, like air or water. The change in velocity of light as it passes from air to water cause the refraction, bending, described above. The refraction index or index of refraction is the ratio of the velocities of light in the two media. We can use the refraction indices of the colored light to set up the appropriate angles of refraction and reflection in our simulation. This information together with algebra and trigonometry can be blended to model the color dispersion we see in the phenomena of a rainbow. (More details can be found in the references cited below.)

The Model: Imagine a large number of parallel rays hitting a spherical rain drop. For each ray we plot a single point of light in accordance with the laws of reflection and refraction. For each ray of light we randomly choose a color that will be seen by an observer once it has been refracted and reflected either resulting in a point of light in the primary or secondary rainbow. Since we are randomly assigning a color from the spectrum to a ray, the process is using a Monte Carlo simulation. In order to get the angles correct we use the refraction index of the colored light for the mediums air and water. This also determines the placement of the corresponding dot of light in our picture. Figure 2 shows a result of our simulation using 15,000 rays. In this simulation we have used only the three primary colors red, green and blue for simplicity. Even with this small number of rays the primary 

                                                     Figure 2.

and secondary rainbows are evident, with of course a lot of 'scatter' within the primary arc. In Figure 3 we simulated the rainbow using 30,000 rays. The result is a stronger definition of both the primary and secondary arcs.

                      Figure 3.
If the water droplets are sea water then the index of refraction changes for the colors in the visible spectrum. By altering these indices we can simulate a sea water rainbow as depicted in Figure 4.
                                                                Figure 4.

A simulation using the seven colors of the rainbow was performed in a similar manner by modifying the routine that generated Figures 3 and 4. The primary arc is shown in Figure 5 and an enlarged portion of the arc is in Figure 6. The colored bands for the seven colors are quite visible. The modification required more information of the refraction indices for colored light.

                                  Figure 5.
                                                     Figure 6.

The rainbow figures in this demo were generated by MATLAB routines rainbow , rainbowsea , and rainbowfull respectively. (Click on the names to download these programs. To see a description of the mfile rainbow click on mfiledescription.) These routines are based on an Applesoft Basic program written by eighth and ninth grade students in a summer program supported by NSF; see reference [1] below. Interestingly, there is also additional information on simulating an "acid rainbow," which may occur in the clouds of Venus. We have also developed a JAVA applet for this demo which has the same functionality : Rainbow.html. (The displays of the applet may vary slightly because of the browser you use and the screen resolution of your monitor.)

You can construct your own rainbow with a garden hose under the right conditions. Click here to a 'backyard' rainbow. (Photo used with the permission of Dr. Paul Perlmutter.)


There are a large number of web sites that contain information on rainbows. Several that were helpful in creating the descriptions above are listed in the references. [6] contains an java applet related to refraction and reflection. A very nice calculus project on rainbows is available in [7].

The National Geographic Magazine has an Ask Us column. In its March 2002 edition the following item appeared. 

"Q When I see a double rainbow, the colors in the second one are reversed. Why is that?"

"A When sunlight strikes a raindrop, light rays go in, bounce off the back of the drop, and come back out. When passing in and out, the rays bend -- as in a prism -- and the colors are separated so that we see the hues of the rainbow, with red on the outer arc. A smaller number of rays reflect twice inside the drop before they exit. The second reflection inverts the image, resulting in a paler rainbow with red on the inner arc. Sometimes a third rainbow, with red again on the outer rim, is visible."


[1] D. Olson, et al., "Monte Carlo Computer Simulation of a Rainbow", 
The Physics Teacher, April 1990, pp. 226-227.

[2] S. Janke, "Somewhere Within the Rainbow", UMAP Module 724, COMAP, Inc., Lexington, MA. (This work appeared in the UMAP Journal 13 (2), pp. 149 - 174, 1992.)

[3] F.J. Wicklin and P. Edelman, "Circles of Light: The Mathematics of Rainbows",
(This work is based on the module in [2].)

[4] D. Harrison, "LIght",

[5] B.T.Lynds, "About Rainbows",

[6] F.K. Hwang, "Rainbow",
(A Java applet that shows refraction and reflection of colored light. Page contains some interesting links for physics.)

[7] R.W. Hall and N. Higson, The Calculus of Rainbows,

Credits: This demo was adapted from [1]. The MATLAB programs were developed by David R. Hill. The Java applet was developed by Saritha Somasundaram, a student  at Temple University.

DRH 12/05/01  Lasted updated 5/4/2004

Since 6/14/01