kl800.com省心范文网

ch1-lec1_图文

PHY 410-505 Computational Physics I

Chapter 1: A First Numerical Problem
Lecture 1

Monday August 25, 2008

1

LECTURE OUTLINE

LECTURE OUTLINE

Lecture Outline Course Information Computational Physics Computation and Algorithms . . . . . . . . . . . . . . . . . . . . A First Numerical Problem Real world phenomenon . . Mathematical Model . . . . Computational problem . . . Numerical algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 5 5 7 7 7 7 8 9 9 10 11 13 13

Computer Programs in Mathematica and First “Hello world” programs . . . . . . . . Mathematica decay program . . . . . . . . C++ decay program . . . . . . . . . . . . Maple decay program . . . . . . . . . . . . Display data using Gnuplot . . . . . . . . .
2

C++ . . . . . . . . . . . . . . . . . . . .

COURSE INFORMATION

Course Information Computational Physics, Giordano and Nakanishi, Second Edition Chapters 1–7, approximately 2 weeks per chapter Math prerequisites: di?erential equations, linear algebra Physics: undergraduate mechanics, electrodynamics, statistical physics Numerical analysis: Textbook appendices, Numerical Recipes Programming language: C++. (Can use Fortran or Java, but you are on your own.) Computational systems: Mathematica or Maple – use optionally as fancy calculator – available on UB computers and UBMicro Homework, exams, grades: Weekly Homework Assignments (?3 textbook problems) 50% Mid-term Exam (October 19) 15% Final Exam 25% Class Participation 10%
3

COURSE INFORMATION

Assignments will involve writing codes, running simulations, and analyzing results Learn to code by studying example programs and making changes Not di?cult, but ?nding and correcting bugs requires time and patience! Discuss assignments with classmates, but coding, simulation and writeup must be your own work. Pass = 50%, A ≈ 85% Operating systems and software You can use Windows, Mac OS X, or Linux (ubuntu or UBLinux) All needed software is available for free Windows: Microsoft Visual Studio Express 2008 or Dev-C++ Mac OS X: Xcode Linux: GCC plus Emacs or Vi; KDevelop Gnuplot for graphics, OpenGL for graphics and animation
4

COMPUTATIONAL PHYSICS

Computational Physics Physical System: In Science and Engineering we study physical phenomena in the real world – usually an isolated self-contained system – e.g., pendulum oscillations under gravity Mathematical Model: Approximate the system using a mathematical model – e.g., an ordinary di?erential equation d2θ g = ? sin θ 2 L dt Computer Simulation: Exact analytical solution may not be available – use a digital computer to generate an approximate numerical solution Code Veri?cation: Is the numerical solution accurate? Error analysis Model Validation: Does the model and numerical solution agree with the real world – compare with experiment – data analysis Computation and Algorithms Solutions of a mathematical model are functions
5

Computation and Algorithms

COMPUTATIONAL PHYSICS

A function is a map from a set of input points (domain of the function) to a set of output points (range of the function) A computation is a process that generates an output point from a given input – a computation evaluates the function An algorithm is a procedure (or recipe) that transforms an input point into an output point in a sequence of steps In a digital computation, the domain and range are discrete sets, and the algorithm terminates after a ?nite number of steps The computational complexity of an algorithm is important – it must give a result in a reasonable number of steps (time complexity) and using a reasonable amount of resources, e.g., computer memory (space complexity)

6

A FIRST NUMERICAL PROBLEM

A First Numerical Problem Real world phenomenon A collection of radioactive nuclei are observed to decay Measure decay times, measure mean lifetime τ Mathematical Model Approximate discrete number of nuclei by real variable N (t) Di?erential equation dN N =? dt τ Exact solution N (t) = N (0)e?t/τ

Model validation: N must be large Computational problem Given N (0), τ, t ?nd N (t)
7

Numerical algorithm

A FIRST NUMERICAL PROBLEM

Numerical algorithm Use Euler’s algorithm §1.2 and Appendix A Discretize t using a time step ?t Taylor series dN (t) N (t + ?t) = N (t) + ?t + . . . dt dots represent error in this approximation Use N dN =? dt τ Iterative (marching) algorithm: Given N (0) ?nd N (?t) = N (0) ? N (0)?t/τ Using N (?t) ?nd N (2?t) = N (?t) ? N (?t)?t/τ Repeat as often as needed Monitor (. . . ) errors which accumulate in each step Code veri?cation: Compare numerical results with exact solution
8

COMPUTER PROGRAMS IN MATHEMATICA AND C++

Computer Programs in Mathematica and C++ First “Hello world” programs Mathematica: create a ?le hello.m with the following code:
Print["Hello, world!"]

Start Mathematica and load the code (press Shift-Enter):
In[1]:= << hello.m

C++: create a ?le hello.cpp with the following code:
hello.cpp #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; }
9

Mathematica decay program

COMPUTER PROGRAMS IN MATHEMATICA AND C++

Compile and execute the code:
> g++ -o hello hello.cpp > hello

Mathematica decay program Type the following code directly in Mathematica, or load it from a ?le decay.m.
decay.m (* Get input from user *) n = Input["Enter initial number of nuclei: "] tau = Input["Enter decay time constant tau: "] dt = Input["Enter time step dt: "] tMax = Input["Enter time to end simulation: "] (* Initialize variables and output list *) t = 0 result = {{t, n}} (* Calculate results and store in list *) While[t < tMax, n -= n / tau * dt;
10

C++ decay program

COMPUTER PROGRAMS IN MATHEMATICA AND C++

t += dt; result = Append[result, {t, n}] ] (* Plot output and exact solution *) euler = ListPlot[result, DisplayFunction->Identity] exact = Plot[100 Exp[-t], {t, 0, 5}, DisplayFunction->Identity] Show[euler, exact, DisplayFunction->$DisplayFunction]

C++ decay program
decay.cpp #include <cmath> #include <fstream> #include <iostream> #include <string> using namespace std; int main() { // Get input from user cout << "Enter initial number of nuclei: "; double n; cin >> n; cout << "Enter decay time constant tau: ";

11

C++ decay program

COMPUTER PROGRAMS IN MATHEMATICA AND C++

double tau; cin >> tau; cout << "Enter time step dt: "; double dt; cin >> dt; cout << "Enter time to end simulation: "; double tMax; cin >> tMax; cout << "Enter name of output file: "; string name; cin >> name; // Initialize variables and open output file double t = 0; ofstream file(name.c_str()); file << t << ’\t’ << n << ’\n’; // Calculate results and store in file while (t < tMax) { n -= n / tau * dt; t += dt; file << t << ’\t’ << n << ’\n’; } // Close file

12

Maple decay program

COMPUTER PROGRAMS IN MATHEMATICA AND C++

file.close(); cout << "Results stored in " << name << endl; }

Maple decay program Display data using Gnuplot C++ does not have standard graphics built in. Use Gnuplot (or Mathematica, Excel, or any plotting program) to display the results.

13

Display data using Gnuplot

COMPUTER PROGRAMS IN MATHEMATICA AND C++

Radioactive decay: Time Constant = 1 s 100 90 80 70 Number of Nuclei 60 50 40 30 20 10 0 0 1 2 time (s) 3 4 5 tau=0.5 tau=0.2 tau=0.05 Exact

14


ch1-lec1_图文.pdf

ch1-lec1 - PHY 410-505 Computational Phy

ch1-Signals and Systems-lec[1-1](1)_图文.pdf

ch1-Signals and Systems-lec[1-1](1)_信息与通信_工程科技_专业资料。Spring?2014 信号与系统 Signals?and?Systems Chapter?1 Signals?and?Systems Lecture?1...

lec1_图文.pdf

lec1 - 课程介绍 15.511 公司会计 暑期 2004 SP Kotha

软件体系结构Lec1-1_图文.ppt

软件体系结构Lec1-1 - 火龙果?整理 uml.org.cn 软件体系结构

Lec1_图文.pdf

Lec1 - 第一章:Web技术概述 主讲人:邹兆年 哈工大计算机学院 数据与知

2015-aos-lec1-intro-1_292606723_图文.pdf

2015-aos-lec1-intro-1_292606723_临床医学_医药卫

Lec-1_图文.pdf

Lec-1 - Digital Image Processing Introduction Lecture 1 1 Literatures Textbook: Digital Image...

lec1_022311_图文.pdf

lec1_022311 - Lecture 1: Introduction 第一

Lec1_概述_图文.ppt

Lec1_概述 - 高级语言程序设计 程序设计基础 ? 2012秋季 C++ 程

cs11-cpp-lec1_图文.pdf

cs11-cpp-lec1 - CS11 Introduction to C

过程辨识Lec1_图文.ppt

过程辨识Lec1 - 系统辨识 System Identification 上海

Lec+1_图文.ppt

Lec+1 - Thermodynamics: An Engineering A

LEC1-ER_图文.pdf

LEC1-ER - Entity-Relationship Model Prof

LEC1锅炉程序控制器_图文.pdf

LEC1锅炉程序控制器 - January 1997 7 761 FM739 LEC1... Control Unit Series 02 Control unit for doubl...

lec1_PP_图文.ppt

lec1_PP - 中科大计算机并行编程课件-1... lec1_PP_IT/计

lec1-信息隐藏技术概论_图文.pdf

lec1-信息隐藏技术概论 - 《信息隐藏技术》 任德斌 本课程的目的和要求 ?

lec1-2_图文.doc

lec1-2 - Doing Business in Singapore Thi

lec1---实例及解答_图文.ppt

lec1---实例及解答 - EX 1 家具生产的安排 家具公司生产桌子和椅子,

lec1-introduction_图文.ppt

lec1-introduction - University of Electr

Lec-1_图文.ppt

Lec-1 - 如何撰写和发表高水平的科研论文 1. 选题与创新 2. 构思与撰