Computational Physics With Python - UNIOS

Computational Physics With Python

Dr. Eric Ayars California State University, Chico

ii

Copyright c 2013 Eric Ayars except where otherwise noted. Version 0.9, August 18, 2013

Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi

0 Useful Introductory Python

1

0.0 Making graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 1

0.1 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

0.2 Reading data from files . . . . . . . . . . . . . . . . . . . . . 6

0.3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1 Python Basics

13

1.0 The Python Interpreter . . . . . . . . . . . . . . . . . . . . . 13

1.1 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.2 Simple Input & Output . . . . . . . . . . . . . . . . . . . . . 16

1.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

1.4 Mathematical Operators . . . . . . . . . . . . . . . . . . . . . 27

1.5 Lines in Python . . . . . . . . . . . . . . . . . . . . . . . . . . 28

1.6 Control Structures . . . . . . . . . . . . . . . . . . . . . . . . 29

1.7 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

1.8 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

1.9 Expanding Python . . . . . . . . . . . . . . . . . . . . . . . . 40

1.10 Where to go from Here . . . . . . . . . . . . . . . . . . . . . . 43

1.11 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

2 Basic Numerical Tools

47

2.0 Numeric Solution . . . . . . . . . . . . . . . . . . . . . . . . . 47

2.0.1 Python Libraries . . . . . . . . . . . . . . . . . . . . . 55

2.1 Numeric Integration . . . . . . . . . . . . . . . . . . . . . . . 56

2.2 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . 66

2.3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

iv

CONTENTS

3 Numpy, Scipy, and MatPlotLib

73

3.0 Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

3.1 Scipy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

3.2 MatPlotLib . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

3.3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4 Ordinary Differential Equations

83

4.0 Euler's Method . . . . . . . . . . . . . . . . . . . . . . . . . . 84

4.1 Standard Method for Solving ODE's . . . . . . . . . . . . . . 86

4.2 Problems with Euler's Method . . . . . . . . . . . . . . . . . 90

4.3 Euler-Cromer Method . . . . . . . . . . . . . . . . . . . . . . 91

4.4 Runge-Kutta Methods . . . . . . . . . . . . . . . . . . . . . . 94

4.5 Scipy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

4.6 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

5 Chaos

109

5.0 The Real Pendulum . . . . . . . . . . . . . . . . . . . . . . . 110

5.1 Phase Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

5.2 Poincar?e Plots . . . . . . . . . . . . . . . . . . . . . . . . . . 116

5.3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

6 Monte Carlo Techniques

123

6.0 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . 124

6.1 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

6.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

7 Stochastic Methods

131

7.0 The Random Walk . . . . . . . . . . . . . . . . . . . . . . . . 131

7.1 Diffusion and Entropy . . . . . . . . . . . . . . . . . . . . . . 135

7.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

8 Partial Differential Equations

141

8.0 Laplace's Equation . . . . . . . . . . . . . . . . . . . . . . . . 141

8.1 Wave Equation . . . . . . . . . . . . . . . . . . . . . . . . . . 144

8.2 Schr?odinger's Equation . . . . . . . . . . . . . . . . . . . . . . 147

8.3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

A Linux

155

A.0 User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 156

A.1 Linux Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

A.2 The Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

CONTENTS

v

A.3 File Ownership and Permissions . . . . . . . . . . . . . . . . . 162 A.4 The Linux GUI . . . . . . . . . . . . . . . . . . . . . . . . . . 163 A.5 Remote Connection . . . . . . . . . . . . . . . . . . . . . . . . 163 A.6 Where to learn more . . . . . . . . . . . . . . . . . . . . . . . 165 A.7 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

B Visual Python

169

B.0 VPython Coordinates . . . . . . . . . . . . . . . . . . . . . . 171

B.1 VPython Objects . . . . . . . . . . . . . . . . . . . . . . . . . 171

B.2 VPython Controls and Parameters . . . . . . . . . . . . . . . 174

B.3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

C Least-Squares Fitting

177

C.0 Derivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

C.1 Non-linear fitting . . . . . . . . . . . . . . . . . . . . . . . . . 181

C.2 Python curve-fitting libraries . . . . . . . . . . . . . . . . . . 181

C.3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

References

185

vi

CONTENTS

Preface: Why Python?

When I began teaching computational physics, the first decision facing me was "which language do I use?" With the sheer number of good programming languages available, it was not an obvious choice. I wanted to teach the course with a general-purpose language, so that students could easily take advantage of the skills they gained in the course in fields outside of physics. The language had to be readily available on all major operating systems. Finally, the language had to be free. I wanted to provide the students with a skill that they did not have to pay to use!

It was roughly a month before my first computational physics course began that I was introduced to Python by Bruce Sherwood and Ruth Chabay, and I realized immediately that this was the language I needed for my course. It is simple and easy to learn; it's also easy to read what another programmer has written in Python and figure out what it does. Its whitespace-specific formatting forces new programmers to write readable code. There are numeric libraries available with just what I needed for the course. It's free and available on all major operating systems. And although it is simple enough to allow students with no prior programming experience to solve interesting problems early in the course, it's powerful enough to be used for "serious" numeric work in physics -- and it is used for just this by the astrophysics community.

Finally, Python is named for my favorite British comedy troupe. What's not to like?

CONTENTS

vii

viii

CONTENTS

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

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

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches