该问题要求我们使用Rk4方法∫sinh(𝑥)(𝑥−5)𝑑𝑥
从0
到5
在python中解决此积分问题。最好的方法是什么,如何将其更改为ODE?
def RK4(f, dx, x0, y0, imax):
output = np.empty((imax, 3))
i = 0
xi = x0
yi = y0
while(i < imax):
k1 = dx*f(xi,yi)
k2 = dx*f(xi + 0.5*dx, yi + 0.5*k1)
k3 = dx*f(xi + 0.5*dx, yi + 0.5*k2)
k4 = dx*f(xi + dx, yi + k3)
yi = yi + 1/6*(k1 + 2*k2 + 2*k3 + k4)
xi += dx
output[i, 0] = xi
output[i, 1] = yi[0]
output[i, 2] = yi[1]
i += 1
return output
RK4(0,0,5,100,10)
我收到一个错误,它对于k1(“ int object is not callable”)?我该如何解决这个问题,或者问题可能在我的代码中。谢谢你的帮助。这个问题也明确要求我们同时使用Simpsons规则和RK4方法求解积分]
您正在传递整数0
代替函数。然后,在k1
行中,您尝试使用该整数作为函数,从而给出报告的错误,因为对0(0,5)
没有明智的解释。您可以使用
RK4(lambda x,y:0,0,5,100,10)
但是,步长为零将不会发生积分。