computation physicssite.iugaza.edu.ps/ghossain/files/2011/02/chapter341.pdf · 2013. 10. 5. · dr...
TRANSCRIPT
Dr Maher O. El-Ghossain ١
COMPUTATION PHYSICS
PHYS 6312DR. Maher O. El-Ghossain
Associate Professor
10/5/2013 1:09:23 PM
٢
CHAPTER 3
• BASIC MATHEMATICAL PROGRAMS
• 3.1 Numerical Errors• 3.2 Numerical Differentiation• 3.3 Numerical Integration• 3.4 Finding Roots
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٣
•3.1 Numerical Error:
• Eps = 10-20/((h+10-20)-h)• Normally Equal one• Let Eps between 1 and the next floating number.• Let temp =( 10+h)-h • Fraction Error• = ABS(temp-h)/h• Plot fraction error Vs h• The Round off problem using • Double precession.
F
e
X X X
X X
X
X X X X
h 10-20 10-10 100
10-20
101
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٤
Range of Error
• Single Precession 10+37 to 10-37
• Double Precession 10+308 to 10-308
• ao lies in the single precession but numerator and dominator is not, change unit to ev and Ao will solve the problem.
682
278
112
2
0
1034.2
..1024.14
103.54
−
−
−
=
=
≈=
xem
mCkgxh
mxemha
e
o
e
o
πε
πε
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٥
Range is Problem not Unit
• N! = 1x2x3 x … (N-1)XN• For N > 170 is not possible using double
precession.• The problem is solved using Logarithm• Log N! = log 1 + log 2 + .+log(N-1)+log N• Using Stirling’s formula:• N!= (mantissa)x10exponent , • Exponent=integer part of log10(N!)• Mantissa =10(fraction part of log10(n!))
..)288
112
11(2! 2 +++= −
nnennn nnπ
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٦
3.2 Numerical Differentiation:
• a) Simple Method:• Let f(x) a function of x , fn = f(xn), xn=nh• N=0,+1,-1,…, using Taylor Expansion• f(x)= fo+xf ` + x2/2! F``+ x3/3! F```+.. • f(x=±h)= fo±hf ` ± h2/2! F``± h3/3! F```±..• f(x=±2h)= fo±2hf `± 2h2/2! F``± 4h3/3!
F```±..O(h4)., subtract f-1 from f+1• f ` = f+1-f-1 / 2h - h2/6 f````+ O(h4)• 3 point formula for small h:• f ` = f+1-f-1 / 2h +O(h2)
f-2
ff-1
f1
f2
f0
h
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٧
•Five point formula:f `=1/12h( f-2-8f-1+8f1-f2 )+o(h4)
• In Calculus the derivatives:• f `= limh-0 (f(x+h)-f(x))/h, h very small which is
the two point method can be written as : f ` = f1-f0/h is 2 point forward, and
• f ` = f0-f-1/h as 2 point backward • b) Centered Formula f `=f(x+h)-f(x-h)/2h+.• f ``= (f(x+h)+f(x-h)-2f(x))/h2 –error• Error =ABS(( True value) – (computed value))• Example1: Write Program to find derivative of
f(x)= sin(x) for x = 1, using 2, 3 point methods.10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٨
Program 1
• 10 READ (*,*) X• READ(*,*),’ENTER the value of H’• READ(*,*)H • IF(H.LE.0)STOP• FPRIME=(sin(x+h)-sin(x-h))/2*h• EXACT=COS(X)• ERROR=EXACT-FPRIME• WRITE(*,*)H,error,fprime,exact• GO TO 10• STOP• END
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٩
•3.3 Numerical Integration(Quadrature):
• Calculate the definite integral of f between two limits a and b, a<b.
• N =( a-b)/h even integral
• a)Trapezoidal Rule:Area of trapezoid
∫∫∫∫−
+
+
+
+++=b
hb
ha
ha
ha
a
b
a
dxxfdxxfdxxfdxxf2
4
2
2
)(...)()()(
)()2(2
)( 3101 hOfffhdxxf
h
h
+++= −−∫
ff-1
f1
f2
f0
h10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٠
•b)Simpson’s Rule:
• The integral from a to b:
))()(4....
)3(4)2(2)(4)((3
)(
)()4(3
)( 3101
bfhbf
hafhafhafafhdxxf
hOfffhdxxf
b
a
b
a
+−++
++++++=
+++=
∫
∫ −
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١١
•C) Bode’s Rule:
• The Integral from a to b is:
)(732
12327(452)(
)()33(8
3)(
743
210
53210
4
0
3
0
hOff
fffhdxxf
hOffffhdxxf
x
x
x
x
+++
++=
++++=
∫
∫
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٢
•Example 2
• Write a FORTRAN program to calculate the Integral of ex between a=0, b=1, using trapezoidal rule, Simpson’s Rule , and Bode’s Rule compare with exact value of the integration?
• Where the exact value is:
7182.111
0
=−=∫ edxe x
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٣
•Program 2• C The Integral of a function using T. R• Write(*,*)’enter N even’• 30 Read(*,*)N• if(N.LT.2)stop• H=1/N• SUM=EXP(0)• DO 10 I=1,N-1
• X=I*H• 10 SUM=SUM+2*EXP(x)
XINT=(SUM+EXP(1))*H/2.0• EXACTI=EXP(1.0)-1.0• ERROR=EXACTI-XINT• Write(*,*)N,XINT,EXACTI,ERROR• go to 30• stop• end
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٤
•1.3 FINDING ROOTS
• Roots of a function f(x) , finding x0 for which f(x0) = 0.
• a)Bisection Method:• X є (a, b) for f(x) =0, divide the region into
equal parts, x1=(a+b)/2, f(a) f(x1)<0• And f(x1)f(b) >0, roots between (a,b), f(a)
f(b)<0 , x2=(a+x1)/2 otherwise , • x2=(x1+b)/2 repeat untill < error • For example 3: f(x) = ex ln x - x2 , x=1, f(x)=-1• X=2 f(x) = 1 , so there is x0 є (1,2) so that f(x0)=0• Write program for this example.10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٥
•Program 3• C PROGRAM ROOTS BISECTIONS• C Roots of f(x) = exp(x) *ln(x) –x*x• DL = 1.0 E –06• A=1.0• B= 2.0 • Istep = 0• Do 100 while (abs(Dx).GT.DL)• x0= (A+B)/2.0• If( (f(A).f(x0)).LT.0) then• B=x0
• Dx= B-A• ELSE• A=x0
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٦
• Dx = B-A• END If• Istep = Istep +1• 100 END DO• WRITE(6,999) Istep, x0,Dx• stop• 999 Format(I4,2F16.8)• End • Function F(x)• F = EXP(x)*ALOG(x)-x*x• Return • END
• X0=1.694600 , error 0.00001 (Istep =20)
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٧
•b) Newton Method:
• Fn = f(xn) , let f( x0) = 0 , expand around the root x0,
• F(x0) = f(x) + (x-x0) f `(x)+…=0• X trial value of root of x0 at nth steps,• F(xn+1) =f(xn) + ( xn+1-xn) f `(xn) = 0• That is in iterative scheme• Xn+1 = xn – fn / fn` , this is called Newton
Method or Newton Rapgson Method.10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٨
•c ) Secant Method:
• Replace fn` in newton method from two point formula from the two point formula:
• Xn+1 = xn – ( xn – xn-1 ) fn /(fn – f n-1)• Which called discrete Newton method or secant
method .• Example 4: Write program to find the root of x2 –
5 = f(x) , x0 = 51/2 =2.236058 to a tolerance of =10-6, using the initial guess x=1 in steps of 0.5.
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
١٩
•Program 4
• C Program calculate roots of x*x – 5 = 0 FUNC(X)=X*X-5 .
TOLX=1.E-06 X=1 . FOLD=FUNC(X) DX=.5 ITER=0
10 CONTINUE ITER=ITER+1 X=X+DX PRINT *,ITER,X,SQRT(5.)-X IF ((FOLD*FUNC(X)) .LT. 0) THEN X=X-DX DX=DX/2 END IF
IF (ABS(DX) .GT. TOLX) GOTO 10 STOP
END10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٠
CHAPTER 4Ordinary Differential Equation
• The general form of ordinary differential equation is:
• dy/dx = f ( x, y ),
• Motion of a particle in a force field :• M d2z/dt2 = F ( z ) , second order d. eq.• P(t) = M dz/dt , dP/dt =F(z) , dz/dt =p/M
• Solving for y (x) ??????10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢١
•4.1 (a) Simple Method:
• Consider initial condition, yo=(y(x=0))
• dy/dx= f(x, y), replace dy/dx by its’ derivatives:
• dy/dx = (yn+1- yn)/h + O(h) = f (xn,yn),
• The Recursion relation yn+1 interms of yn:
• Yn+1 = yn + h f (xn, yn) + O (h2)10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٢
•(b) Taylor Method:
• Using Taylor Expansion of yn+1 about yn
• Yn+1=y(xn+h)=yn+hy`n + ½ h2 yn``+O (h3)
• y`n= f(xn,yn), yn``=df/dx ( xn,yn), • dy/dx= f(xn, yn)
• yn``= df/dx + df/dy dy/dx (partial derivatives)
• Then,• Yn+1 =yn + h f + ½ h2 ( df/dx + f df/dy) + O (h3)
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٣
•4.2 Multistep and Implicity Method
• To get higher accuracy is to use recursion that relate yn+1 not just to yn, but also to other points like yn-1,yn-2, …
• dy/dx = f ( x , y ) , Integrate• Yn+1 = yn +
• Using a linear extrapolation of f over required interval,
• f = (x – xn-1)/h fn - (x-xn)/h fn-1 + O (h2)
∫+ 1
),(n
n
x
x
yxf
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٤
• Substitute into f(x,y) and integrate,
• Yn+1 =yn + h(3/2 fn - ½ fn-1) + O (h2 )• Which is Adams – bash forth two- step Method.
• If f is extrapolated by cubic polynomial fitted to fn, fn-1, fn-2, and fn-3 then A dams – Bash fort four step method result:
• Yn+1= yn + h/24 (55 fn – 59 fn-1 + 37 fn-2 – 9 fn-3 ) + O ( h4) .
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٥
• These method discussed so far are all explicit in which yn+1 directly is giving in terms of yn.
• Implicit methods, in which an equation must be solved to determine yn+1 after yet
• Another mean of higher accuracy.• Consider dy/dx\x+1/2 =f(xn+1/2,yn+1/2)
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٦
• Xn+1/2=(n+1/2) h midway between two points• (yn+1-yn)/h + O(h2) = ½( fn +fn+1)+o(h3)
• Yn+1=yn+1/2h(f(xn,yn)+f(xn+1,yn+1)+O(h3)
• Which is Crumer or midpoint method.• If f is linear in y, so f(x,y)=g(x)y, last equation
can be written as :
• Yn+1=((1+1/2g(xn)h)/(1-1/2g(xn+1)h))yn
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٧
• The Adams-Moulten both multistep and implicit two step method can be derived from:
• yn+1=yn + • Using quadratic polynomial passing
through fn-1,fn,fn+1
• f=(x-xn)(x-xn-1)/h2 fn+1-(x-xn+1)(x-xn-1)/h2
fn-1 + (x-xn+1)(x-xn)/h2 fn-1+ O (h2)
∫+ 1
),(n
n
x
x
yxf
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٨
• Implicit relation can be obtained if f interpolated over a region from xn to xn+1,
• Yn+1 = yn+ h/12(5 fn+1+8fn-fn-1)+ O (h4)
• Using Cubic polynomial , the three step formula :
• Yn+1=yn+h/24(9 fn+1+19fn-5fn+fn-2) + O (h5)
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٢٩
Example 1:
• Consider the differential equation and boundary condition:
• dy/dx = - x y ; y(0) = 1, • whose solution is:• Y = Exp (- x2/2),
• write Fortran program to solve it using different methods.
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٣٠
• C PROGRAM 1 • FUNC(X,Y)=-X*Y • PRINT * ' Enter step size ( .le. 0 to stop)' • READ *, H • IF (H .LE. 0.) STOP NSTEP=3./H • Y=1. • DO 10 IX=0,NSTEP-1• X=IX*H • Y=Y+H*FUNC(X,Y)• DIFF=EXP(-0.5*(X+H)**2)-Y• PRINT *, IX,X+H,Y,DIFF10• CONTINUE • GOTO 20 • END
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٣١
4.3 Runge –Kutta Methods:
• The second order Runge Kutta algorithm, by Taylor series expansion about the mid point of integration,
• Yn+1 = yn + h f (xn+1/2, yn+1/2) + O (h3)• let k be : k = h f(xn,yn)
• Yn+1= yn + h f ( xn+1/2h, yn+1/2k)+ O (h3)• Which is the second order Runge Kutta
Algorithm.10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٣٢
• Runge – Kutta schemes of higher order can be derived, yn+1=yn +
• Using Simpson’s Rule :• Yn+1 = yn +h/6( f(xn,yn)+4 f(xn+1/2,yn+1/2)• + f( xn+1,yn+1) + O (h5)• k1=hf(xn,yn),k2=hf(xn+1/2h,yn+1/2k),• k3=hf(xn+h,yn-k1+2k2)• Yn+1= yn +1/6 ( k1+ 4k2+ k3)+ O (h4)• Which is the second order R. K. M.• Fourth Order Runge-Kutta Method:• Yn+1=yn + 1/6 ( k1 + 2k2+ 2 k3+ k4) + O (h5)
∫+ 1
),(n
n
x
x
yxf
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٣٣
EXAMPLE 2
• Calculate the solution of :
• dy/dx = -y , with y(x= 0) = 1.0
• Solution y = e-x
• Solve for y and compare with exact (error).10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٣٤
C PROGRAM 2• PRINT *,' Enter value of step size ( .le. 0 to stop)'
READ *, H IF (H .LE. 0) STOP YMINUS=1YZERO=1.-H+H**2/2NSTEP=6./H DO 20 IX=2,NSTEP X=IX*H YPLUS=YMINUS-2*H*YZERO YMINUS=YZERO YZERO=YPLUS EXACT=EXP(-X) PRINT *, X,EXACT,EXACT-YZERO
20 CONTINUE GOTO 10END
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain
٣٥
INPUT and OUTPUT FILE
•OPEN(unit=12,file=‘info’,status=‘old’)•Read(12,*)code,temp,pres
•Open(unit=6,file=‘out’,status=‘new’)•Write(6,*)x,y
10/5/2013 1:09:23 PM Dr Maher O. El-Ghossain