Python math.erfc() 方法



Python 的 math.erfc() 方法用於計算互補誤差函式,其定義為 1 − erf(x),其中 erf(x) 是誤差函式。

此方法表示正態分佈隨機變數在某個範圍之外的事件的機率。在數學上,互補誤差函式定義為:

$$\mathrm{erfc(x)\:=\:1\:-\:erf(x)\:=\frac{2}{\sqrt{\prod}}\int_{x}^{∞}\:e^{-t^{2}}dt}$$

其中,e 是自然對數的底數,π 是數學常數 pi。互補誤差函式是一個偶函式,這意味著 erfc(-x) = erfc(x),並且其值介於 0 和 2 之間。

語法

以下是 Python math.erfc() 方法的基本語法:

math.erfc(x)

引數

此方法接受一個實數或數值表示式作為引數,用於計算其互補誤差函式。

返回值

該方法返回在 x 處計算的互補誤差函式的值。

示例 1

在以下示例中,我們使用 math.erfc() 方法計算正實數的互補誤差函式:

import math
x = 1.5
result = math.erfc(x)
print("Complementary Error method for x =", x, ":", result)

輸出

獲得的輸出如下:

Complementary Error method for x = 1.5 : 0.033894853524689274

示例 2

在這裡,我們使用 math.erfc() 方法計算負實數的互補誤差函式:

import math
x = -0.75
result = math.erfc(x)
print("Complementary Error method for x =", x, ":", result)

輸出

以下是上述程式碼的輸出:

Complementary Error method for x = -0.75 : 1.7111556336535152

示例 3

在此示例中,我們使用 math.erfc() 方法計算 x=2 和 x/2 的互補誤差函式之和:

import math
x = 2
result = math.erfc(x) + math.erfc(x/2)
print("Complementary Error method expression result for x =", x, ":", result) 

輸出

我們得到如下所示的輸出:

Complementary Error method expression result for x = 2 : 0.1619769420313324

示例 4

現在,我們使用 math.erfc() 方法直接計算 x=0 的互補誤差函式:

import math
x = 0
result = math.erfc(x)
print("Complementary Error method for x =", x, ":", result)

輸出

產生的結果如下所示:

Complementary Error method for x = 0 : 1.0
python_maths.htm
廣告