Back to table of contents Using Computing Tools

Computing Solutions Numerically

Learning Objectives

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

Using Computers To Do Physics

While physics is about describing the fundamental interactions that govern our world, being able to quantify things with numbers and make numerical predictions is a valuable tool. It's what gives physics the ability to be useful in the everyday world. Much of the calculation we do in physics enlists the aid of computers. Being able to let the tool worry about the numbers gives us more time to think about the principles and the problems we are trying to solve.

Many of the problems we will solve in this course will be simple problems designed to help you learn the principles. How do we scale up the things we learn to problems that need solving in the real world? We break hard problems into smaller simple problems. Though it might take us a long time to solve many simple problems over and over, if we can solve the problem once, we can make a computer repeat the calculations over until we have a solution for the larger more difficult problem. Many numerical methods are built on the principle of breaking a difficult problem into a smaller easier problem that can be repeated over and over.

Python: A Powerful Computing Tool

One example of a computing tool that is commonly used in Physics is Python. Python is a programming language that is fairly easy to learn. In this text, we will use Python to do numerical calculations as part of our physics experience. There is much one can learn with python, but to get started we only need a few basics.

The first thing we need to learn about is variables. When we are doing physics, we will want to assign labels for certain quantities. For example, we often use the letter v to represent the speed of an object. An object may have a specific number for its velocity (such as 3 m/s), but to help us remember what the number means, and to keep our focus on the principles, we write v. In python we can set that letter to hold the value of 3 m/s for us so we can just remember the v. Below is a python code that sets v to the number we want and saves it for us. Press the run button to run the python code.

v = 3 # m/s

Python doesn't actually keep track of the m/s, only the number 3. Notice the hashtag symbol. It tells python to ignore everything after that on the row. These hashtags are used in python to create code comments. They don't talk to the computer, only the human reading the code. I used it here to remind myself that v is measured in meters per second.

In physics we will need to plug in numbers to equations that we derive. When we solve problems in physics, we often come up with an equation that gives the result we are looking for. As an example, the final location of a moving truck (traveling with a speed of v) is xf=x0+vt. The symbol xf represents the final position, which is what we are looking for. The symbol t represents the time that the truck has been moving since it was at its original position, which is represented by the symbol x0. The different variables in the equation represent numbers, but we let the computer plug them in for us. For this we will need to store values for t and for x0 like we did earlier for v. Below is a python code that stores the original numbers and calculates the final answer.

# store initial position (starting at 0 meters) x0 = 0 # m/s # store time of travel t = 30 # seconds # calculate the final position xf = x0 + v*t # print the results print(xf)

Notice that since we stored the velocity earlier, we didn't need to store it again. The code remembered it from earlier (if you ran the code earlier). In this textbook, each page will remember code that was ran earlier on the same page.

Also notice in the code that once we have the equation that gives our answer, all we need to do is type it in. Typing equations into python is very straight forward and looks much like the way we write them when we are coming up with the equations.

For now, this is a good start. We will learn more tricks throughout our physics journey that will allow us to break difficult problems into simpler ones. As you move forward, we will provide python cells in the text for you to use like this to compute answers to the problems you are working on. As the problems get harder, we will introduce more things you can do in python to make the tool more useful. We invite you to practice with it now, even when the problems are simple.

Check Your Understanding

What would you need to do to the code from earlier to calculate the position of the truck after 15 seconds instead of calculating the velocity of the truck after 30 seconds? What would you need to do to calculate the position of the truck if it originally started at 15 meters intead of starting at 0 meters? Make these changes and see if they seem reasonable? Is that what you would have expected?

For more details on how to use variables in Python, see Scientific Computing, Chapter 2, by Lance J. Nelson.

Back to table of contents