Physics 165 - Introduction to Programming in the Physical Sciences - Fall, 2003

Prof. Ted Einstein (Homepage)

Physics Building, Room 2310 (Mailbox in 2323); x56147


Welcome to Physics 165. Messages of special importance and novelty will be posted here.

Final Exam Rules:

Students may use any documentation or files on the course server and website, books, and notes, but may NOT discuss the questions with anyone else (except me; you may email or phone me to point out typos or ambiguities and to ask questions !).

The exam will be emailed to students by 6pm Tuesday, Dec. 16, and answers should be emailed back to BOTH einstein@wam.umd.edu and einstein@umd.edu by noon Thursday, Dec. 18. I intend that the exam will not take more than 2 hours (if you have prepared adequately!). It will consist of several short problems that require you to use some of the techniques we have studied this semester. Often this will involve writing functions or a few lines of code, using VPython capabilities, and modifying programs we have studied. The exam is cumulative but will emphasize material covered since the midterm.

Introduction to VPython
Number types, mathematical operations, lists
Screen I/O, print-statement format
Numerical Solutions of Differential Equations: Newton's laws with Euler method*
Logical expressions, conditionals, and iteration/recursion: while, for, if, else, elif
Outputing graphs with VPython: gdots, gcurve, ghistogram; gdisplay specs
User defined functions: arguments, return
Ufuncs
Operations on lists, arrays, tuples
Two-dimensional arrays (list of lists) and operations thereon
Random number generator and ways to use it for other than uniform distributions between 0 and 1.
Converting between types
Using color capabilities of VPython to display physical information
Using num.py capabilities, familiarity with online resources, esp. documentation
Using exceptions in output
Interactions of large numbers of particles*
Using gas.py* and soundspeed.py*
Elementary aspects of classes and subclasses

*You may be asked to make minor modifications of these programs or to correct errors in modified versions of them.

 

Last updated December 11, 2003


Links to Course Information
Schedule
References,
URLs
Course
Policies
Programming
Projects
Homework
Assignments
 



SYLLABUS

Introduction
The course will cover basic programming skills using examples from the physical sciences. The course prerequisite is one semester of a calculus-based physics course such as Phys 141, 161 or 171, or credit on the physics AP exam. The language used is Python, which is an interpreted, object-oriented language with both scripting and systems-level capabilities. The course will provide instruction in the standard programming techniques used in upper-level languages such as FORTRAN, C, and Pascal, as well as an introduction to the object-oriented programming techniques used in languages such as C++ and Java. The course will include a strong component of visualization and graphing using the visualization tools available through the VPython* implementation.

*The Visual library is Copyright (c) 2000 by David Scherer.

Course Description
    The class meets Monday, Wednesday, and Friday at 3:00-3:50 pm in room 1201 of the [John S. Toll] Physics Building. This new high-tech lecture room has hook-ups for laptops (though students are not expected to have them). On Fridays (for at least the first few weeks) we will meet in Room 3115, the Physics Open Workstation Laboratory (POWL), where there are computers for each student.

Instructor
Prof. Ted Einstein
Room 2310, Physics Bldg.
Phone 405-6147
e-mail: einstein@umd.edu
Office hours: after class on Monday, to be arranged, and whenever you find me in my office.

Course Software
Python and VPython* (both version 2.3)

The software is installed and available for use in the POWL, room 3115 Physics Bldg. If you wish to install a copy of the software on your personal computer, it is available as freeware, for windows at http://www.vpython.org/win_download.html and more generally at: http://www.vpython.org/download.html.

Course Text

How to Think Like a Computer Scientist Learning with Python, by Allen B. Downey, Jeffrey Elkner and Chris Meyers, available for free download at http://www.ibiblio.org/obp/thinkCSpy/ or in a very-reasonably-priced paperback published by Green Tea Press.

Recommended:

Learn to Program Using Python, A. Gauld, Addison-Wesley, 2001. It is in paperback, probably worth buying, though you can find a lot of the information posted free as sidebars at http://www.freenetpages.co.uk/hp/alan.gauld/ . Gauld gives a nice introduction that gets you started quickly and providing lots of basic computer science ideas, but not as systematic or useable as the assigned text above.

Matter and Interactions, Vol. I, R.W. Chabay and B.A. Sherwood, Wiley, 2002. Although in paperback it seems very overpriced; 3 copies have been placed on course reserve in the EPSL library in the Math Building. We will get some illustrations from this book, but it does not seem worth buying unless you are flush with funds.

There are many other books worth inspection. See References, URLs

Goals

1) To learn basic ideas of computer science, especially to solve physics problems

2) To implement them by learning basic Python, an increasingly popular language that is free, works on most platforms, has minimal overhead and idiosyncrasies, is modern and object-oriented but with minor changes in updates and good backward compatibility, and provides a good launching pad to learn other languages

3) To use VPython to be able to generate visual displays of model systems quickly and to allow easy and flexible modification.

While this course satisfies the lower-level requirement in Computer Sciences for Physical Sciences majors, it is not a substitute for CMSC 106 in that it does not teach C, is not so rigorous as CMSC 106, does not satisfy the prerequisite for upper-level CMSC courses, and does not spend much time on text manipulation, data structures, etc. (except to the extent that they are important in the physics applications).

Grading

    The final exam is scheduled for Wednesday, Dec. 17, 1:30-3:30pm. A mid-term exam will be given on or about Oct. 10 (since Early-Warning Grades are due Oct. 17). The course grade will be determined by roughly the following weighting:

 
Homework 20%

Quizzes and Mid-Term Exam[s]

20%

Term Projects (2) 40%
Final Exam 20%

Academic Honesty
    Discussing and working together on assignments is encouraged. However, each student is expected to do the assigned problems and write the assigned programs independently, and hand in his or her own work for grading. For any questions about academic honesty, see University policies at: http://www.testudo.umd.edu/soc/dishonesty.html .

Regarding MidTerm Test on Oct. 10

Exam will be a practical programming exercise, held in the POWL lab on Oct. 10. It should be possible to do it in 50 minutes if you are well prepared, but since homework is taking some students longer than expected, you may have up to 75 minutes if you want. I will arrive at the POWL lab at 2:40pm and let students start when they arrive. My graduate student Tim will stay after 3:50 for stragglers. (On the Questionnaire, all students stated that they had no conflicts during 2-3pm on Fridays.)

Exam Rules:
Blank floppy disks will be provided.
Students may use any documentation or files on the course server and website.
Students may not use books, notes or any contents of their own floppy disk.

Exam Format:
Students will be asked to write a Python program that
a) creates a list or lists
b) fills the lists with numbers using a prescribed algorithm
c) creates a user-defined function to perform some basic mathematical operations
d) makes use of conditionals
e) reports the results in a prescribed format, using VPython graphical facilities

Numerical integration of Newton's equations will NOT be on this midterm test, but is crucial for your first course project; if you are having trouble with RK2, just use the Euler technique!

Sample Exam Problem from 2001 website:
Create a list of the values of f(x) for x from 0 to 51 in increments of 3, where
f(x) = 36x for odd values of x
f(x) = x**2 - 1206 for values of x that are multiples of 4
f(x) = x**2 for all other values of x
Find the values of x for which f(x) is greater than 300 and divisible by 18
Print x, f(x) in column format for these values of x.

Another Noteworthy Problem from 2001:

Try the three segments of code and make sure you understand exactly what each one is doing. (Be careful about indentation, which might be messed up due to changes to html.) Note that none of them work properly! Then change (or replace) the code to accomplish two new outcomes:
a) to list all the factorizations for each number (e.g. 2*4 and 4*2 for 8).
b) to list all the unique factorizations for each number (e.g. 2*4 only for 8, but 3*4 and 2*6 for 12).
Test your program to make sure it works for larger ranges. [Grading on this problem was based on whether the programs worked (for any range) and length of code (shorter is better)].

#VERSION 1
for n in range(2,13):


for x in range (2,n):
if n % x == 0:
print n, "equals", x, "*", n/x

else:
print n, 'is a prime number'
print "the program is done"

#VERSION 2
print "\n second trial program \n"


for n in range(2,13):


for x in range (2,n):
if n % x == 0:

print n, "equals", x, "*", n/x


break


else:
print n, 'is a prime number'


print "the program is done"


#VERSION #3
print "\n third trial program \n"


for n in range(2,13):


for x in range (2,n):
if n % x == 0:

print n, "equals", x, "*", n/x


break


print n, 'is a prime number'

print "the program is done"


Topics we have studied:

Introduction to VPython
Number types, mathematical operations, lists
Screen I/O, print-statement format
Numerical Solutions of Differential Equations: Newton's laws with Euler method and RK2
Logical expressions, conditionals, and iteration/recursion: while, for, if, else, elif
Outputing graphs with VPython: gdots, gcurve, ghistogram; gdisplay specs
User defined functions: arguments, return
Ufuncs
Operations on lists, arrays, tuples
Two-dimensional arrays (list of lists) and operations thereon
Random number generator

Quick, concise summaries such as those by Johnson and by Hetland on the References, URL page of the course website may be helpful in reviewing, so long as you do not get panicked by the inclusion of things that we have not yet studied!

Reminder of some useful references available online via course URL list:

VPython reference manual for graphical displays

Python Tutorial [by Guido van Rossum, Fred L. Drake, Jr.] is generally useful on topics we have covered, esp:

section 5.1 (& 3.1.4): list "methods" (i.e. listname.method)

section 5.1.4: list "shorthand" techniques, intrinsic rather than in NumPy

section 7.1: formatted output

section 3.1.1 & 3.1.2: arithmetic & string operations

section 4.6 & 4.7.1: functions

Numerical Python reference manual, chaps. 5 & 8 for arrays/lists, chap. 6 for ufuncs

There are lots of nice examples in Non-Programmers Tutorial for Python by Josh Cogliati, and topics are relatively easy to locate, but it is somewhat incomplete; worth a look on topics that are confusing or unclear.

Note also the function help()