By the end of this section, you will be able to:
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.
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. Python is just a calculator, though an extremely powerful one. 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 Index of Python Material page lists all the skills that this textbook goes over, as well as where to find each one. In Unit 1 of this volume, we will focus mostly on the skills needed to use Python as a graphing calculator. Aside from the skills introduced on this page, these skills will be introduce in sections of the text labeled "Python Skill." However, there will also be Python demonstrations throughout the text that will demonstrate more advanced numrical methods. These more advanced demonstrations are labeled as "Numerical Method Examples." In later volumes, we will go over in more detail how these advanced methods work. As you go through Unit 1 of the text, you shouldn't feel like you need to master these advanced topics, but you should learn to be comfortable with using Python as a graphing calculator and be able to apply it to the physics problems you encounter.
Several pages in this textbook have Python code cells. You are able to run the code, and change it, and run it again. If you wish to reset the code to its default value, there is a button that will do so. There is also a button that copies the current code in the cell incase you wish to paste it in your own python installation or somewhere else.
Some code cells in the text rely on previous cells. When a Python cell is ran, anything that gets defined or stored will be kept in memory until you reload the page. If a code cell depends on a cell before it, you must run the previous cell first. Because of this, the Python cells in this text have a "run previous cells" button that you can use to make sure any previous code was completed. This style of programming is typical behavior for notebook style code documents, which are frequently used in physics. A popular example is called Jupyter notebook. If you are interested in installing Jupyter on your own device you can find more from page written by Lance Nelson, or from the Jupyter homepage.
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 to represent the speed of an object. An object may have a specific number for its speed (such as 3 m/s), but to help us remember what the number means, and to keep our focus on the principles, we write . In Python we can set that letter to hold the value of 3 m/s for us so we can just remember the . Below is a Python code that sets to the number we want and saves it for us. Press the run button to run the Python code.
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 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 ) is . The symbol represents the final position, which is what we are looking for. The symbol represents the time that the truck has been moving since it was at its original position, which is represented by the symbol . 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 and for like we did earlier for . Below is a Python code that stores the original numbers and calculates the final answer.
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.
Let's go over how to use the most common arithmetic operations: addition, subtraction, multiplication, division, and exponentiation. The first four are as you might expect. To raise a variable to a power in Python, you use a double asterisk. Python follows the order of operations, and you can insert parenthesis to help you keep the order straight. Below is an example.
Python also has access to functions that perform other mathematical operations we are used to. Many of these come from a library called NumPy. From NumPy we can find logarithm functions, trigonometry functions, square root functions, and others. Below is an example.
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 for you 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.
For more details on how to use variables in Python, see Scientific Computing, Chapter 2, by Lance Nelson.
In Python it is relatively simple to make plots. To do this, we will use the pyplot function from the matplotlib library. Pyplot doesn't plot functions, instead it plots points; it requires a collection of x values and y values. In order to give pyplot the x and y values we need to know how to use a Python list. A list in Python is an object that can contain several values. Lists in Python are surrounded by square brackets, and the values are separated by a comma. Below is an example of how we might use lists to store our x and y values.
Now that we have lists for our x and y points, we can make a plot. Below shows an example of a simple plot using our lists.
Notice that cell was is one that relies on the cell previous having already been run. Otherwise, you may get the error that xpoints and ypoints have not yet been defined.
By default pyplot will connect the dots. If you don't want the dots connected, you can add '.' as a third argument in the plot command, giving us
plt.plot(xpoints, ypoints, '.').
Letting pyplot connect the dots can be useful for plotting functions. However, with only 5 points, the curve is not very smooth. Additionally, it can be pretty tedious to type out every point that we want to plot. Luckily for us, there is a function in NumPy that will give us a list of points. The function is called linspace. It takes in a starting point, an ending point, and how many points we want to create in that range. Actually, the linspace function gives us a special kind of list called an array. Arrays are a little different from lists, but they can still be used to plot a collection of points. One advantage of using an array is that we can put the array into our equation for the function we are trying to plot, and it will return an array of the y values. Below demonstrates using arrays to plot the function .
Notice how by using arrays, we were able to make our curve much smoother. We will discuss more specifically how arrays work later on in Chapter 2, but for now you can use it as an easy way to plot a function for which you have an equation. For more details also see here.
If you want to add a second line to the plot, all you need to do is insert a new plt.plot() function after the first one, using
a new set of x and y lists. Modify the code above to add a second line representing the function .
You will need to create a second list of y points (call it ypoints2), but you can reuse the same x points list if you would like.
For more details on making plots in Python, see Chapter 11 of Scientific Computing, by Lance Nelson.