By the end of this section, you will be able to:
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 . In the horizontal direction, suppose there is wind that causes an acceleration of . (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.
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.
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.
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.
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.
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.