1. No more reading quizzes. I am tired of writing them and I don’t think they are doing too much in terms of helping. From now on, just read the book and we can all be happy.
2. I forgot to pass back the chapter 6 quiz today. I will give you extra time to submit a video reassessment for this standard.
Here is a comparison of launching rockets from a mountain vs. the equator.
Here is the problem I suggested in class.
Now you know enough stuff (from previous tutorials) to do something useful. Let’s start with a simple question: at what angle should you shoot a projectile to get the maximum range? If you just shout out “45 degrees” – you just memorized that. Let’s see if we can reproduce that with GlowScript.
Here is a starter program. http://www.glowscript.org/#/user/rhettallain/folder/phys221/program/GStutorial7changingprojectile
It looks like this:
This program first creates a function called “proj” (which is short for projectile motion). You give the function an initial velocity magnitude and angle and it models a thrown ball with that same starting velocity. The model shows the motion of the ball starting from y = 0 back to y = 0 (so on flat ground). The inside of the function looks just like your previous projectile motion program.
Notice that the function does not have a “return” statement. Instead, it just displays the motion of the ball. After the function, there are three instances that call the function. All start with the same initial speed but at different angles. When you run it, you get this:
Notice that I didn’t bother to draw the ground. Why would that matter? In fact, I don’t need to draw the ball at all. If I don’t make a sphere, GlowScript won’t create the 3d environment. Let me remove the sphere from the function and have it return the final x position. Here’s what the function now looks like.
This is essentially the same program. The big difference is the lack of objects (sphere) and the function returns the final x position and time. When you run it, you get this.
Each time the function is called, it prints out both the final x and the final time. I could just print the x position with this line.
Since the function produces two values, it gives them as a list. We can reference the first item in a list as proj(4,.2) and the second item as proj(4,.2) (remember that lists start with the index 0 not 1).
We are now ready to do some serious stuff. Here is the plan. Pick some starting launch velocity and some angle (a small angle). Run the projectile function and produce the final position of the projectile. Plot the starting angle and the final position. Repeat until you get to some very large angle. This should make a graph.
Here is a starter program with some blanks for you to fill in.
And this is the important part (the program won’t run until you fix it.
Clearly “stuff” shouldn’t be there. You will need to make some changes. Also notice that I used the variable v and theta1 instead of v0 and theta. Since these same variables are used inside the function, some would say it would be bad form to also use them outside the function.
When you fix it, you should get an output like this:
This shows a maximum range at around 45 degrees. Boom. Oh, why is the line squiggly? I will let you play with the code and see if you can figure that one out.
Next, something super useful. I mean, we already knew the answer for maximum range, right? What if I shoot the ball from a starting location that is 1 meter above the ground? At what angle will it go a maximum distance? Hint: you only need to change one thing in your program. Double hint: here is the output you should get.
There are many things left to explore. Here are some things you can try.
- What is the launch angle that gives the greatest time of flight?
- What if you add air resistance (see a previous tutorial)? What is the launch angle for a ping pong ball fired at a speed of 12 m/s?
- What if you start the ball on the ground a distance of 1 meter away from a vertical wall? What angle should you launch the ball (speed of 4 m/s) so that it hits the vertical wall at the highest point?
- If you shoot the ball starting 1 meter off the ground, does the angle for max range change with launch speed?
- What if you start from the ground and launch up to a table that is 1 meter off the ground? What is the angle for the maximum range? This is a little bit tougher since you will need to change the loop condition in your projectile function.
You want to do cool stuff with numerical calculations, right? Well, you can only go so far before you need to learn about functions.
Actually, functions aren’t anything new. You have already used functions like mag() and norm(). Let’s make our own function. Take a look at the following simple program.
When you run that, it should look like this:
This function is created in line 3 with “def”. Test is the name of the function and x and y are the two parameters that you pass to it. Anything indented after the def statement is part of the function. The “return” line tells the function what to give back when you call it.
Maybe that sounds confusing. Of course this “test” function is sort of silly. How about I make a function that recreates the magnitude of a vector. It would look like this.
Be aware that I have to use a unique name for this function. I couldn’t call it “mag” since that’s already used. Anything inside the function (like tempmag) only exists inside the function. I can give three different things to the function when I call it.
- A variable that is a vector (like A or B).
- A vector – like vec(-1,3,4)
- or a scalar (like 3).
Here is what happens when I run this program.
It appears that the first three times the function works correctly. When you give it a scalar, the function basically replies “dude. I don’t know what you are doing.” Your function uses the components of a vector. If you don’t give it a vector, it won’t work. The output says “NaN” which stands for “Not a Number”.
Ok, one more function. What if I want to sum the numbers up to a certain number. For instance, if I gave it 4, I would want it to calculate 1+2+3+4 = 10. Here is that function.
I guess I should have talked about loops first. Right? I feel dumb now although I technically DID talk about loops in previous tutorials when we used the “while” loop. Here is another kind of loop, he “for” loop. This “for” loop makes values of i for everything in n. Here is a super simple loop example.
Running gives this:
You can use any variable instead of “i” – but it will start from the value 0. That’s why my dumb counting function adds i+1 to the sum for each loop. Maybe it was a bad example of a function.
In the next tutorial we will do some physics using functions.