-2

我想知道如何通过对从 890 到 910(含)的 x 值使用循环在 Python 中键入以下公式(请查看下面的图片):

公式

我的代码:

# PURPOSE : Evaluate cumulative probabilities for a normal distribution, sum an infinite series and convert the infinite series to integers


import math

m_ = 900         # identifier for normal distribution mean   [mm]
s_d = 1          # identifier for normal distribution standard deviation [mm]

print "DISTRIBUTION OF A PLATE WIDTH:" " MEAN", "=",m_,"," "STD DEV", "=", s_d
print ""
print "Using functions from the math library ..."


# The probability density function(pdf) for a normal distribution with mean m_ and standard deviation s_d
xx = 890
print "x" "       " " f(x)" "        " " F(x)"
while xx < 911:
    print xx
    xx = xx + 1

print (1 / ((s_d * (math.sqrt(2 * math.pi) * math.exp((- (x - m_) ** 2) / (2 * (s_d) ** 2)  
4

1 回答 1

0

最好为公式定义一个函数......

工作代码

# PURPOSE : Evaluate cumulative probabilities for a normal distribution, sum an infinite series and convert the infinite series to integers

import math

def foo(x, mean, deviation):
    return (
                (
                    1 /
                    (
                        deviation
                        *
                        math.sqrt(2 * math.pi)
                    )
                )
            *
                math.exp
                (
                    -
                    (
                        (
                            (x - mean) ** 2
                        )
                        /
                        (
                            2 * (deviation ** 2)
                        )
                    )
                )
            )

m_ = 900         # identifier for normal distribution mean   [mm]
s_d = 1          # identifier for normal distribution standard deviation [mm]

print "DISTRIBUTION OF A PLATE WIDTH:" " MEAN", "=",m_,"," "STD DEV", "=", s_d
print ""
print "Using functions from the math library ..."

# The probability density function(pdf) for a normal distribution with mean m_ and standard deviation s_d

print "x" "       " " f(x)" "        " " F(x)"

for xx in range(890, 911):
    print xx
    xx = xx + 1

    print foo(xx, m_, s_d)

输出

>>> 
DISTRIBUTION OF A PLATE WIDTH: MEAN = 900 ,STD DEV = 1

Using functions from the math library ...
x        f(x)         F(x)
890
1.69484813456e-18
891
5.05227108354e-15
892
1.50606078392e-11
893
6.07588284982e-09
894
2.45118608751e-06
895
0.000133830225765
896
0.00730688274528
897
0.0539909665132
898
0.398942280401
899
0.398942280401
900
0.398942280401
901
0.0539909665132
902
0.00730688274528
903
0.000133830225765
904
2.45118608751e-06
905
6.07588284982e-09
906
1.50606078392e-11
907
5.05227108354e-15
908
1.69484813456e-18
909
7.69459862671e-23
910
3.49334237203e-27
>>>
于 2013-02-01T20:02:42.783 回答