Error Function Code
Contents |
function erf(x) The following code first appeared as Python code in my blog post Stand-alone error function erf. See that post for documentation. See also Relating inverse error function c++ erf and Φ.#include
Error Function Table
few input values double x[] = { -3, -1, 0.0, 0.5, 2.1 }; // Output computed by Mathematica // y = Erf[x] double y[] = { -0.999977909503, -0.842700792950, 0.0, 0.520499877813, 0.997020533344 }; int numTests = sizeof(x)/sizeof(double); double maxError = 0.0; for (int i = 0; i < numTests; ++i) { double error = fabs(y[i] - erf(x[i])); if (error > maxError) maxError = error; } std::cout << "Maximum error: " << maxError << "\n"; } A&S refers to Handbook of Mathematical Functions by Abramowitz and Stegun. See Stand-alone error function for details of the algorithm.This code is in the public domain. Do whatever you want with it, no strings attached.Other versions: C#, PythonMore stand-alone numerical code Search for: Subscribe to my newsletter John D. Cook© All rights reserved. Search for:
that occurs in probability, statistics, and partial differential equations describing diffusion. It is defined as:[1][2] erf ( x ) = 1 π ∫ − x x e − t 2 d t = 2 π ∫ 0 x e − t 2
Inverse Error Function Calculator
d t . {\displaystyle {\begin − 6\operatorname − 5 (x)&={\frac − 4{\sqrt {\pi }}}\int _{-x}^ error function matlab − 3e^{-t^ − 2}\,\mathrm − 1 t\\&={\frac − 0{\sqrt {\pi }}}\int _ 9^ 8e^{-t^ 7}\,\mathrm 6 t.\end 5}} complementary error function calculator The complementary error function, denoted erfc, is defined as erfc ( x ) = 1 − erf ( x ) = 2 π ∫ x ∞ e − t 2 d t = e − x http://www.johndcook.com/blog/cpp_erf/ 2 erfcx ( x ) , {\displaystyle {\begin 2\operatorname 1 (x)&=1-\operatorname 0 (x)\\&={\frac Φ 9{\sqrt {\pi }}}\int _ Φ 8^{\infty }e^{-t^ Φ 7}\,\mathrm Φ 6 t\\&=e^{-x^ Φ 5}\operatorname Φ 4 (x),\end Φ 3}} which also defines erfcx, the scaled complementary error function[3] (which can be used instead of erfc to avoid arithmetic underflow[3][4]). Another form of erfc ( x ) {\displaystyle \operatorname 2 (x)} for non-negative x {\displaystyle x} is known https://en.wikipedia.org/wiki/Error_function as Craig's formula:[5] erfc ( x | x ≥ 0 ) = 2 π ∫ 0 π / 2 exp ( − x 2 sin 2 θ ) d θ . {\displaystyle \operatorname 0 (x|x\geq 0)={\frac Φ 9{\pi }}\int _ Φ 8^{\pi /2}\exp \left(-{\frac Φ 7}{\sin ^ Φ 6\theta }}\right)d\theta \,.} The imaginary error function, denoted erfi, is defined as erfi ( x ) = − i erf ( i x ) = 2 π ∫ 0 x e t 2 d t = 2 π e x 2 D ( x ) , {\displaystyle {\begin Φ 0\operatorname − 9 (x)&=-i\operatorname − 8 (ix)\\&={\frac − 7{\sqrt {\pi }}}\int _ − 6^ − 5e^ − 4}\,\mathrm − 3 t\\&={\frac − 2{\sqrt {\pi }}}e^ − 1}D(x),\end − 0}} where D(x) is the Dawson function (which can be used instead of erfi to avoid arithmetic overflow[3]). Despite the name "imaginary error function", erfi ( x ) {\displaystyle \operatorname 8 (x)} is real when x is real. When the error function is evaluated for arbitrary complex arguments z, the resulting complex error function is usually discussed in scaled form as the Faddeeva function: w ( z ) = e − z 2 erfc ( − i z ) = erfcx ( − i z ) . {\displaystyle w(z)=e^{-z^ 6}\operatorname 5
Execution: java ErrorFunction z * * Implements the Gauss error function. * * erf(z) = 2 / sqrt(pi) * integral(exp(-t*t), t = 0..z) * * * % java ErrorFunction 1.0 * erf(1.0) = 0.8427007877600067 // actual = 0.84270079294971486934 * Phi(1.0) = 0.8413447386043253 // actual = http://introcs.cs.princeton.edu/21function/ErrorFunction.java.html 0.8413447460 * * * % java ErrorFunction -1.0 * erf(-1.0) = -0.8427007877600068 https://gist.github.com/5870580 * Phi(-1.0) = 0.15865526139567465 * * % java ErrorFunction 3.0 * erf(3.0) = 0.9999779095015785 // actual = 0.99997790950300141456 * Phi(3.0) = 0.9986501019267444 * * % java ErrorFunction 30 * erf(30.0) = 1.0 * Phi(30.0) = 1.0 * * % java ErrorFunction -30 * erf(-30.0) = -1.0 * Phi(-30.0) = 0.0 * error function * % java ErrorFunction 1E-20 * erf(1.0E-20) = -3.0000000483809686E-8 // true anser 1.13E-20 * Phi(1.0E-20) = 0.49999998499999976 * * ******************************************************************************/ public class ErrorFunction { // fractional error in math formula less than 1.2 * 10 ^ -7. // although subject to catastrophic cancellation when z in very close to 0 // from Chebyshev fitting formula for erf(z) from Numerical Recipes, 6.2 public static error function calculator double erf(double z) { double t = 1.0 / (1.0 + 0.5 * Math.abs(z)); // use Horner's method double ans = 1 - t * Math.exp( -z*z - 1.26551223 + t * ( 1.00002368 + t * ( 0.37409196 + t * ( 0.09678418 + t * (-0.18628806 + t * ( 0.27886807 + t * (-1.13520398 + t * ( 1.48851587 + t * (-0.82215223 + t * ( 0.17087277)))))))))); if (z >= 0) return ans; else return -ans; } // fractional error less than x.xx * 10 ^ -4. // Algorithm 26.2.17 in Abromowitz and Stegun, Handbook of Mathematical. public static double erf2(double z) { double t = 1.0 / (1.0 + 0.47047 * Math.abs(z)); double poly = t * (0.3480242 + t * (-0.0958798 + t * (0.7478556))); double ans = 1.0 - poly * Math.exp(-z*z); if (z >= 0) return ans; else return -ans; } // cumulative normal distribution // See Gaussia.java for a better way to compute Phi(z) public static double Phi(double z) { return 0.5 * (1.0 + erf(z / (Math.sqrt(2.0)))); } /*************************************************************************** * Test client ***************************************************************************/ public static void main(S
a GitHub account Sign in Create a gist now Instantly share code, notes, and snippets. Star 1 Fork 0 danielc192/InverseError.rb Last active Dec 19, 2015 Embed What would you like to do? Embed Embed this gist in your website. Embed Share Copy sharable URL for this gist. Share Clone via HTTPS Clone with Git or checkout with SVN using the repository's web address. HTTPS Learn more about clone URLs Download ZIP Code Revisions 2 Stars 1 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. Raw InverseError.rb 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 Contact GitHub API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Status Help You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.