Last active
December 19, 2015 00:39
-
-
Save danielc192/5870580 to your computer and use it in GitHub Desktop.
Code to calculate the Inverse Error function using a power series approximation. Based on information at http://en.wikipedia.org/wiki/Error_function#Inverse_functions. Implemented as a method in the Math module, but it could easily be run independently.
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
module Math | |
def self.invErf(x) | |
c = [] | |
c[0] = 1.0 | |
c[1] = 1.0 | |
result = 0.0 | |
(0..100).each do |k| | |
# Calculate C sub k | |
if k > 1 then | |
c[k] = 0.0 | |
(0..(k-1)).each do |m| | |
term = (c[m]*c[k-1.0-m])/((m+1.0)*(2.0*m+1.0)) | |
c[k] += term | |
end | |
end | |
result += (c[k]/(2.0*k+1))*((Math.sqrt(Math::PI)/2)*x)**(2.0*k+1) | |
end | |
return result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment