#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep 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. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480

    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]==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 and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo