python - RK4 giving wrong result -
i'm trying numerically solve simple second order de x'' = -x. used new variable x'=v, have 2 equations. while seems simple, somehow produces result that's far of correct result.
def f(x): return -x def rk4(f=f,h=2*pi/100,x0=1,v0=0,t0=0,n=100): '''rk4''' v=[v0] x=[x0] in range(n-1): v1= h*f(x[-1]) x1= h *(v[-1]) v2= h*f(x[-1]+1/2*x1) x2= h *(v[-1]+1/2*v1) v3= h*f(x[-1]+1/2*x2) x3= h *(v[-1]+1/2*v2) v4= h*f(x[-1]+x3) x4= h *f(v[-1]+v3) v.append(v[-1]+1/6 *(v1+2*v2+2*v3+v4)) x.append(x[-1]+1/6 *(x1+2*x2+2*x3+x4)) return x,v
the weird thing if use rk45 coefficients, works fine. idea on wrong?
def rk4(f=f,h=2*pi/100,x0=1,v0=0,t0=0,n=100): '''rk4''' v=[v0] x=[x0] in range(n-1): v1=h*f(x[-1]) x1=h *v[-1] v2=h*f(x[-1]+1/4*x1) x2=h *(v[-1]+1/4*v1) v3=h*f(x[-1]+9/32*x2+3/32*x1) x3=h *(v[-1]+9/32*v2+3/32*v1) v4=h*f(x[-1]+7296/2197*x3 - 7200/2197 * x2 + 1932 / 2197 * x1) x4=h *(v[-1]+7296/2197*v3 - 7200/2197 * v2 + 1932 / 2197 * v1) v5=h*f(x[-1]-845/4104 * x4 +3680/513*x3 - 8 * x2 + 439 / 216 * x1) x5=h *(v[-1]-845/4104 * v4 +3680/513*v3 - 8 * v2 + 439 / 216 * v1) v.append(v[-1]+25/216*v1+1408/2565*v3+2197/4104*v4-1/5*v5) x.append(x[-1]+25/216*x1+1408/2565*x3+2197/4104*x4-1/5*x5) return x,v
it simple copy-paste error. in line x4=...
there should no f
, in x1=...
, x2=...
, x3=...
.
then looks nice should.
this 1 reason stay close possible cook-book implementation, use vector-valued functions , vector arithmetic.
by way, if want last point have time 2*pi
, should iterate (in both methods) on range(n)
have n
integration steps , t=n*(2*pi/n)=2*pi
.
Comments
Post a Comment