Search code examples
pythonode

solve differential equation with iterated parameter


I'm lerning to solve Differential equation using (scipy.integrate.odeint) and (scipy.integrate.ode). I have a simple example:

dy/dt=f[i]*t

and f is parameter corresponding to t[i], same like the example in code ; i.e.

t[0]=0.0, f[0]=0.0

t[1]=0.1, f[1]=0.1

...

t[10]=1.0, f[1]=1.0

the manually result should be:

y=1/2*f[i]*t**2, because the initial value of y is zero

then, numerical result of y should be [0.0, 0.0005, 0.004, 0.0135, 0.032, 0.0625, 0.108, 0.1715, 0.256, 0.3645, 0.5]. But when i use scipy.integrate.ode, i have got a different result. My question is: 1. Have i used ode wrong? How can i reduce the errors? 2. Can i use odeint or other method to solve this problem?

The code is like:

import matplotlib.pyplot as pl
import numpy as np
import sympy as sp
from scipy.integrate import odeint
from scipy.integrate import ode
import numpy as np

def func(t, y, f):
    return f*t

t=np.linspace(0.0, 1.0, 11)
f=np.linspace(0.0, 1.0, 11)
dt = t[1]-t[0]

sol= np.empty_like(t)
r = ode(func).set_integrator("dopri5")
r.set_initial_value(0, 0).set_f_params(f[0])

# result of ode
for i in xrange(len(t)):
    r.set_f_params(f[i])
    r.integrate(r.t+dt)
    sol[i] = r.y

res=[]
# result of t**3/3
for a in np.linspace(0.0, 1, 11):
    f=(a**3)/3
    print f
    res.append(f)

# result3
res2=[]
for n in range(0, 11):
    dt=0.1
    y= t[n]**3/3 - dt*t[n]**2/4 - dt**2*t[n]/12
    res2.append(y)

pl.plot(sol)
pl.plot(res)  
pl.plot(res2)
pl.show()

I have extend this example to 2-dimensional-differential equations:

du/dt=-u(v-f[i])

dv/dt=v(f[i]-u)

with initial values: u(0)=v(0)=1. And below is the code:

import matplotlib.pyplot as pl
import numpy as np
import sympy as sp
from scipy.integrate import odeint
from scipy.integrate import ode
from numpy import array

def func(t, y, f):
    u,v=y
    dotu=-u*(v-f)
    dotv=v*(f-u)
    return array([dotu, dotv])

t=np.linspace(0.0, 10, 11)
f=np.linspace(0.0, 20, 11)

dt = t[1]-t[0]

# result correct
y0=array([1.0, 1.0])
sol= np.empty([11, 2])

sol[0] = array([1.0, 1.0])
r = ode(func).set_integrator("dopri5")
r.set_initial_value(t[0], sol[0]).set_f_params(f[0])

for i in range(len(t)-1):
    r.set_f_params(f[i])
    r.integrate(r.t+dt)
    sol[i+1] = r.y

pl.plot(sol[:,0])

But i get a error message:

Traceback (most recent call last): File "C:\Users\odeint test.py", line 26, in <module> sol[0] = array([1.0, 1.0]) ValueError: setting an array element with a sequence.


Solution

  • What you are doing is closer to integrating y'(t)=t^2, y(0)=0, resulting in y(t)=t^3/3. That you factor t^2 as f*t and hack f into a step function version of t only adds a small perturbation to that.


    The integral of t[i]*t over t[i]..t[i+1] is

    y[i+1]-y[i] = t[i]/2*(t[i+1]^2-t[i]^2) 
    = (t[i+1]^3-t[i]^3)/3 - (t[i+1]-t[i])^2*(t[i]+2t[i+1])/6
    = (t[i+1]^3-t[i]^3)/3 - dt*(t[i+1]^2-t[i]^2)/4 - dt^2*(t[i+1]-t[i])/12
    

    which sums up to about

    y[n] = t[n]^3/3 - dt*t[n]^2/4 - dt^2*t[n]/12
    

    How to get the correct solution

    sol= np.empty_like(t)
    

    set the initial value

    sol[0] = 0
    r = ode(func).set_integrator("dopri5")
    

    use the initial point as initial point, both to make explicit that the point at index 0 is fixed and "used up"

    r.set_initial_value(sol[0],t[0]).set_f_params(f[0])
    
    # result of ode
    

    go from point at t[i] to point at t[i+1]. Ends with i+1=len(t) or i=len(t)-1

    for i in xrange(len(t)-1):
        r.set_f_params(f[i])
        r.integrate(r.t+dt)
    

    value at t[i]+dt is value at t[i+1]

        sol[i+1] = r.y
    

    With these changes the numerical solution coincides with the manually computed solution.