Created
December 11, 2017 12:29
-
-
Save gmaze/a1009b0cb314885d8e148d7df426de6e to your computer and use it in GitHub Desktop.
Demonstrate the different behaviours of numpy arange and linspace with regard to truncation error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
eps = np.finfo(np.float64).eps | |
print "This is epsilon:", eps | |
print "\nnumpy.arange" | |
dX = 0.1 | |
X = np.arange(34.,38.,dX) | |
udX = np.unique(np.diff(X)) | |
print "Unique dX:", udX | |
trunc_err = (np.diff(X)/dX-1)/eps | |
print "Float truncation error (as N x eps) with arange:", trunc_err | |
print "Mean float truncation error (as N x eps) with arange:", np.mean(trunc_err) | |
print "\nnumpy.linspace" | |
X = np.linspace(34.,38.,(38-34)/dX+1) | |
udX = np.unique(np.diff(X)) | |
print "Unique dX:", udX | |
trunc_err = (np.diff(X)/dX-1)/eps | |
print "Float truncation error (as N x eps) with linspace:", trunc_err | |
print "Mean float truncation error (as N x eps) with linspace:", np.mean(trunc_err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code will print the following: