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


Lec1_图文.pdf

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

机器人技术-仿人机器人_Lec1_图文.pdf

机器人技术-仿人机器人_Lec1_机械/仪表_工程科技_专业资料。仿人机器人 基

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

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

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

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

lec1_图文.pdf

lec1 - MIT ICAT 16.72 Air Traffic Contro

lec1_022311_图文.pdf

lec1_022311 - Lecture 1: Introduction 第一

Lec1_EDA1_图文.ppt

Lec1_EDA1 - 集成电路EDA技术 集成电路EDA技术 EDA 主讲:

BI565-TechMan-lec1-taster1_图文.pdf

BI565-TechMan-lec1-taster1 - BI5651 Tech

lec1_PP_图文.ppt

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

过程辨识Lec1_图文.ppt

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

材料物理性能-lec1_图文.ppt

材料物理性能-lec1 - 材料物理性能 杜宇雷 材料科学与工程学院 2010年

cs11-cpp-lec1_图文.pdf

cs11-cpp-lec1 - CS11 Introduction to C

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

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

Hinton lecture_slides-lec1_图文.ppt

Hinton lecture_slides-lec1 - Neural Netw

LEC1-ER_图文.pdf

LEC1-ER - Entity-Relationship Model Prof

lec1-introduction_图文.ppt

lec1-introduction - University of Electr

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

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

Lec1-upload_图文.ppt

Lec1-upload - 工程流体力学 本课件仅供天津大学机械系学生自学及复习

SPR-Spring2012-Lec1-Overview_图文.pdf

SPR-Spring2012-Lec1-Overview - Statistic

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

软件体系结构Lec1-1 - 软件体系结构 主讲老师:周立新 2012年6月18