Solving ODE with python - python

I've tried to solve a 1-st order ode equation using scipy.integrate.ode.
dh/dx = - s0 * (1 - (hn/h)^3)/(1 - (hc/h)^3)
The initial condition is x = 0 and h = 10.
s0 = 0.001 when x < 15000,
s0 = 0.0005 when x >= 15000.
hn = (f*q^2/8*g*s0)^(1/3)
hc = (q^2/g)^(1/3)
f, q, g are constant.
The method I used is node bdf, but the result I got is different than the answer solved by matlab.
The answer should be like this:
https://dl.dropboxusercontent.com/u/18438495/result.png
Can anyone see the problem?
import numpy as np
from scipy.integrate import ode
import matplotlib.pyplot as plt
def waterdepth(t, y):
if t < 15000:
s0 = 0.001
elif t >= 15000:
s0 = 0.0005
q = 3.72
f = 0.03
g = 9.81
hn = (f*q*q/8*g*s0)**(1.0/3.0)
hc = (q*q/g)**(1.0/3.0)
return -s0 * (1.0 - (hn/y)**3)/(1.0 - (hc/y)**3)
y0 = 10.0
t0 = 0.0
solver = ode(waterdepth).set_integrator('node', method = 'bdf')
solver.set_initial_value(y0, t0)
dt = 100.0
t1 = 25000
x = []
h = []
while solver.successful() and solver.t < t1:
x.append(solver.t)
solver.integrate(solver.t + dt)
h.append(solver.y)
plt.plot (x, h)

return -s0 * (1.0 - (hn/y)**3)/(1.0 - (hc/y)**3)
is different from the equation on top.
dh/dx = - s0 * (1 - (hn-h)^3)/(1 - (hc-h)^3)

Related

Solving system of coupled differential equations using Runge-Kutta in python

This python code can solve one non- coupled differential equation:
import numpy as np
import matplotlib.pyplot as plt
import numba
import time
start_time = time.clock()
#numba.jit()
# A sample differential equation "dy / dx = (x - y**2)/2"
def dydx(x, y):
return ((x - y**2)/2)
# Finds value of y for a given x using step size h
# and initial value y0 at x0.
def rungeKutta(x0, y0, x, h):
# Count number of iterations using step size or
# step height h
n = (int)((x - x0)/h)
# Iterate for number of iterations
y = y0
for i in range(1, n + 1):
"Apply Runge Kutta Formulas to find next value of y"
k1 = h * dydx(x0, y)
k2 = h * dydx(x0 + 0.5 * h, y + 0.5 * k1)
k3 = h * dydx(x0 + 0.5 * h, y + 0.5 * k2)
k4 = h * dydx(x0 + h, y + k3)
# Update next value of y
y = y + (1.0 / 6.0)*(k1 + 2 * k2 + 2 * k3 + k4)
# Update next value of x
x0 = x0 + h
return y
def dplot(start,end,steps):
Y=list()
for x in np.linspace(start,end,steps):
Y.append(rungeKutta(x0, y, x , h))
plt.plot(np.linspace(start,end,steps),Y)
print("Execution time:",time.clock() - start_time, "seconds")
plt.show()
start,end = 0, 10
steps = end* 100
x0 = 0
y = 1
h = 0.002
dplot(start,end,steps)
This code can solve this differential equation:
dydx= (x - y**2)/2
Now I have a system of coupled differential equations:
dydt= (x - y**2)/2
dxdt= x*3 + 3y
How can I implement these two as a system of coupled differential equations in the above code?
Is there any more generalized way for system of n-number of coupled differential equations?
With the help of others, I got to this:
import numpy as np
from math import sqrt
import matplotlib.pyplot as plt
import numba
import time
start_time = time.clock()
a=1
b=1
c=1
d=1
# Equations:
#numba.jit()
#du/dt=V(u,t)
def V(u,t):
x, y, vx, vy = u
return np.array([vy,vx,a*x+b*y,c*x+d*y])
def rk4(f, u0, t0, tf , n):
t = np.linspace(t0, tf, n+1)
u = np.array((n+1)*[u0])
h = t[1]-t[0]
for i in range(n):
k1 = h * f(u[i], t[i])
k2 = h * f(u[i] + 0.5 * k1, t[i] + 0.5*h)
k3 = h * f(u[i] + 0.5 * k2, t[i] + 0.5*h)
k4 = h * f(u[i] + k3, t[i] + h)
u[i+1] = u[i] + (k1 + 2*(k2 + k3 ) + k4) / 6
return u, t
u, t = rk4(V, np.array([1., 0., 1. , 0.]) , 0. , 10. , 100000)
x,y, vx,vy = u.T
# plt.plot(t, x, t,y)
plt.semilogy(t, x, t,y)
plt.grid('on')
print("Execution time:",time.clock() - start_time, "seconds")
plt.show()

Ode integrator Python TypeError 'float' object is not subscriptable

I am desperately trying to use the scipy ODE integrator, but I keep getting the following error :
Y[0] = (1/I3) * T_z(INP[0], INP[1], INP[2], INP[3], INP[4])
TypeError: 'float' object is not subscriptable
My code is the following :
import scipy.integrate as spi
import numpy as np
import pylab as pl
from time import time
#Constants
I3 = 0.00396
lamb = 1
L = 5*10**-1
mu = 1
m = 0.1
Cz = 0.5
rho = 1.2
S = 0.03*0.4
K_z = 1/2*rho*S*Cz
g = 9.81
#Initial conditions
omega0 = 10*2*np.pi
V0 = 25
theta0 =np.pi/2
phi0 = 0
psi0 = -np.pi/9
X0 = 0
Y0 = 0
Z0 = 1.8
#for integration
t_start = 0.0
t_end = 5
t_step = 0.1
t_range = np.arange(t_start, t_end+t_step, t_step)
INPUT = omega0, V0, theta0, phi0, psi0, X0, Y0, Z0 #initial conditions
def diff_eqs(INP,t):
def M(v_G, w_z):
return L*K_z*(v_G**2 + v_G*L*w_z*np.sin(w_z*t_step)+(L*w_z)**2)
def F_x(w_z, v_G, theta, phi, psi):
return K_z*(v_G**2+(L*w_z)**2)*np.sin(theta)*np.sin(phi) + lamb*v_G*(np.cos(psi)*np.cos(phi) - np.cos(theta)*np.sin(phi)*np.sin(psi))
def F_y(w_z, v_G, theta, phi, psi):
return -K_z*(v_G**2+(L*w_z)**2)*np.sin(theta)*np.cos(phi) + lamb*v_G*(np.cos(psi)*np.sin(phi) + np.cos(theta)*np.cos(phi)*np.sin(psi))
def F_z(w_z, v_G, theta, phi, psi):
return -K_z*(v_G**2+(L*w_z)**2)*np.cos(theta) + lamb*v_G*np.sin(theta)*np.sin(psi) - m*g
def T_x(w_z, v_G, theta, phi, psi):
return M(v_G, w_z)*(-np.sin(w_z*t_step)*(np.cos(psi)*np.cos(phi) - np.cos(theta)*np.sin(phi)*np.sin(psi)) \
+ np.cos(w_z*t_step)*(-np.sin(psi)*np.cos(phi) - np.cos(theta)*np.sin(phi)*np.cos(psi))) \
- mu * w_z * (np.sin(theta)*np.sin(phi))
def T_y(w_z, v_G, theta, phi, psi):
return M(v_G, w_z)*(-np.sin(w_z*t_step)*(np.cos(psi)*np.sin(phi) + np.cos(theta)*np.cos(phi)*np.sin(psi)) \
+ np.cos(w_z*t_step)*(-np.sin(psi)*np.sin(phi) - np.cos(theta)*np.cos(phi)*np.cos(psi)))
- mu * w_z * (np.sin(theta)*np.cos(phi))
def T_z(w_z, v_G, theta, phi, psi):
return M(v_G, w_z)*(-np.sin(w_z*t_step)*np.sin(theta)*np.sin(psi) + np.cos(w_z*t_step)*np.sin(theta)*np.cos(psi)) \
- mu * w_z * np.cos(theta)
Y = np.zeros(8)
Y[0] = (1/I3) * T_z(INP[0], INP[1], INP[2], INP[3], INP[4])
Y[1] = -(lamb/m)*F_x(INP[0], INP[1], INP[2], INP[3], INP[4])
Y[2] = (1/(I3*INP[0]))*(-T_y(INP[0], INP[1], INP[2], INP[3], INP[4])*np.cos(INP[4]) - T_x(INP[0], INP[1], INP[2], INP[3], INP[4])*np.sin(INP[4]))
Y[3] = (1/(I3*INP[0]*np.cos(INP[3]))) * (-T_y(INP[0], INP[1], INP[2], INP[3], INP[4])*np.sin(INP[4]) + T_x(INP[0], INP[1], INP[2], INP[3], INP[4])*np.cos(INP[4]))
Y[4] = -(1/(m*INP[1]))*F_y(INP[0], INP[1], INP[2], INP[3], INP[4])
Y[5] = INP[1]*(-np.cos(INP[4])*np.cos(INP[3]) + np.sin(INP[4])*np.sin(INP[3])*np.cos(INP[2]))
Y[6] = INP[1]*(-np.cos(INP[4])*np.sin(INP[3]) - np.sin(INP[4])*np.cos(INP[3])*np.cos(INP[2]))
Y[7] = INP[1]*(-np.sin(INP[4])*np.sin(INP[2]))
return Y
ode = spi.ode(diff_eqs)
# BDF method suited to stiff systems of ODEs
ode.set_integrator('vode',nsteps=500,method='bdf')
ode.set_initial_value(INPUT,t_start)
ts = []
ys = []
while ode.successful() and ode.t < t_end:
ode.integrate(ode.t + t_step)
ts.append(ode.t)
ys.append(ode.y)
t = np.vstack(ts)
I have a set of 8 differentials equations I want to numerically solve. Therefore I have 8 initial values stored in "INPUT". But when I use this variable in ode.set_initial_value(INPUT,t_start), it keeps repeating that the variable is a float ! It has been bugging me for hours and the answer is maybe obvious but I can't see where I made a mistake. And I don't think the equations themselves, even though they are pretty messy, are involved here.
Thanks in advance for your help.
Your argument order is the one required in the ODE function for odeint. For ode you need the order (t, INP).
Try to use the more recent solve_ivp interface, it has about the same functionality of the ode class and about the same compact call structure as odeint.

Using the timing of the event in the ODE

(This is a follow up question related to Scipy ODE time steps going backward)
I have a system of equations that I am trying to solve with scipy's solve_ivp. Here's a minimal working code:
import numpy as np
from scipy.integrate import solve_ivp
def synapse(t, t0):
tau_1 = 5.3
tau_2 = 0.05
tau_rise = (tau_1 * tau_2) / (tau_1 - tau_2)
B = ((tau_2 / tau_1)**(tau_rise / tau_1) - (tau_2 / tau_1)**(tau_rise / tau_2)) ** -1
return B*(np.exp(-(t - t0) / tau_1) - np.exp(-(t - t0) / tau_2))
def alpha_m(v, vt):
return -0.32*(v - vt -13)/(np.exp(-1*(v-vt-13)/4)-1)
def beta_m(v, vt):
return 0.28 * (v - vt - 40) / (np.exp((v- vt - 40) / 5) - 1)
def alpha_h(v, vt):
return 0.128 * np.exp(-1 * (v - vt - 17) / 18)
def beta_h(v, vt):
return 4 / (np.exp(-1 * (v - vt - 40) / 5) + 1)
def alpha_n(v, vt):
return -0.032*(v - vt - 15)/(np.exp(-1*(v-vt-15)/5) - 1)
def beta_n(v, vt):
return 0.5* np.exp(-1*(v-vt-10)/40)
def event(t,X):
return X[0] + 20
event.terminal = False
event.direction = +1
def f(t, X):
V = X[0]
m = X[1]
h = X[2]
n = X[3]
last_inputspike = inputspike[inputspike.searchsorted(t, side='right') - 1 ]
last_t_event = -100 #Not sure what to put here
g_syn_in = synapse(t, last_inputspike)
g_syn_spike = synapse(t, last_t_event)
syn = 0.5 * g_syn_in * (V - 0) + 0.2 * g_syn_spike * (V + 70)
dVdt = - 50*m**3*h*(V-60) - 10*n**4*(V+100) - syn - 0.1*(V + 70)
dmdt = alpha_m(V, -45)*(1-m) - beta_m(V, -45)*m
dhdt = alpha_h(V, -45)*(1-h) - beta_h(V, -45)*h
dndt = alpha_n(V, -45)*(1-n) - beta_n(V, -45)*n
return [dVdt, dmdt, dhdt, dndt]
# Define the spike events:
nbr_spike = 20
beta = 100
first_spike_date = 500
np.random.seed(0)
inputspike = np.cumsum( np.random.exponential(beta, size=nbr_spike) ) + first_spike_date
inputspike = np.insert(inputspike, 0, -1e4) # set a very old spike at t=-1e4
# it is a hack in order to set a t0 for t<first_spike_date (model settle time)
# so that `synapse(t, t0)` can be called regardless of t
# synapse(t, -1e4) = 0 for t>0
# Solve:
t_start = 0.0
t_end = 2000
X_start = [-70, 0, 1,0]
sol = solve_ivp(f, [t_start, t_end], X_start, method='BDF', max_step=1, vectorized=True, events=event)
print(sol.message)
I want to detect when there is a spike (defined as V > 20), and have the timing of the spike affect the syn in the ODE via the changing g_syn_spike, in a similar way that the random input affects it.
In essence, I was wondering if it is possible and how I could go about accessing the last value of the sol.t_events at the given iteration of the solver?
I have been looking for a way to simulate discrete events in continuous systems of differential equations as well. Modeling such discontinuities is not trivial, and two (recent) packages out there that can help you coop with this are:
assimulo - https://pypi.org/project/Assimulo/
simupy - https://pypi.org/project/simpy/
(and not simpy, this is only for discrete systems)
I hope this helps, in case you found a different solution already I'd like to hear what that is as well

How to use if statement in a differential equation (SciPy)?

I am trying to solve a differential equation with Python.
In this two system differential equation if the value of first variable (v) is more than a threshold (30) it should be reset to another value (-65). Below I put my code. The problem is that the value of first variable after reaching 30 remains constant and won't reset to -65. These equations describe the dynamics of a single neuron. The equations are taken from this website and this PDF file.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
from scipy.integrate import odeint
plt.close('all')
a = 0.02
b = 0.2
c = -65
d = 8
i = 0
p = [a,b,c,d,i]
def fun(u,tspan,*p):
du = [0,0]
if u[0] < 30: #Checking if the threshold has been reached
du[0] = (0.04*u[0] + 5)*u[0] + 150 - u[1] - p[4]
du[1] = p[0]*(p[1]*u[0]-u[1])
else:
u[0] = p[2] #reset to -65
u[1] = u[1] + p[3]
return du
p = tuple(p)
y0 = [0,0]
tspan = np.linspace(0,100,1000)
sol = odeint(fun, y0, tspan, args=p)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
plt.plot(tspan,sol[:,0],'k',linewidth = 5)
plt.plot(tspan,sol[:,1],'r',linewidth = 5)
myleg = plt.legend(['v','u'],\
loc='upper right',prop = {'size':28,'weight':'bold'}, bbox_to_anchor=(1,0.9))
The solution looks like:
Here is the correct solution by Julia, here u1 represent v:
This is the Julia code:
using DifferentialEquations
using Plots
a = 0.02
b = 0.2
c = -65
d = 8
i = 0
p = [a,b,c,d,i]
function fun(du,u,p,t)
if u[1] <30
du[1] = (0.04*u[1] + 5)*u[1] + 150 - u[2] - p[5]
du[2] = p[1]*(p[2]*u[1]-u[2])
else
u[1] = p[3]
u[2] = u[2] + p[4]
end
end
u0 = [0.0;0.0]
tspan = (0.0,100)
prob = ODEProblem(fun,u0,tspan,p)
tic()
sol = solve(prob,reltol = 1e-8)
toc()
plot(sol)
Recommended solution
This uses events and integrates separately after each discontinuity.
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp
a = 0.02
b = 0.2
c = -65
d = 8
i = 0
p = [a,b,c,d,i]
# Define event function and make it a terminal event
def event(t, u):
return u[0] - 30
event.terminal = True
# Define differential equation
def fun(t, u):
du = [(0.04*u[0] + 5)*u[0] + 150 - u[1] - p[4],
p[0]*(p[1]*u[0]-u[1])]
return du
u = [0,0]
ts = []
ys = []
t = 0
tend = 100
while True:
sol = solve_ivp(fun, (t, tend), u, events=event)
ts.append(sol.t)
ys.append(sol.y)
if sol.status == 1: # Event was hit
# New start time for integration
t = sol.t[-1]
# Reset initial state
u = sol.y[:, -1].copy()
u[0] = p[2] #reset to -65
u[1] = u[1] + p[3]
else:
break
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
# We have to stitch together the separate simulation results for plotting
ax.plot(np.concatenate(ts), np.concatenate(ys, axis=1).T)
myleg = plt.legend(['v','u'])
Minimum change "solution"
It appears as though your approach works just fine with solve_ivp.
Warning I think in both Julia and solve_ivp, the correct way to handle this kind of thing is to use events. I believe the approach below relies on an implementation detail, which is that the state vector passed to the function is the same object as the internal state vector, which allows us to modify it in place. If it were a copy, this approach wouldn't work. In addition, there is no guarantee in this approach that the solver is taking small enough steps that the correct point where the limit is reached will be stepped on. Using events will make this more correct and generalisable to other differential equations which perhaps have lower gradients before the discontinuity.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter
from scipy.integrate import solve_ivp
plt.close('all')
a = 0.02
b = 0.2
c = -65
d = 8
i = 0
p = [a,b,c,d,i]
def fun(t, u):
du = [0,0]
if u[0] < 30: #Checking if the threshold has been reached
du[0] = (0.04*u[0] + 5)*u[0] + 150 - u[1] - p[4]
du[1] = p[0]*(p[1]*u[0]-u[1])
else:
u[0] = p[2] #reset to -65
u[1] = u[1] + p[3]
return du
y0 = [0,0]
tspan = (0,100)
sol = solve_ivp(fun, tspan, y0)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
plt.plot(sol.t,sol.y[0, :],'k',linewidth = 5)
plt.plot(sol.t,sol.y[1, :],'r',linewidth = 5)
myleg = plt.legend(['v','u'],loc='upper right',prop = {'size':28,'weight':'bold'}, bbox_to_anchor=(1,0.9))
Result

Simple Dynamical Model in PyMC3

I'm trying to put together a model of a dynamical system in PyMC3, to infer two parameters. The model is the basic SIR, commonly used in epidemiology :
dS/dt = - r0 * g * S * I
dI/dt = g * I ( r * S - 1 )
where r0 and g are parameters to be inferred. So far, I'm unable to get very far at all. The only examples I've seen of putting together a Markov chain like this yields errors about recursion being too deep. Here's my example code.
# Time
t = np.linspace(0, 8, 200)
# Simulated observation
def SIR(y, t, r0, gamma) :
S = - r0 * gamma * y[0] * y[1]
I = r0 * gamma * y[0] * y[1] - gamma * y[1]
return [S, I]
# Currently no noise, we just want to infer params r0 = 16 and g = 0.5
solution = odeint(SIR, [0.99, 0.01, 0], t, args=(16., 0.5))
with pymc.Model() as model :
r0 = pymc.Normal("r0", 15, sd=10)
gamma = pymc.Uniform("gamma", 0.3, 1.)
# Use forward Euler to solve
dt = t[1] - t[0]
# Initial conditions
S = [0.99]
I = [0.01]
for i in range(1, len(t)) :
S.append(pymc.Normal("S%i" % i, \
mu = S[-1] + dt * (-r0 * gamma * S[-1] * I[-1]), \
sd = solution[:, 0].std()))
I.append(pymc.Normal("I%i" % i, \
mu = I[-1] + dt * ( r0 * gamma * S[-1] * I[-1] - gamma * I[-1]), \
sd = solution[:, 1].std()))
Imcmc = pymc.Normal("Imcmc", mu = I, sd = solution[:, 1].std(), observed = solution[:, 1])
#start = pymc.find_MAP()
trace = pymc.sample(2000, pymc.NUTS())
Any help would be much appreciated. Thanks !
I would try defining a new distribution. Something like the following. However, this is not quite working, and I'm not quite sure what I did wrong.
class SIR(Distribution):
def __init__(self, gamma, r0,dt, std):
self.gamma = gamma
self.r0 = r0
self.std = std
self.dt = dt
def logp(self, SI):
r0 = self.r0
std = self.std
gamma = self.gamma
dt = self.dt
S=SI[:,0]
I=SI[:,1]
Si = S[1:]
Si_m1 = S[:-1]
Ii = I[1:]
Ii_m1 = I[:-1]
Sdelta = (Si - Si_m1)
Idelta = (Ii - Ii_m1)
Sexpected_delta = dt* (-r0 * gamma * Si_m1 * Ii_m1)
Iexpected_delta = dt * gamma * Ii_m1 *( r0 * Si_m1 - 1 )
return (Normal.dist(Sexpected_delta, sd=std).logp(Sdelta) +
Normal.dist(Iexpected_delta, sd=std).logp(Idelta))
with Model() as model:
r0 = pymc.Normal("r0", 15, sd=10)
gamma = pymc.Normal("gamma", 0.3, 1.)
std = .5
dt = t[1]-t[0]
SI = SIR('SI', gamma, r0, std,dt, observed=solution[:,:2])
#start = pymc.find_MAP(start={'gamma' : .45, 'r0' : 17})
trace = pymc.sample(2000, pymc.NUTS())

Categories

Resources