PDF A Lab Manual - Weber State University

Physics Simulations in Java

A Lab Manual

Daniel V. Schroeder Physics Department Weber State University Version 2.4, August 2011

Copyright and Permissions

This lab manual is copyright c 2005?2011 by Daniel V. Schroeder. Permission is hereby granted to reproduce this manual for noncommercial pur-

poses. All reproductions must include this copyright notice. You may download the latest version of this manual from the author's web site,

. The TEX source code for this manual is also available, so other instructors may

modify it to suit their needs. All modifications must be for noncommercial purposes and must carry the same copyright (adding the names of the additional authors) and permissions as the original. To obtain the source code please visit the web site or contact the author.

2

Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Project 1: Hello, World! . . . . . . . . . . . . . . . . . . . . . . . 11 Project 2: Range of a Projectile . . . . . . . . . . . . . . . . . . . . 23 Project 3: Adding Sine Waves . . . . . . . . . . . . . . . . . . . . . 35 Project 4: Simulating Projectile Motion . . . . . . . . . . . . . . . . 39 Project 5: Pendulum . . . . . . . . . . . . . . . . . . . . . . . . . 51 Project 6: Orbits . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Project 7: Molecular Dynamics . . . . . . . . . . . . . . . . . . . . 77 Project 8: Random Processes . . . . . . . . . . . . . . . . . . . . . 93 Project 9: The Ising Model . . . . . . . . . . . . . . . . . . . . . 101 Java Language Words . . . . . . . . . . . . . . . . . . . . . . . 111 Java Classes Used in This Manual . . . . . . . . . . . . . . . . . . 112 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

3

Preface

Introductory physics courses are full of simplifications: projectiles fly without air resistance, pendulums swing only at small angles, orbits are always circular, and no more than two particles move at any time. These kinds of simplifications are necessary and appropriate when you're first trying to understand the basic laws of nature. But the real world is far more complex, and far more interesting. Because the ultimate goal of physics is to understand the real world, students deserve a course that applies the laws of physics to more complex situations.

Fortunately, modern electronic computers make it possible to perform extremely lengthy calculations in a negligible amount of time. These days, therefore, computers offer the best avenue toward applying the basic laws of nature to complex and realistic physical systems. A computer program that models the behavior of a physical system is called a computer simulation. Creating and using computer simulations is an integral part of modern science and engineering.

This lab manual is intended for a hands-on introductory course in computer simulations of physical systems, using the Java programming language. The goals of the course are as follows: ? Learn enough of the Java language to write programs that do numerical calcu-

lations with graphical output; ? Learn some special techniques for doing mathematical calculations (such as solv-

ing differential equations) on a computer; ? Gain a better understanding of Newton's laws and other physical principles; ? Study a variety of physical systems that are too complex for simple pencil-and-

paper calculations, and see what sorts of phenomena emerge in such systems.

Prerequisites

Before taking this course you should have completed a semester of introductory physics, covering Newton's laws of motion, conservation principles, and a bit of thermodynamics. You should also have taken at least one semester of calculus. Prior experience in programming a computer is not required, but you should be fairly comfortable using word processing and spreadsheet software.

4

Preface 5

Required Materials

Naturally, you'll need a computer. You should have access to a computer lab with suitably equipped computers. If you would like to use your own computer instead (or in addition), you'll need to install and configure the needed software--the Java language and a programmer's text editor--as described in Project 1. Any reasonably new computer running Windows, Macintosh OS X, or Linux should work fine.

You'll also need a convenient way of backing up your programs and other files. I recommend a USB flash memory device; even the smallest available sizes are more than adequate.

Finally, you'll need a few low-tech materials such as scratch paper, pencils, and a 3-ring binder to hold this manual.

How To Use This Manual

This manual is divided into nine chapters, corresponding to nine separate projects. In each project you will write a computer program or (more often) a small number of closely related computer programs. Rather than giving you complete programs to run, this manual provides only code fragments and general instructions on how to write your programs. This way, once you have completed each program, it will be yours.

As you create your computer programs, you will inevitably have questions and encounter difficulties. While you should try to think things through for yourself whenever possible, don't spend too much time being stuck and getting frustrated. Ask your instructor or your classmates for help. This is not a test.

Exercises and questions are sprinkled among the instructions in this manual, with space for you to write your answers. Please work each exercise and answer each question immediately, before you read on.

The general premise of this manual is that you'll learn more by trying something than by reading a comprehensive explanation of it. Computer languages are like ordinary languages in this respect: We normally learn new words by hearing, reading, and using them in context, not by studying a dictionary. But whenever you want to see a term clearly defined, be sure to consult the glossary at the end of this manual. There's some other reference material there as well.

Computer programming is fun because it's so open-ended. You'll constantly think of things to try that go beyond the explicit instructions in this manual. By all means, try anything you want! If you're not sure how to add a certain feature to one of your simulations, or if you're not sure whether it's practical to do so within a limited amount of time, be sure to ask your instructor.

When you finish a project, remove its pages from this manual and staple them together with any printed output from your programs. This stapled packet, together

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download