Updating force, momentum, and position

From Physics Book
Jump to navigation Jump to search

This topic covers building a VPython script to loop until a specified time and update the fore, momentum, and position in a system to create a neat animation and solve problems dealing with force, momentum, and position at a specific time.

The Main Idea

The main idea is to take the mathematical models we already know, and learning the syntax to put them into a VPython script that can help us solve problems quickly and easily.

A Mathematical Model

The mathematical models we will use are force update equations, momentum update equations, and position update equations.

Force (These equations are the force equations for whatever type of problem you are solving. In our case, the gravitational force of Earth on the Spacecraft.):
[math]\displaystyle{ \vec{F}_{grav} = -G\frac{{m}_{earth}{m}_{craft}}{|{r}_{earth \rightarrow craft}|^2}{\hat{r}}_{earth \rightarrow craft} }[/math]

Momentum Update Formula:
[math]\displaystyle{ \vec{p}_{f} = \vec{p}_{i} + \vec{F} \Delta t }[/math]

Position Update Formula:
[math]\displaystyle{ \vec{r}_{f} = \vec{r}_{i} + \frac{\vec{p}_{craft}}{{m}_{craft}} \Delta t }[/math]

A Computational Model

Here is a link to what your code should look like after you finish reading through this page: Goal GlowScript

Example

Here is a step by step on how to write this VPython script:

Defining Constants

The first thing you are going to want to do is define the constants in your system. In our example where a space craft orbits the Earth we just have a few constants to define, the gravitational constant (G), mass of Earth and the spacecraft, and the time step. To do this, just choose your variable name and give it a value like so:

G = 6.7e-11
mEarth = 6e24
mcraft = 15e4
deltat = 600

Define Objects

The next step is to define the objects in the system and give them their initial values. Here we have the Earth at rest with a given radius and a spacecraft with a given position from the earth and the initial velocity. To represent these objects, we are going to use spheres in VPython and use the pos, radius, and color attributes to give them their characteristics. We are also going to use variables to hold the velocity and momentum of the spacecraft.

Earth = sphere(pos=vector(0,0,0), radius=6.4e6, color=color.cyan)
craft = sphere(pos=vector(-6.976e7,6.4e6,0), radius=3e6, color=color.yellow)
vcraft = vector(160,2664,0)
pcraft = mcraft*vcraft

Calculations

Lastly, we need to set up a time loop with the needed calculations to update the spacecrafts position as it travels around the Earth.

First we define a time variable to hold the current time, and open a while loop to stop the calculations once we hit a goal time (we will use 2901312 seconds in this example). The last line of code here slows down the animation so that it is easier to watch:

t = 0
while t < 2901312:
    rate(100)

Next we find the position vector pointing from Earth to the spacecraft by subtracting the Earth's position from the spacecraft's. Then use our mathematical models of updating the force, momentum, and position to find the new values. In the code, we type out the formulas just as they are:

    rVec = craft.pos - Earth.pos
    fNet = -G*(mEarth*mcraft)/(rVec.mag**2)*norm(rVec)
    pcraft = pcraft + fNet*deltat
    craft.pos = craft.pos + pcraft/mcraft*deltat

Lastly we increment the time variable by the given delta t:

    t += deltat

Full Code

G = 6.7e-11
mEarth = 6e24
mcraft = 15e4
deltat = 600

Earth = sphere(pos=vector(0,0,0), radius=6.4e6, color=color.cyan)
craft = sphere(pos=vector(-6.976e7,6.4e6,0), radius=3e6, color=color.yellow)
vcraft = vector(160,2664,0)
pcraft = mcraft*vcraft

t = 0
while t < 2901312:
    rate(100)

    rVec = craft.pos - Earth.pos
    fNet = -G*(mEarth*mcraft)/(rVec.mag**2)*norm(rVec)
    pcraft = pcraft + fNet*deltat
    craft.pos = craft.pos + pcraft/mcraft*deltat

    t += deltat

Connectedness

This topic is something that I am interested in because I love programming and writing scripts that not only help me solve problems faster, but also simulate the problem that I am solving. Writing these programs lets me really see and visualize the problems that I am working on. Also, being a computer science major, I also get to see how physics can be implemented into programming and software development. It shows me that I am not just taking a class that won't help me in the future. Also, with this type of programming, the industrial applications are endless. Using our physics principles and these basic coding concepts, we could potentially model and visualize any problem thrown at us and apply it to anything.

History

This idea is timeless. It is applicable to every physics property and formula, and the programming can be adapted to any problem that is given anywhere, to anyone. The possibilities that VPython and other scripting languages have opened up are infinite and can fit any context.

See also

If you are interested in other VPython guides check out the VPython guide category to learn more!

Further reading

Throughout the class textbook there are many examples on how to apply the various physics topics to VPython and writing little scripts to make your life easier while solving problems.

External links

VPython Documentation - This page is the documentation for VPython and has access to every command you can use and how the syntax is to use it correctly.

References

VPython Documentation - I used this site to learn the syntax of VPython and how to use the objects like the spheres.