Journal of Advances in Information Technology Vol. 10, No. 4, November 2019

Numerically Simulating the Solar System in Mathematica

Charles Chen Northwood High School, Irvine, USA Email: [email protected]

Abstract—The planetary motion within our solar system is a variables is global across all notebooks. This works by topic that has been studied for hundreds of years and has assigning a serial number $푠푛푛푛 to the end of all variable given rise to the science of astronomy. It is very important names, making them unique. to know the positions of the planets in our solar system, as Due to its user-friendly notebook format, Mathematica many of our current scientific research depends on it. Space is commonly used as an elaborate graphing calculator and exploration, for example, is a perfect example of when we need to know the exact positions of the planets in our solar sometimes overlooked as a for system. Since it takes many years to send a rover or satellite numerical simulations. Furthermore, Mathematica’s to a planet, we will need to be able to predict the position of selection of helpful built-in functions such as “Animate”, that planet many years into the future. Therefore, I present “Manipulate”, and “Dynamic”, which allow the user to a second order Runge-Kutta simulation to predict the control input parameters (via a slide bar) for graphical future position and velocity of the planets in our solar plots of analytic functions, often cause users to overlook system based on Newtonian laws of motion. The equations Mathematica’s ability to animate numerical solutions in of motion are implemented into a Mathematia script which real time. The code presented in this paper shows a animates the motion of each planet by generating a single creative method to achieve step-by-step, real time static plot at each iteration within the while loop, stepping forward in time, re-plotting overtop the previous frame. animations by updating a particle’s position in a while This step-by-step numerical simulation is typically loop. To highlight the difference between analytical and overlooked as an animation technique available in numerical solutions, this paper begins with a discussion Mathematica. I herein provide an introduction to the on differential equations, using the spring-mass system as software, an intuitive comparison of numerical vs analytical an example. The spring-mass problem is solved both solutions to differential equations, and finally present the analytically and numerically to demonstrate the  results of the simulation. difference between the two approaches.

Index Terms—Mathematica, Newton’s second law of motion, II. DIFFERENTIAL EQUATIONS: ANALYTIC VS Runge -Katta, Universal Law of Gravitation NUMERICAL SOLUTIONS A is any equation that involves I. MATHEMATICA the derivative of a function, with the derivative being the Mathematica is a mathematical computation software instantaneous rate of change of a function. Take for that uses the Wolfram programming language, and it example: 푑푦(푥) offers better symbolic manipulation than many other = 3푥5 programming languages. Mathematica documents are 푑푥 called ‘notebooks’ and are organized into cells that can Notice the solution to the above differential equation is be individually evaluated. The Wolfram language has itself a function, 푦(푥). This is true for all differential many built in functions, but it also allows the creation of equations. Due to the time-derivative relationship custom functions. Functions make use of the fact that between acceleration, velocity, and position, any physical Wolfram is case-sensitive by using capital letters for system wherein the position is affected by the velocity or built-in functions, which is why it is good practice to start acceleration (i.e. essentially physical systems) will be custom functions and variables with lowercase letters. In described by a differential equation. order to define a custom function, 푓[푥−]: = is used. There are two approaches to solving differential Variables in Mathematica work like variables in any equations, analytical and numerical. The difference other programming language and are defined by using the between analytical and numerical solutions is that ‘set’ assignment operator 푣푎푟 = . Modules in analytical solutions are more accurate, as their solutions Mathematica allows different programs to not conflict are continuous functions, while numerical solutions are with each other by localizing the scope of variables used discrete functions. However, many real-life situations in the program, as the default scope for Mathematica must be solved numerically, as their analytical solution either does not exist or is too difficult to solve for. A common real-world example where the analytical Manuscript received May 11, 2019; revised September 26, 2019. solution of a differential equation does not exist would

© 2019 J. Adv. Inf. Technol. 160 doi: 10.12720/jait.10.4.160-164 Journal of Advances in Information Technology Vol. 10, No. 4, November 2019

′′′ the Blasius equation, used in fluid mechanics: 푓 + 푘 푘 1 푥(푡) = 퐴 cos √ 푡 − 푖퐵 sin √ 푡 (2) 푓푓′′ = 0 [1]. 푚 푚 2 In order to demonstrate the two ways of solving here, Euler’s identity has allowed to the exponential to be differential equations, a simple spring-mass system will rewritten in terms of oscillating sine and cosine functions be used. Since a spring-mass system is much simpler [3]. The unknown complex constants A and B are easily than the solar system, it serves as an effective example to solved for when the initial conditions of the system are highlight the difference between the two methods. known. One can substitute 푥(푡) → 푥0 and 푡 → 0, as well The example system that will be used is a horizontal as 푣(푡) → 푣0 and 푡 → 0 to yield two equations with two spring with a mass on the end of it, resting on a unknowns, solve for A and B, and obtain an exact frictionless surface with no gravity involved. The goal is solution for 푥(푡) at all times. to solve for the spring’s exact motion in time 푥(푡), given To highlight the difference between analytical and an initial position 푥(푡 = 0) = 푥0 and an initial velocity numerical solutions, the numerical solution to the same 푣(푡 = 0) = 푣0 (schematic shown in Fig. 1). problem is now presented. Again, the goal is to find an unknown 푥(푡) given a set of initial conditions: 푥(푡 = 0) = 푥0 and 푣(푡 = 0) = 0. In other words, “what is the motion of a mass after an initial perturbation?” First, the horizontal axis (time) is broken into discrete segments, as shown above in Fig. 2, and each 푥(푡) must have some value at each point: {푥1 , 푥2 , 푥3 …}. Next, it is assumed that the slope (i.e. derivative) between two neighboring points 푥푛 and 푥푛+1 is simply the slope of a line that connects the two points, these are shown as red lines in Fig. 2. The slope of a line is given by mnemonic 푥 −푥 “rise over run”, or slope ≈ 푛+1 푛. Since velocity is the 푡푛+1− 푡푛 derivative (i.e. slope) of position with respect to time, the Figure 1. Schematic of a simple spring-mass system. following equation is obtained:

푥푛+1−푥푛 = 푣푛 (3) To solve for 푥(푡) analytically, it is important to note 푡푛+1− 푡푛 that velocity is the derivative of position as a function of time, and acceleration is the derivative of velocity as a Since acceleration is the derivative of velocity with 푑푥 푑푣 respect to time: function of time. Therefore, 푣 = and 푎 = [2]. Thus, 푣 −푣 푑푡 푑푡 푛+1 푛 = 푎 (4) acceleration is equal to the double derivative of position Δ푡 푛 2 푑 푥 where Δ푡 = 푡 − 푡 has been substituted in. From as a function of time, 푎 = 2. Newton’s second law of 푛+1 푛 푑푡 Newton’s second law, the acceleration of the mass must motion states that 퐹 = 푚푎, and Hooke’s law states that 푘푥 be – : 퐹 = −푘푥, where k is the force constant of a spring and x 푚 푣 −푣 푘 is the displacement of the spring from its equilibrium 푛+1 푛 = − 푥 (5) position. Setting the forces of the equations above equal Δ푡 푚 푛 to each other gives the equation −푘푥 = 푚푎 . Setting Solving for 푥푛+1 and 푣푛+1 results in 푥푛+1 = 푣푛Δ푡 + acceleration to be the second derivative of position with 푘푥 푥 and 푣 = − Δ푡 + 푣 . These final two equations respect to time, and dividing by 푚, gives the following 푛 푛+1 푚 푛 differential equation: are well suited for computation. A while loop can be 푘 푑2푥 implemented to continuously calculate the new position − 푥 = (1) 푚 푑푡2 and velocity {푥푛+1, 푣푛+1} using the previous position and velocity {푥푛, 푣푛}. Although it looks complicated, the equation simply As simple as Euler’s method is, it isn’t the most means that the second derivative of 푥 (t) is the same 푘 accurate, as one side of the equation is centered with time, function, 푥(푡), multiplied by a constant, − . With this 푥푛+1−푥푛 푚 and the other side isn’t. For example, in = 푣 , Δ푡 푛 new information, an ansatz that 푥(푡) = 퐶푒−𝑖퐴푡 can be the left side is centered halfway between 푡푛+1 and 푡푛 , made. Plugging this ansatz into the different equation while the right side is centered at 푡 . Runge-Kutta is one −𝑖퐴푡 2 −𝑖퐴푡 푛 푘퐶푒 푑 (퐶푒 ) solution to the issue, which attempts to solve the issue by results in − = 2 . Taking the first 푚 푑푡 centering both sides of the equation at the same 푡 through derivative of 퐶푒−𝑖퐴푇 gives −푖퐴퐶푒−𝑖퐴푡 , and taking the a half step using Euler’s method to get 푥 and 푣 at 푡 , second derivative of that gives 퐶퐴2푒𝑖퐴푡, which can then 푛+1/2 be substituted back into the equation, giving 퐶퐴2푒−𝑖퐴푡 = which can later be used to more accurately solve for the 푘푒−𝑖퐴푇 next step. Applying this to the spring-mass system results − . Diving both sides by 퐶 푒−𝑖퐴푡 gives the equation 푘푥 푚 in 푣ℎ푎푙푓 = − ∗ Δ푡ℎ푎푙푓 + 푣푛 and 푥ℎ푎푙푓 = 푣푛 ∗ Δ푡ℎ푎푙푓 + 푘 푚 퐴2 = , which means: 푚 푥푛, which are then used to solve for the next step with 푘 푘푥ℎ푎푙푓 −𝑖√ 푡 푥 = 푣 ∗ Δ푡 + 푥 and 푣 = − ∗ Δ푡 + 푣 . 푥(푡) = 푒 푚 → 푛+1 ℎ푎푙푓 푛 푛+1 푚 푛

© 2019 J. Adv. Inf. Technol. 161 Journal of Advances in Information Technology Vol. 10, No. 4, November 2019

Figure 2. Graphical representation of Euler’s method applied to find an unknown function x(t).

Using Runge-Kutta, the half steps of the previous III. NUMERICAL SIMULATION OF THE SOLAR SYSTEM equations are then taken, giving the final equations:

Moving on to the solar system, the mathematics is very 푝ℎ푎푙푓 = 푣푝 ∗ 푑푡ℎ푎푙푓 + 푝푛 (8) similar to the mathematics in the spring-mass system. As 푚 −푝̂ there are many factors to consider in a system with 푠푢푛 (9) 푣푝ℎ푎푙푓 = 퐺 2 2 ∗ 2 2 ∗ 푑푡ℎ푎푙푓 + 푣푝 multiple items, the system will be solved for numerically. 푥 +푦 √푥 +푦 Starting with Newton’s second law of motion 퐹 = 푚푎, 푝푛+1 = 푣푝ℎ푎푙푓 ∗ 푑푡 + 푝푛 (10) and Newton’s universal law of gravitation 퐹 = 푚 푚 푝푙푎푛푒푡 푠푢푛 푚푠푢푛 −푝̂ℎ푎푙푓 −퐺 2 , the two equations can be substituted, 푣 = 퐺 ∗ ∗ 푑푡 + 푣 (11) 푟 푝+1 푥2 +푦2 2 2 푝 then solved for acceleration, giving the equation: ℎ푎푙푓 ℎ푎푙푓 √푥 +푦

푚푝푙푎푛푒푡 ∗ 푚푠푢푛 푚 where 푝 can be replaced by either 푥 or 푦 depending on 퐹⃗ = −퐺 푟̂ = 푚 푎⃗⃗⃗⃗ ⟹ 푎⃗= −퐺 푠푢푛 푟̂ 푟2 푝푙푎푛푒푡 푟2 the component that is being found. These last four (6) equations are good for computational purposes, so given Noting that the mass of the planet cancels out. Using the initial positions and velocities of planets, as well as the fact that velocity is the derivative of position over universal gravitational constant 퐺 , the equations can 푥 −푥 predict the future positions the planets. time, the following formula is given: 푛+1 푛 = 푣 , 푑푡 푥푛 It should be noted that there are factors of where 푥 can be replaced by 푦 to find the vertical vector. consideration that have not been accounted for; however, This equation can be solved for 푥푛+1 , resulting in the these factors do not produce a significant impact on the equation: results. One assumption that is made about planetary 푥푛+1 = 푣푥 ∗ 푑푡 + 푥푛 movement is that the planets move in two dimensions Now using the fact that acceleration is the derivative (snapshots of the animation are shown in Fig. 3). In 푚 reality, the solar system exists in three dimensions, but of velocity over time, gives the equation −퐺 푠푢푛 = 푟2 due to the common orbital angular moment of the gas 푣 +푣 푛+1 푛, and solving for 푣 gives the equation 푣 = cloud that evolved into the solar system, the planets 푑푡 푛+1 푛+1 푚푠푢푛 rotate in a single 2-D plane to a good approximation. At 퐺 ∗ 푑푡 + 푣푛 . However, as the simulation is two- 푟2 most, the orbital inclinations of the celestial planets only dimensional, the equation for velocity needs to be split differ by 7o with respect to on another, meaning very little into 푣 and 푣 . To do this, the scalar 푎 is multiplied by its 푥 푦 accuracy is sacrificed in making this assumption [4]. A unit vector, 푎̂, to get vector 푎⃗. The unit vector is found by second assumption that is made is that the planets only summing the 푥 and 푦 vectors of the planet, and dividing feel a gravitational attraction to the sun. However, since by the magnitude of 푟⃗ , where |푟| = √푥2 + 푦2 . This the mass of the sun is far greater than any other object in −푥̂−푦̂ means that 푟̂ = . Together, this gives the equations: our solar system, with the second most massive thing, √푥2+푦2 Jupiter, being 1000 times less massive, a largest

푚푠푢푛 −푥̂ gravitational force a planet feels will be coming from the 푣푥+1 = 퐺 ∗ ∗ 푑푡 + 푣푥 (7) 푥2+푦2 √푥2+푦2 sun.

© 2019 J. Adv. Inf. Technol. 162 Journal of Advances in Information Technology Vol. 10, No. 4, November 2019

Before entering the initial values of all the planets into simulations of the solar system, e.g. Runge-Kutta- the code, the units of these values must be converted so Nystrom algorithms used by NASA [5]. However, the Mathematica can run the simulation without having to final script (attached at bottom of this paper), maintained deal with numbers that tens of digits long, thousands of impressive accuracy, matching true orbital patterns to times per second. To do this, all the distances were within roughly ± 0.15 earth years for 40 earth years into originally in meters, and were brought down by 109 . the simulation. Fig. 3 shows the position of the planets Times, originally in units of seconds, were divided by from 0 to 100 years into the future. The overall accuracy 33554.5, in order to set the velocity of the Earth at . of each planet is inversely proportional to its orbital The units of mass were kept at kilograms, as only the period, meaning the planet with the shortest period, mass of the sun is ever used. The gravitational constant Mercury, is the first to deviate from its true orbit. If there (units of m3kg-1s-2) was converted to a value of 퐺 = exists even a slight discrepancy between the initial 7.514 ∗ 10−29 so as to incorporate the distance and time velocity and the initial position of the planet, then the conversions used. planet will be thrown off axis very easily. This problem may be fixed if a higher order Runge-Kutta had been IV. CONCLUSION used. This simulation uses second-order Runge-Kutta, only taking the half step once, but a higher order, e.g. Since the employed algorithm only used second-order sixth-order Runge-Kutta could help even out the Runge-Kutta and simplified the assumptions used inaccuracies. Excluding Mercury, the other planets stay (circular orbits in a two-dimensional plane), the presented in orbit for ~500 years before being thrown off axis. simulation is less accurate than state of the art numerical

Figure 3. Snapshot of animation after (a) 0 years (b) 1 year () 50 years and (d) 100 years.

The central purpose of this study, to develop a flexible thank his high school physics teacher for piquing his method for simulating and animating physics in interest in physics. Mathematica was met. Although this paper only explicitly deals with the spring-mass system and celestial REFERENCES bodies orbiting the sun, the script (attached below) can be [1] J. He, “Approximate analytical solution of Blasius' equation,” easily modified to simulate any number of physical Communications in Nonlinear Science and Numerical Simulation, phenomenon by substituting the necessary force in Eq. 6 vol. 3, no. 4, pp. 260-263, 1998. [2] W. Thomson, Theory of Vibration with Applications, CRC Press, and following the subsequent steps of Euler’s (or Runge- 2018. Kutta’s) method outlined in the paper. [3] N. H. Asmar, Partial Differential Equations with Fourier Series and Boundary Value Problems, Courier Dover Publications, 2016. [4] C. D. Murray and S. F. Dermott, Solar System Dynamics, CKNOWLEDGMENT A Cambridge University Press, 1999. The author would like to thank his family and friends, [5] J. R. Dormand and P. J. Prince, “New Runge-Kutta algorithms for numerical simulation in dynamical astronomy,” Celestial especially Kameron, for their help. He would also like to Mechanics, vol. 18, no. 3, pp. 223-232, 1978.

© 2019 J. Adv. Inf. Technol. 163 Journal of Advances in Information Technology Vol. 10, No. 4, November 2019

Charles Chen was born in New Brunswick, NJ, USA in September 2001. He graduated from Northwood High School. Now he currently enrolled at University of California, Berkeley. His research interests include Computational Physics and Applied Mathematics.

© 2019 J. Adv. Inf. Technol. 164