Numpy数组引用在for循环中时表现异常

时间:2019-03-23 08:14:09

标签: python numpy

我正在使用Paul数学笔记here中的示例编写Euler方法的Python实现。

我正在使用n x 3 numpy数组存储结果。目标是在第一列中包含t值,在第二列中包含y,并使用第三列中的当前行来计算y'的值。

当我仅用十次迭代就完成了页面上列出的第一个问题时,所有内容的行为都与预期的完全一样。步长为0.1,因此随着for循环的每次迭代,第一列中的值增加0.1。

但是,既然我已经复制了代码并尝试将其应用于问题3,则第一列的行为非常奇怪。我输入的步长为0.01,但在前十次迭代中,其增量为0.1,然后在第十次迭代后,它似乎重置为零, then 使用了预期的0.01,但随后又重置了以类似的方式。

这是我的代码:

import numpy as np

def ex3(t,y):
    return y + (-0.5 * np.exp(t/2) * np.sin(5*t)) + (5 * np.exp(t/2) * np.cos(5*t))

ex3out = np.empty((0,3), float)
# Input the initial conditions and first y' computation
ex3out = np.append(ex1out, np.array([[0,0,ex3(0,0)]]), axis=0)
h = 0.01
n = 500

for i in range(1,n+1):
    # Compute the new t and y values and put in 0 as a dummy y' for now  
    new = np.array([[ex3out[i - 1,0] + h, ex3out[i - 1,1] + h * ex3out[i - 1,2],0]])
    # Append the new row
    ex3out = np.append(ex3out,new,axis=0)
    # Replace the dummy 0 with y' based on the new values
    ex3out[i,2] = ex3(ex3out[i,0],ex3out[i,1])

这是运行上述代码后ex3out的前几行:

array([[ 0.        ,  1.        , -1.        ],
       [ 0.1       ,  0.9       ,  5.2608828 ],
       [ 0.2       ,  0.852968  ,  3.37361534],
       [ 0.3       ,  0.8374415 ,  0.6689041 ],
       [ 0.4       ,  0.83983378, -2.25688988],
       [ 0.5       ,  0.85167737, -4.67599317],
       [ 0.6       ,  0.86780837, -5.90918813],
       [ 0.7       ,  0.8851749 , -5.51040903],
       [ 0.8       ,  0.90205891, -3.40904125],
       [ 0.9       ,  0.91757091,  0.031139  ],
       [ 1.        ,  0.93132436,  4.06022317],
       [ 0.        ,  0.        ,  5.        ],
       [ 0.01      ,  0.99      ,  5.98366774],
       [ 0.02      ,  0.95260883,  5.92721107],
       [ 0.03      ,  0.88670415,  5.82942804],
       [ 0.04      ,  0.84413054,  5.74211536],
       [ 0.05      ,  0.81726488,  5.65763415],
       [ 0.06      ,  0.80491744,  5.57481145],
       [ 0.07      ,  0.80871649,  5.4953251 ],
       [ 0.08      ,  0.83007081,  5.42066644],
       [ 0.09      ,  0.8679685 ,  5.34993924],
       [ 0.1       ,  0.9178823 ,  5.2787651 ],
       [ 0.11      ,  0.97192659,  5.19944036],
       [ 0.12      ,  0.05      ,  4.13207859],
       [ 0.13      ,  1.04983668,  4.97466166],
       [ 0.14      ,  1.01188094,  4.76791408],
       [ 0.15      ,  0.94499843,  4.5210138 ],
       [ 0.16      ,  0.90155169,  4.28666725],
       [ 0.17      ,  0.87384122,  4.0575499 ],
       [ 0.18      ,  0.86066555,  3.83286568],
       [ 0.19      ,  0.86366974,  3.61469476],
       [ 0.2       ,  0.88427747,  3.40492482],
       [ 0.21      ,  0.92146789,  3.20302701],

我想知道这是否可能是浮点问题,所以我尝试将for循环的各个部分包含在float()中,并得到相同的结果。

我一定在某处打错了吧?

1 个答案:

答案 0 :(得分:0)

简单的循环:

ex3out = [[0, 0, ex3(0,0)]]
h = 0.01
n = 50

for i in range(1,n+1):
    # Compute the new t and y values and put in 0 as a dummy y' for now 
    last = ex3out[-1] 
    new = [last[0] + h, last[1] + h * last[2], 0]
    new[2] = ex3(new[0], new[1])
    # Append the new row
    ex3out.append(new)

print(np.array(ex3out))   # for pretty numpy display