Reputation: 13
The question asks us to solve this integral in python using an Rk4 method ∫sinh(𝑥)(𝑥−5)𝑑𝑥
from 0
to 5
. Whats the best way to do this, how would you change this to an 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)
I get a error that for k1 ("int object is not callable")?? how can I fix this or maybe the issue is somewhere else in my code. Thanks for any help. Also the question explicitly asks us to solve the integral using both Simpsons rule and RK4 methods
Upvotes: 0
Views: 336
Reputation: 26040
You are passing an integer 0
in place of the function. Then in the k1
line you try to use that integer as function, which gives the reported error, as there is no sensible interpretation of 0(0,5)
. You could use
RK4(lambda x,y:0,0,5,100,10)
However, with a step size of zero no integration will occur.
Upvotes: 1