## GlowScript Tutorial 3: Graphing

Let’s go back to the program from tutorial 2 (projectile motion). What if you want to know how high the ball goes (the highest point)? You can’t use the print trick like you did in the last tutorial since you are only printing the vertical position at the end of the motion. There are some other tricks you could use to print the highest point, but what you really want is a graph.

Here is the first part of the projectile motion problem with a graph added.

The f1=series() tells glowscript that we are going to make a graph. However, it doesn’t tell it what to plot. We need to add a data point to the graph during each step of the calculation. I can do this by adding a line inside the loop (and thus indented) like this:

This will add a data point with the time in the horizontal axis and the y-position of the ball in the vertical axis. Here is what that looks like when I run it.

From this plot, you could easily find the highest point the ball traveled. As you move your mouse over the graph in GlowScript, it will show you the x-y coordinates of your mouse. This can be helpful in examining the data (and finding out the highest point of the ball). Here are some other things to try.

- Make a plot of the trajectory of the ball. This would be x vs. y.
- What about the vertical velocity as a function of time.
- Plot the horizontal velocity as a function of time.

One final note. If you have made graphs in VPython, you can use the exact same method for creating graphs in GlowScript.

**GlowScript Tutorial 4: Mass on a Spring**

## GlowScript Tutorial 2: Projectile Motion

Let’s start with a simple program (that I created for you).

This program won’t run – but we can fix that. The “rate(100)” in line 17 tells the program how fast to run the calculation. This says to do no more than 100 calculations every second. Since our time step is 0.01 seconds, this should make it “real time”. The basic idea of a numerical calculation is to break the problem into many tiny steps. During each of these little time steps, we want to:

- Calculate the force on the object (in this case that would just be the gravitational force)
- Update the momentum of the ball
- Update the position of the ball
- Update time

Here are some useful equations:

Notice that for the update position, I am using the momentum at time 2. If my time interval is small enough, it shouldn’t matter which momentum I use to find the new position. Also, look at line 21 (t = t+dt). This might seem like a silly algebraic equation since the t would cancel on both sides of the equation. However, this is NOT an algebraic equation. This is an assign statement. This says “Make the new time (t) the old time (t) plus dt. It is equivalent to t2 = t1 + dt.

Ok, now fill in the missing parts of the program and see if you can get it to work. This is what it should look like:

Now for something more useful. How high was the ball when the program stopped? One way to find out would be to use a print statement. Here is how you could do that:

Line 23 is dedented (instead of indented) so that it is no longer part of the while loop. This means that line 23 won’t be run until after the loop is finished. If you indent line 23, it will print out the ball position for every step (that’s annoying). Now try this:

- What is the final x-position of the ball?
- How long was the ball in the air?
- What is the final velocity of the ball?
- What is the final magnitude of the ball’s velocity?
- What happens if you change the initial velocity of the ball? Change either the magnitude of the velocity or the angle or both.

Now try something crazy. Add another constant force (other than gravity) to the ball. What happens? Play with it.

## GlowScript Tutorial 1: Introduction and Vectors

Let’s get started. Go to http://glowscript.org. At the upper right of the screen, click “sign in”. You should be able to use your selu account or any other google account. By signing in, you can save your programs. Next go to “your programs are here” and click create a new program. Give it a name and you should see this:

Now we are going to make three balls (ball 1, 2, and 3) all three balls have a radius of 0.1 meters and the following positions:

- Ball 1 is at <0,1,0> with a red color.
- Ball 2 is at <-2, -1, 0> with a green color.
- Ball3 is at <1,-1.5,0> with a yellow color.

Here is the code to make ball 1.

In this line (line 3) you make a sphere and assign it to the name “ball1”. The sphere() is a function built into glowscript. You can assign it a position, a radius and color.

Here is the partial code for ball 2 and 3:

Yes, some of that stuff is blank. You need to fill it in. Once you get it together, and click “run this program”, it should look like this:

This is a 3d display. You can use the mouse and right-click drag to rotate the scene and you can use the scroll wheel to zoom in or out. Of course there are multiple methods for rotating and zooming the scene. If you have a track pad instead of a mouse, try ctrl-click and drag to rotate and alt-click drag to zoom.

If your program doesn’t look like that, you have made an error somewhere.

**Properties of spheres.**

Once you make a sphere object, you can call back some of the properties of the object. Look at this code.

ball1.pos is the vector position of the ball1. Using the print statement, the value of this will appear in the output window. This is what it looks like when you run it.

The text output window will probably be below the 3d display window. Since this is a vector, we can do some other stuff. Try this code.

Here you can see the basic vector operations of vector addition and scalar multiplication. Also you can see that you can create new variables that are just vectors ( A = vec(2,-3,-1)) creates a vector. There are two other important vector operations. The mag function finds the magnitude of a vector and norm finds the unit vector.

You can actually calculate these two quantities manually. First, if I just want the x-component of vector A, I would do this:

Now I can use the following code to calculate the magnitude of A and the unit vector in the direction of A.

Notice that in python, “sqrt” is the square root function and **2 means “to the 2 power”. You should be able to work out on paper the magnitude and unit vector for vector A. Add two print statements to check your work.

**Arrows and Vectors**

How do we display a vector in the 3d environment? Here is the arrow object in Glowscript.

For the arrow object, there are two important attributes. “pos” is the location of the start of the arrow and “axis” is a vector from the starting position to the end of the arrow.

Here is an arrow pointing from ball1 to ball2 (I removed ball3).

When you run it, it should look like this.

Now go back to the three balls. Make 3 arrows pointing from each ball to the other balls. That should be good for now.

## Adjusted Date For Numerical Calculations

I am going to move the due date for the numerical calculations to next Friday (March 6). Based on what I have seen, many students seem quite overwhelmed with the assignment. I think this is due to students starting too late as well as my approach of giving too many options.

Here’s what I’m going to do. I am going to make a series of Glowscript tutorials. If you work through these, you should feel a little bit more comfortable with the project. Also remember that you just need to submit anything before the deadline. You can always improve it later.

Oh, if you don’t know how to make a video to show your computer screen, just use your phone to video record your screen. It doesn’t have to be perfect.

## Quick Start for Numerical Calculations

Here are some resources to get you started on numerical calculations. Don’t forget to submit your video by Friday.

This first video can help you get started with the three vpython tutorial documents I posted on moodle. I point out the difference you need to make in order to run this in Glowscript.

- Here are the instructions for the submission of the numerical standard https://phys221.wordpress.com/2015/02/05/numerical-calculations-standard/
- Here is the sample code from class a http://www.glowscript.org/#/user/rhettallain/folder/My_Programs/program/throwball – this wouldn’t be a bad place to start. You don’t have to do the three tutorials.
- Here is some feedback from a previous semester https://phys221.wordpress.com/2014/02/24/vpython-feedback/
- Here is another form of numerical calculations using a spreadsheet https://plab193.wordpress.com/2012/10/08/lab-7-numerical-modeling/ – you might find this easier, but in the long run it gets very difficult to deal with vectors and stuff.

Now for some suggestions of things to do:

- The first priority is to get anything to work. I suggest recreating (and changing a little bit) my projectile motion example from above.
- Try changing the initial velocity. Try changing the location of the landing point.
- What if you add air resistance to the ball?
- What about a mass on a spring? I already linked to an example of this – http://www.wired.com/2014/07/how-do-you-model-a-spring/
- In class, we estimated the deflection of a spacecraft as it came near an asteroid. Could you model this in glowscript? Yes, you could – in fact this would be a pretty awesome calculation (5 points if it works).
- Here is yet another introduction to numerical models http://www.wired.com/2012/08/an-introduction-to-numerical-modeling/