Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 4th, 2012, 04:18 AM
 cheames
Registered User

Join Date: Dec 2012
Posts: 1
Time spent in forums: 1 h 2 m 5 sec
Reputation Power: 0
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.

#2
December 4th, 2012, 02:32 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,217
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 35 m 4 sec
Reputation Power: 455

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]==1-y[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!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Improving ODE algorithm