Method of Runge-Kutta in Python -
i wrote code runge-kutta method in python, every time when program realizes calculus program require differential equation.
this code:
from math import * import numpy np #initial values n=input("enter number of equations n:") n=int(n) x=np.array([]) in range(0,n): x0=input("enter initial value of x{}:".format(i)) x=np.append(x,[x0]) t=input("enter initial value of t:") tf=input("enter final value of t:") h=input("enter time interval h:") m=int((tf-t)/float(h)) #definition of differential equations def f(t,x): f=np.array([]) in range(0,n): f0=input("enter equation f{}:".format(i)) f=np.append(f,[f0]) return f a=1.0/6 in range(0,m): k1=f(t,x) k2=f(t+0.5*h,x+0.5*h*k1) k3=f(t+0.5*h,x+0.5*h*k2) k4=f(t+h,x+h*k3) x=x+a*h*(k1+2*(k2+k3)+k4) t=t+h print t, x
example using equation dx/dt=x, x(0)=1, xf=1, h=0.1:
enter number of equations n:1 enter initial value of x0:1 enter initial value of t:0 enter final value of t:1 enter time interval h:0.1 enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] enter equation f0:x[0] 1.0 [ 2.71827974]
what should enter once differential equation program calculate all?
correct me if don't have understood. trying expression manually-typed user , treat function? found this topic explains how parse formula using sympy. can try modify program using sympy. in manner should able equation once all.
edit: if problem "how entire formula once input..." can try use raw_input() method. have here:
Comments
Post a Comment