Astrofísica Computacional
Astrofísica Computacional
E. Larrañaga
1 Sources of Error
Round-off Error
Truncation Error
2 Finite Differences
Differentiation of a Discrete Function
Differentiation of an Analytic Function
Round-off Error
"""
Round-off Error
We find the value of epsilon for which 1. + epsilon = 1.
This gives the machine epsilon value, representing the error inherent to
representing a floating point number
"""
print (epsilon)
Example:
Function:
f (x) = sin(x) (1)
Taylor series representation:
∞
X x 2n−1
f (x) = (−1)n−1 (2)
(2n − 1)!
n=1
x3
f (x) = x − + O(x 5 ) (3)
6
Convergence Test
"""
Truncation Error - Convergence Test
We implement a 5th order accurate approximation of the function Sin(x)
Calculating the value of this epsilon for x<1 and then taking half of
this value of x we show that the epsilon reduces by 2**5 = 32,
demonstrating 5th-order accuracy
"""
import math as m
# Results. We use the formating in the print function to show the results
print("\nFor x = %f the value of the truncation error is epsilon = %e" %( x, epsilon(x)))
print("For x = %f the value of the truncation error is epsilon = %e" %( x/2, epsilon(x/2)))
print("")
print("The ratio of these values is %f " %(epsilon(x)/epsilon(x/2)))
Differentiation of a Discrete Function
Taylor expansion
∞
X f (n) (x0 )
f (x0 + δx) = δx n (8)
n!
n=0
= f (x0 ) + f 0 (x0 )δx + O(δx 2 ) (9)
Differentiation
df f (x0 + δx) − f (x0 )
= + O(δx) (10)
dx x0 δx
Differentiation
df f (x0 + δx) − f (x0 )
= + O(δx) (10)
dx x0 δx
exact
1.0 left-sided
right-sided
0.8 centered
0.6
0.4
0.2
∆x
0.0
0.0 0.5 1.0 1.5 2.0 2.5 3.0
Differentiation of an Analytic Function
They are, however, in many cases by far not the most efficient way,
since many problems need more resolution in some parts of their
domain than in others.
Differentiation of an Analytic Function in an Unevenly
Spaced Grid
They are, however, in many cases by far not the most efficient way,
since many problems need more resolution in some parts of their
domain than in others.
δx22 00
f (xi + δx2 ) = f (xi ) + δx2 f 0 (xi ) + f (xi ) + O(δx23 )
2 (14)
δx 2
f (xi − δx1 ) = f (xi ) − δx1 f 0 (xi ) + 1 f 00 (xi ) + O(δx13 )
2
Differentiation of an Analytic Function in an Unevenly
Spaced Grid
δx22 00
f (xi + δx2 ) = f (xi ) + δx2 f 0 (xi ) + f (xi ) + O(δx23 )
2 (14)
δx 2
f (xi − δx1 ) = f (xi ) − δx1 f 0 (xi ) + 1 f 00 (xi ) + O(δx13 )
2
Eliminating f 00 (xi ) and solving for f 0 (xi ),
δx22 00
f (xi + δx2 ) = f (xi ) + δx2 f 0 (xi ) + f (xi ) + O(δx23 )
2 (14)
δx 2
f (xi − δx1 ) = f (xi ) − δx1 f 0 (xi ) + 1 f 00 (xi ) + O(δx13 )
2
Eliminating f 00 (xi ) and solving for f 0 (xi ),