Sie sind auf Seite 1von 9

Numerical Practice No2 | Bahadır Türkyılmaz

Numerical Practice N◦2


International Master of Turbulence Lille
2019-2020

Numerical Differentiation

Numerical differentiation is one of the most used techniques in numerical methods. In this
practice, Taylor Expansion based methods of backward, forward and central difference schemes
of various orders and 4th order Pade Scheme are utilized to different functions in the range of [-
1,1].

The schemes used in this practice can be seen below.

2nd Order Forward Scheme


(∂f/∂x)i ≈ (−3f(x+0h)+4f(x+1h)−1f(x+2h))/2h
2nd Order Central Scheme
(∂f/∂x)i ≈ (−1f(x−1h)+1f(x+1h))/2h
2nd Order Backward Scheme
(∂f/∂x)i ≈ (1f(x−2h)−4f(x−1h)+3f(x+0h))/2h

4th Order Forward Scheme


(∂f/∂x)i ≈ (−25f(x+0h)+48f(x+1h)−36f(x+2h)+16f(x+3h)−3f(x+4h))/12h
4th Order Central Scheme
(∂f/∂x)i ≈ (1f(x−2h)−8f(x−1h)+8f(x+1h)−1f(x+2h))/12h
4th Order Backward Scheme
(∂f/∂x)i ≈ (3f(x−4h)−16f(x−3h)+36f(x−2h)−48f(x−1h)+25f(x+0h))/12h

6th Order Forward Scheme


(∂f/∂x)i≈(−147f(x+0h)+360f(x+1h)−450f(x+2h)+400f(x+3h)−225f(x+4h)+72f(x+5h)−10f(
x+6h))/60h
6th Order Central Scheme
(∂f/∂x)i≈(−1f(x−3h)+9f(x−2h)−45f(x−1h)+ 45f(x+1h)−9f(x+2h)+1f(x+3h))/60h
6th Order Backward Scheme
(∂f/∂x)i≈(10f(x−6h)−72f(x−5h)+225f(x−4h)−400f(x−3h)+450f(x−2h)−360f(x−1h)+147f(x
+0h))/60h

At the boundaries keeping same scheme is not possible, under this condition 2 different
approach can be enhanced: using artificial points and swiping direction of scheme while
conserving order. In this report latter is used.

1
Numerical Practice No2 | Bahadır Türkyılmaz

Pade Scheme for points from 1 to N-1


(∂f/∂x)i-1+4(∂f/∂x)i+(∂f/∂x)i+1=3*(fi+1-fi-1)/h
Pade Scheme for point 0
(∂f/∂x)0+2(∂f/∂x)1=(-2.5*f0+2*f1+0.5*f2)/h
Pade Scheme for point N
(∂f/∂x)N+2(∂f/∂x)N-1=(2.5*fN-2*fN-1-0.5*fN-2)/h
Using these expressions, a coefficients matrix is formed and they are solved applying
Thomas Algorithm. The errors for all schemes are calculated by evaluating norm-2 of
the error.
The First Function
The first function is u(x)=sin(πx) and its derivative is ∂u(x)/∂x=πcos(πx).

Figure 1. Derivative of function and estimation of it for 6th order Backward Finite Difference Scheme(a)
2
Numerical Practice No2 | Bahadır Türkyılmaz

and detailed comparison in arbitrary location are seen.

The trend in Figure 1 is observed for all scheme types and all locations. Other comparison
graphs can be drawn using MATLAB code of ‘graphofcalculated1.m’. Calculated data
for each method can be found files of ‘datacalcualted%%.txt’. The calculation codes are
can be found name of ‘q1findifsch.f90’ and ‘q1padesch.f90’.
Each discrete size determined as Δx=2/n where N=2**k+1. The Figures 2,3, and 4
represent error plots for second order finite difference schemes for k=4:10 in logarithmic
scale. The red lines in the Figures are linear fit of error curves. The form of fit is y=a*x+b,
the coefficients for every Finite Difference scheme can be found in Table 1. The slopes
linear fit is very close to order of scheme, even though the relative difference between
slope and order can increase. The first reason of the difference is that schemes cannot be
applied purely in every point, for instance in boundaries different schemes are embraced
and structure is changed. The second reason is when error curves become more curved
and quality of linear fit reduces. In addition, in Figure 5 error plot for forth order Pade
scheme for k=4:20 can be seen. In graph, it is observed that error decreases with decrease
of discretization size until some point, however after that point it starts to increase. The
reason of this trend is in the first region truncation error reduces and round-off error is
not significant, then in the second region the round-off error becomes significant and
increases total error.

Figure 2. Error Plot of Second Order Backward Finite Difference


3
Numerical Practice No2 | Bahadır Türkyılmaz

Figure 3. Error Plot of Second Order Center Finite Difference

Figure 4. Error Plot of Second Order Forward Finite Difference


4
Numerical Practice No2 | Bahadır Türkyılmaz

Method a b
Second O Forward -1.89808778227885 1.60181693421081
Second O Centered -2.44595709303987 3.15250305169143
Second O Backward -2.49091362114419 4.03036620671545
Forth O Forward -4.34216741444836 9.67129012123499
Forth O Centered -4.09078555003972 6.84053841170034
Forth O Backward -3.33628176743672 2.52356720525630
Sixth O Forward -6.37135104934356 11.7666803609797
Sixth O Centered -6.40153110968432 1.78965879214528
Sixth O Backward -6.67478041144824 11.9970650011996
Forth O Pade -4.41943139147760 7.24333652325779
Table 1. Coefficients of linear fit of error

Figure 5. Error graph of Forth Order Pade FD for increased number of iteration

5
Numerical Practice No2 | Bahadır Türkyılmaz

The Second Function


u(x) du/dx d2u/dx2 d3u/dx3
x<0 sin(πx) πcos(πx) -π2sin(πx) -π3cos(πx)
Exact value 0 π 0 -π3
close to 0
x>0 πxe-x^2 πe-x^2-2πx2e-x^2 -6xπe-x^2+4πx3e-x^2 -6πe-x^2+24πx2e-x^2
-8πx4e-x^2
Exact value 0 π 0 -π
close to 0
x=0 0 0 0 0
Calculated 3.16254207 -54.223114 518.5703
value close to 0
Forward 2
Calculated 3.14125204 10.862091 -22.7017136
value close to 0
Center 2
Calculated 3.12125236 -54.265900 -567.4922
value close to 0
Backward 2
Calculated 3.15546925 -76.133911 3506.906
value close to 0
Forward 4
Calculated 3.14147425 8.148346 -21.8004036
value close to 0
Center 4
Calculated 3.13773187 -76.171082 -3553.281
value close to 0
Backward 4
Calculated 3.14972713 -111.62451 3810.125
value close to 0
Forward 6
Calculated 3.14146852 6.195831 -21.3766098
value close to 0
Center 6
Calculated 3.12998722 -111.65771 -3875.250
value close to 0
Backward 6
Calculated 3.14146709 280.954254 -21.1815929
value close to 0
Pade 4
Table 2. The first three derivative of the Second Function and Its exact values and calculated values

6
Numerical Practice No2 | Bahadır Türkyılmaz

Figure 6. The graph of Second Function (Red) and Its First Order Derivative (Orange)

Figure 7. The graph of First Order Derivative of Second Function (Red) and Its Second Order
Derivative (Orange)
7
Numerical Practice No2 | Bahadır Türkyılmaz

Figure 8. The graph of Second Order Derivative of Second Function (Red) and Its Third Order
Derivative (Orange)

Figure 9. Detailed Graph of Discontinuity of Third Order Derivative of Second Function (Orange)

8
Numerical Practice No2 | Bahadır Türkyılmaz

In Table 2, the derivative of the second function in various orders and exact and calculated values
of them at x=0 are shown. The values of the first derivative for different schemes have similar
magnitude because of continuity of function 2. The first derivative is not differentiable at x=0
because left and right side of it involves different trends even though both approaches to 0 at x=0.
That’s why forward and backward schemes have high amount of error for second derivative in
this point. However, the central finite difference schemes can compensate property of first
derivative by taking data both side of zero. For the third derivative, the error increases due to
discontinuity of derivative function as can be seen in Figure 9.

Method a b
Second O Forward -2.30603575574742 3.26782185970784
Second O Centered -2.30604255959462 3.26777447673901
Second O Backward -2.49448884864289 3.35337159026522
Forth O Forward -4.92414561269646 8.29582667481417
Forth O Centered -4.20750459295045 7.24985886517311
Forth O Backward -4.87949447457906 8.16526994614468
Sixth O Forward -6.14089930306327 7.75102497515126
Sixth O Centered -6.30204187006424 1.57580601047048
Sixth O Backward -6.14473727702804 7.77177004151712
Forth O Pade -4.64490508633646 7.98535351817607
Table 3 Coefficients of linear fit of error for function 2

The Table 3 represents coefficients of linear fit for log-log scale curve of Number of discretization
points and error. The slope of curve is linearly proportional with order of precision of the scheme
utilizing. The errors of slopes can be sourced by same reasons discussed for Function 1.
CONCLUSION
In the Tutorial 2, various Finite Difference Schemes are utilized two different functions. The
relation between Number of discretization points, error and precision of the scheme are observed.
Also, importance of continuity and differentiability of function for applying numerical
differentiation is recognized.

Das könnte Ihnen auch gefallen