ps2 lab 1 - harvard university

8
A tool for science INTRO TO PYTHON PS2 - Lab 1

Upload: others

Post on 08-May-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PS2 Lab 1 - Harvard University

A tool for science

INTRO TO PYTHON

PS2 - Lab 1

Page 2: PS2 Lab 1 - Harvard University

You can’t be wrong in lab!Lab does not have the same purpose as lecture. In lecture, you’re taught very important physics concepts and how to apply them to solve problems.

In lab, you’ll ask your own questions and we’ll help you learn some of the tools you need to answer your own questions in quantitative ways. When you make a claim, you need to back it up - you need to tell someone how you know you know.

We’re going to scratch the surface of the basic tools you need to answer questions quantitatively. Those basic tools include:

• a powerful tool for calculations, visualizing data, and data analysis (Python)

• The know-how to make a meaningful measurement

• Chi Squared framework for comparing measured data with a model

And that’s OKAY!!!

Page 3: PS2 Lab 1 - Harvard University

Today we’re focusing on Python as a tool. In its most basic form, Python is a smart calculator that makes calculating numbers a whole lot easier than using a graphing calculator once you have a little practice.

Learning to write even just a tiny bit of code is an extremely useful skill and an important tool for all types of scientists. Why? Well, computers are great for performing long and tedious mathematical tasks so that you don't have to! Before computers were invented, these tasks were performed by groups of people—often women—who sat in a room together and crunched numbers. There is a very rich history of women in computation and programming, and if you'd like to read more you could start here. From Wikipedia, the picture to the left shows Dorothy Vaughan, Lessie Hunter, Vivian Adair, Margaret Ridenhour and Charlotte Craidon.

We will mostly be using Python in this class for three purposes:

1. To eliminate the need for a calculator, making mathematical calculations easier (graphing calculators can be cumbersome to use)

2. To visualize and analyze data in lab (ie make plots and fit curves, find averages, etc)

3. To have fun

Page 4: PS2 Lab 1 - Harvard University

This mini-tutorial is meant to get you up and running with Python using Google Colab. It is specifically tailored for the work that you'll do in PS2, but it is by no means comprehensive! (If you already have some experience coding, you'll be able to just blast through these exercises in no time—please use your expertise to help others.)

To get the most out of this tutorial, follow along and try everything out in this notebook or a new one. Don't be afraid to play around or try your own things—this is the best way to learn and it's the most fun. If you're worried you'll break your computer, I promise you won’t!

To access the tutorial, click the link below. Click File -> Save a copy in drive to save to your Google Drive.

If you ever get stuck, call a TF for help!

After this mini-tutorial, you should be able to…

• Import libraries and use functions from those libraries

• Define a variable

• Use print statements (displays a value on the screen)

• Add comments to your code

• Calculate a quantity and store it in a variable (ie, understand how the equals sign works in Python

• Write a little code to compute something

Python as a calculator

Activity 1

Python Tutorial (Part 1)

Page 5: PS2 Lab 1 - Harvard University

• Make a new Google Colab document by going to File->New. Write a script in this file to solve the following problem with Python:

• Check to see if you’re getting it by by submitting your final answer on Canvas. Call a TF to come by and give you some feedback on your code before you move on to the next part.

Writing a mini ScriptAm I getting it?

To calculate the force between two masses, we use Newton’s Law of Universal Gravitation:

where =10,000 kg and =50,000 kg are the masses of the two

objects, is the separation distance between the objects, and is

the gravitational constant, 6.674 N . Find the gravitational force between two masses, located at

position (4 m, 2m), and located at position (3 m, 5 m).

F = Gm1m2

r2,

m1 m2

r GG = × 10−11 (m/kg)2

m1

r1 = m2 r2 =

Problem:

Page 6: PS2 Lab 1 - Harvard University

After this mini-tutorial, you should be able to…

• Define an array

• Access particular elements in an array

• Perform mathematical operations on an array

• Plot a function

Plotting data in python

Activity 2

(0,0)

(0,1)(0,2)

(0,3)(0,4)

(1,0)

(1,1)(1,2)

(1,3)(1,4)

8 -321

9 7 74 6 -3

In the previous part of the lab, we learned how to use Python as a simple calculator. We saw that Python makes it easy to perform calculations quickly! However, we didn't do anything in part 1 that you couldn't do by hand or using a hand calculator. So, why should we bother with this whole coding thing anyway?

Let's say you wanted to compute the gravitation force exerted by a set of a thousand masses on a single object: Instead of performing calculations one at a time, you can use Python to perform the same calculation on multiple values all at once! This will be extremely helpful for doing computations on large data sets, making plots, and more - We’ll do this using a thing called an array. Click on the link below for a tutorial on arrays.

Python Tutorial (Part 2)

Page 7: PS2 Lab 1 - Harvard University

• Get a new cell in your Colab notebook from the first Am I getting it? Use python to solve the following problem graphically.

• Check to see if you’re getting it by by submitting your final answer on Canvas. Call a TF to come by and give you some feedback on your code before you move on to the next part.

Writing a mini ScriptAm I getting it?

A cannonball is shot at an angle of 40 degrees from the horizontal with a speed of 10 m/s. Compute the x and y positions as a function of time using arrays. Use those arrays to plot the cannonball’s trajectory through the air. How far away does the cannonball land?

Problem:

Page 8: PS2 Lab 1 - Harvard University

That’s it! Time to upload your Colab notebook!

• To download your Colab notebook as a pdf, click File - > Print and save to your computer as a pdf. Upload your pdf to the lab assignment on Canvas.