Back to table of contents Using Computing Tools

Euler's Method in Multiple Dimension

Learning Objectives

By the end of this section, you will be able to:

Euler's Method in Multiple Dimension

Now that we know how to solve physics problems in multiple dimensions, we can modify our Euler's method code to work for multiple dimension problems. There are two ways we will go about doing this. The first is to keep track of each direction as a seperate variables. This essentially amounts to doing two Euler's method problems that share the same while loop. The second way we will modify our Euler's method code is to store all of our dynamical variables (position, velocity, acceleration) as NumPy arrays. The advantage of this second method is that the equations will resembled the vectorized kinematic equations, which we like because of their conciseness and theoretical value.

For both methods, we will use an two dimensional example (though it can be easily extended to three dimensions if you wish). Suppose we launch a ball upwards with a velocity of 10 meters per second and an angle of 40 degrees from the horizontal. In the vertical direction, gravity will cause an acceleration of ay=g. In the horizontal direction, suppose there is wind that causes an acceleration of ax=100(sin(8t)+sin(32t)). (This still isn't the most realistic wind example, but we will us it because it makes the problem interesting.) We want to determine the time it takes the ball to hit the ground again.

Keeping Track of Multiple Components

Let us first modify our Euler's method code so that we have two variables for each acceleration, velocity, and acceleration. We will label one of them the x version and the other the y version. Then, let us store both x and y values in our list, so we can plot the trajectory of the ball. The trajectory of the ball is the path we get when we plot y against x rather than plotting y against time. Below is the modified code.

from numpy import sin, cos, pi g = 9.8 # g is a positive number # first define accelerations def ax(t): return 100*(sin(8*t) + sin(32*t)) def ay(t): return -g # define initial conditions x = 0 y = 0 v = 10 # this is just the speed (magnitude of velocity) theta = 40 *pi/180 # converted to radians vx = v*cos(theta) vy = v*sin(theta) t = 0 # define empty lists (not arrays) for plotting xpoints = [] ypoints = [] # now loop through dt = 0.001 # define step size # update this to be greater than OR EQUAL TO (otherwise it will never run since we start at 0) while y >= 0: # add the current values to the list (before we change them) xpoints.append(x) ypoints.append(y) # write new y over the old y value and x value y = y + vy*dt # using velocity in y direction x = x + vx*dt # write new v over the old v value (remember that ax and ay are functions) vx = vx + ax(t)*dt vy = vy + ay(t)*dt # update time t = t + dt # while loop is over. print(f'It took t = {t:.3g} seconds for the ball to hit the ground.') # plot the results from matplotlib import pyplot as plt plt.clf() plt.xlabel('Distance (m)') plt.ylabel('Height (m)') plt.title("Euler's Method Solution (Trajectory)") plt.plot(xpoints, ypoints) plt.show()

We expect the motion in the two directions to be indpendent. Try modifying the acceleration in the x direction. Does it change the time it takes the ball to reach the ground?

Modifying the code in this way wasn't too difficult. However, as the number of dimensions gets larger, we may choose to use the more compact method.

Using NumPy Arrays

Let's try to the solve the same problem by instead replacing all of our dynimcal variables (position, velocity, and acceleration) with NumPy arrays. We will keep the problem to two dimensions, but this method is more easily scalable to larger problems.

from numpy import array, sin, cos, pi g = 9.8 # g is a positive number # first define acceleration def a(t): return array([ 100*(sin(8*t) + sin(32*t)), -g ]) # instead return an array (2d) # define initial conditions r = array([ 0, 0 ]) # we often call the position vector r theta = 40 *pi/180 # converted to radians v = 10 * array([ cos(theta), sin(theta) ]) # vector velocity t = 0 # define empty lists (not arrays) for plotting xpoints = [] ypoints = [] # now loop through dt = 0.001 # define step size while r[1] >= 0: # compare the y value # add the current values to the list (before we change them) xpoints.append(r[0]) ypoints.append(r[1]) # write new r over the old r value r = r + v*dt # constant velocity equation # write new v over the old v value v = v + a(t)*dt # we need parentheses since a is a function. Use current time for input. # update time t = t + dt # while loop is over. print(f'It took t = {t:.3g} seconds for the ball to hit the ground.') # plot the results from matplotlib import pyplot as plt plt.clf() plt.xlabel('Distance (m)') plt.ylabel('Height (m)') plt.title("Euler's Method Solution (Trajectory)") plt.plot(xpoints, ypoints) plt.show()

Notice that with this method, the inside of the while loop didn't change much. All we did was replace y with r and ypoints with rpoints. The acceleration function changed by return an array instead of a number, and the velocity and position variables are now arrays instead of numbers.

The list that we create are only for plotting purposes, so we make lists only for the variables that we wish to plot. In both cases, notice that we no longer have a tpoints list, since we want to plot the trajectory rather than the position vs time graph. In this second example, we create a list of xpoints and a list for ypoints, and we use the two components of r to fill that list. Apart from this change, modifying the code to use arrays leaves most of the code more or less unchanged.

Conclusion

Now that we can use Euler's method in multiple dimensions, we have a tool that we can use to tackle more real world problems. Notice that we really only made small changes to a previously existing code, rather than writing a new one from scratch. This is often the case in physics. This is another reason why you shouldn't worry so much about memorizing code concepts, but instead use this text as a reference. Use these tools to help you better explore physics and solve interesting problems.

Back to table of contents