728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads
728 x 90 Leaderboard Ads

Wanna Learn About Vectors? Just Let Python Do the Work

THIS POST WAS ORIGINALLY PUBLISHED ON THIS SITE Click Here To Read Entire Article

One of the most important topics taught at the start of introductory physics is the idea of vectors. Students really need proficiency with them if they are to use force and momentum.

Just about every course covers this, of course, but I will do it a little differently. I’ll use python to help students hone their vector skills. But first, a quick intro to the subject.

What is a vector?

Here is a definition I’m not fond of:

Vector: A quantity that has both magnitude and direction. Example: velocity, force, acceleration. A quantity that only has magnitude would be a scalar.

It’s not wrong, but I don’t like emphasizing direction. It seems to indicate that you can express a vector with only a number and an angle. (Yes, I know it doesn’t actually say that, but that’s what some students read into it.) OK, here is my definition, which I admit is not perfect:

Vector: A quantity that contains more than one piece of information. Three dimensional vectors have three components. Example: velocity, force, acceleration. A vector with only one piece of information (a 1D vector), is a scalar.

But how do you use a vector? I won’t go over this in detail, as I’ve done so before. How about the highlights?

  • One way of representing a vector is to list its x,y, and z components. Something like v = <1,2,3> m/s. Of course there are many ways to represent a vector.
  • There is a thing called vector addition. If A and B are vectors, then A + B = C, where C also is a vector. The components of vector C are the sum of the components of A and B. (People typically distinguish vector variables from scalars by drawing an arrow over it, but that’s not easy to do when typing so I used boldface.)
  • Scalar multiplication is when a vector is multiplied by a scalar. To find the result, you merely multiply each vector component by the scalar value.
  • The magnitude of a vector is the square root of the sum of the squares of the components. Yes, that’s terrible to write out but I’m just trying to move on.
  • A unit vector is a vector with a magnitude of one and no units—yes, that seems weird. The main idea of a unit vector is to describe the direction of a vector. Trust me, it’s useful.

OK, that’s enough of vectors. Honestly, I don’t want to go over every little detail. Instead, I want to show you how to use Python to help students understand vectors.

Vector Operations with Python

Before proceeding, I will assume you have a basic understanding of vectors. If you don’t, that’s cool, but there might be points where you might refer to previous material from …somewhere. Not here.

In case you aren’t familiar with Python, I am going to be use it with the visual module (called VPython). The visual module adds lots of cool stuff, but in particular it includes a variable class for vectors. But how do you add scalars? Let’s review. In this case, I will use a web-based implementations of VPython, trinket.io, since I can embed it in the page and you can change the code as much as you like.

Here is a short bit of code that shows scalar addition and scalar multiplication (between two scalars). Click the “play” button to run the code and the “pencil” to edit it.

[embedded content]

If you haven’t played with Python before, you really must change something in that code. It won’t break, I promise. In fact, you might realize that this would make an awesome calculator for your physics homework. Yes, that is true.

Now for some similar calculation with vectors.

[embedded content]

Notice that by assigning a variable to a vector, VPython will then treat it as a vector. Here’s a quick test—try multiplying vector A and vector B. Yes, just change the code above. You should get a result that includes NaN. This is Python’s way of saying “that is not a number and you can’t do that.” I should note that Python is case sensitive—“a” is not the same as “A.”

How about one more quick demonstration adding vectors. Look at this code (and then run it):

[embedded content]

You can see that once a variable is created as a vector, the components of that vector can be accessed by adding a “.x” or “.y” to the end of that name. You should be able to modify the code to show that the vector c is indeed the same as A + B. Next let’s calculate the magnitude of a vector. I will do this two ways in the code below.

[embedded content]

In the first method, I calculated the magnitude of the vector by squaring each component, then adding them together before taking the square root (as this is the definition of magnitude). Notice that “sqrt()” is a built in function for the square root and you can raise a scalar to a power with the “**” notation. The second method is a bit simpler—it uses the built in “mag()” function. If you put a vector in there, it will return the magnitude. Go ahead and add this line to the code above and re-run it:

print(“The magnitude is”, mag(vector(1,2,3)))

Does that work? Yes—but still check it.

What about unit vectors? Here is another bit of code that calculates the unit vector two different ways.

[embedded content]

Again, you can see that there are two ways of finding a unit vector: the long way and the built-in way using the function “norm()”.

Visualizing Vectors

VPython can also draw vectors. Here is a bit of code that displays two vectors:

[embedded content]

The “arrow” is another built in object in VPython. There are really just three important properties for the arrow object:

  • pos: This is the position of the start of the arrow—think of this as the location of the vector.
  • axis: This is a vector from the start of the vector to the end—so the actual vector.
  • color: I hope it’s obvious that this is the color of the arrow.

Try adding this line to the code:

Barr.pos=Aarr.pos+Aarr.axis

You should see that now vector B (the yellow one) starts at the end of vector A. You just moved the starting location of B. Here are some other things to try.

  • Create a third vector arrow—you could call it Carr.
  • Make Carr equal to A plus B and display it so that it’s clear these add up.
  • Assume that vectors A and B are position vectors. Create vector C so that C = B – A and can represent the displacement from A to B. Make sure it displays properly.

You should be a vector master now.

About The Author

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *