Numerically Simulating the Solar System in Mathematica
Total Page:16
File Type:pdf, Size:1020Kb
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 programming language 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 differential equation 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.