如何变量方程python中的参数

时间:2014-02-06 16:38:29

标签: python

我正在解决以下等式:

wf=1110
wt=647
wp=119000
c=300000000
e0=10983849
e1 =e0-(wp**2/(w*2+wt))

我希望“w”作为一个变量,比如1到1000或者我想要的任何升级范围。

我想要一个e1 vs w

的情节

有谁知道怎么做?

1 个答案:

答案 0 :(得分:2)

一种方法:

def solve_equation(start, end, equation):
    for x in xrange(start, end):
        yield equation(x)

def my_equation(x):
    wt=647
    wp=119000
    e0=10983849
    e1 =e0-(wp**2/(w*2+wt))
    return e1

print solve_equation(0, 100, my_equation)
print solve_equation(500, 1000, my_equation)

这将方程式本身的范围的方程式求解。

如果你要大规模地这样做,你也可以调查numpy。然后,您将为w创建一个numpy值的数组,并将它们计算为向量。这会将你的代码简化为类似的东西:

from numpy import arange
wt=647
wp=119000
e0=10983849
w = arange(0, 100)
e = e0-(wp**2/(w*2+wt))

这会将w设置为数组([0,1,2,...,98,99])并将e设置为:

array([-10903322, -10835873, -10768839, -10702215, -10635998, -10570184,
   -10504770, -10439751, -10375125, -10310887, -10247035, -10183565,
   -10120472, -10057755,  -9995410,  -9933433,  -9871821,  -9810570,
    -9749679,  -9689143,  -9628960,  -9569126,  -9509638,  -9450494,
    -9391690,  -9333224,  -9275092,  -9217292,  -9159820,  -9102675,
    -9045853,  -8989352,  -8933169,  -8877301,  -8821745,  -8766499,
    -8711561,  -8656927,  -8602596,  -8548564,  -8494830,  -8441391,
    -8388244,  -8335387,  -8282817,  -8230533,  -8178532,  -8126812,
    -8075370,  -8024204,  -7973312,  -7922693,  -7872342,  -7822259,
    -7772442,  -7722888,  -7673595,  -7624560,  -7575784,  -7527262,
    -7478993,  -7430975,  -7383206,  -7335685,  -7288409,  -7241376,
    -7194584,  -7148033,  -7101719,  -7055641,  -7009797,  -6964186,
    -6918805,  -6873654,  -6828729,  -6784030,  -6739555,  -6695302,
    -6651269,  -6607455,  -6563858,  -6520477,  -6477310,  -6434355,
    -6391611,  -6349076,  -6306749,  -6264628,  -6222712,  -6180999,
    -6139488,  -6098177,  -6057065,  -6016151,  -5975432,  -5934908,
    -5894577,  -5854438,  -5814490,  -5774730])

使用优秀的IPython Notebookpylab,您可以这样做:

plot(w, e)

并有一个很好的图表显示结果:

plot(w, e)

最多绘制10000将产生如下图形:

plot(w, e) for w in range(1, 10000)