December 4th, 2012, 04:18 AM

Improving ODE algorithm
Hi, I'm doing python in class and have got completely stuck.
I have this code to solve an ODE, but now I need to improve it so that after finding the slope at a point A, it uses it to find some point B, it's slope, then averages them out to get a better value. Here's the code:
Code:
def f(y,t):
return y+1.0
def odestep(f,y,t,dt):
return y+dt*f(y,t)
t=0; y=0; dt=0.2
tf=2.0; nsteps=int(tf/dt)
print t, y
for i in range(nsteps):
y=odestep(f,y,t,dt)
t=(i+1)*dt
print t, y
Thanks.
EDIT: solved, nevermind.
December 4th, 2012, 02:32 PM

Glad you solved this.
http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
I'd use a canned routine. See zip file at post 4.
The problem and solution:
# mathematica:
# DSolve[y'[t]==1y[t],y[t]]
# giving c1 Exp[t]+1 == y[t]
# c1 is 1 so that y[0] == 0
[code]
Code tags[/code] are essential for python code and Makefiles!