How to pass arguments as tuple to odeint?
Question:
I wanted to use odeint of scipy with a function
def func(y,t,a=123,b=456)
then use it as
odeint(func,y0,t)
If I want to change the values a and b using args
odeint(func,y0,t,args=(a=1,b=2))
It complains that arguments are not tuple.
It may be a very elementary question,
how can I pass keyword arguments as a tuple?
odeint(func,y0,t,args=...?)
Answers:
You just need to remove the assignments to make a legit tuple:
odeint(func,y0,t,args=(123, 456))
There’s another answer here with an example of calling odeint with arguments.
I know this is old, but here is my solution:
-
first, we must segment the time we are going to use according to the parameters we are going to use.
-
then, we made a zip(time, a, b) loop, therefore, a and b must have the same length.
def time_segment(tobesimilar, dt_float=0.01):
'''time_segment produces an array of dt-length time intervals, the shape of the output is ( len(tobesimilar), 2)
inputs:
tobesimilar: array or list, which length (len) is only used as a reference to be duplicated
dt: float, dt refers to what is going to be our integration step, in this case, it is used as the interval length.
output:
tmp: array, such as tmp.shape= (len(tobesimilar),2)
'''
newt=np.arange(0,len(tobesimilar)*dt, dt)
#segments the nwet[a,b] in n intervals [a_i,b_i]
#whith a_0 = 0 and b_n = newt[-1]
if len(newt)%2==0: #if len(newt) is even, no problem
tmp = [[newt[i],newt[i+1]] for i in range(len(newt)-1)]
else: #if len(newt) is odd, just add an item
newt= np.append(newt,newt[-1]+dt)
tmp = [[newt[i],newt[i+1]] for i in range(len(newt)-1)]
#turns tmp into an array cause <3 (heart) numpy
tmp=np.array(tmp)
return tmp
def func(t,y,a=123,b=456):
return print( "what ever your function does" )
a=np.array([1,2,3,45,67,88])
b=np.array([54,4,1,99,42,100])
y_0= 0 #initial condition
solution=np.asanyarray([y_0]) #preallocate the output
time=time_segment(vol, dt=dt)
for ts in zip(time, a, b):
y = odeint(func, y0=solution[-1], t=ts[0], args=(ts[1],ts[2],), tfirst= True)
solution = np.append(solution, [y.T[0][1]])
I wanted to use odeint of scipy with a function
def func(y,t,a=123,b=456)
then use it as
odeint(func,y0,t)
If I want to change the values a and b using args
odeint(func,y0,t,args=(a=1,b=2))
It complains that arguments are not tuple.
It may be a very elementary question,
how can I pass keyword arguments as a tuple?
odeint(func,y0,t,args=...?)
You just need to remove the assignments to make a legit tuple:
odeint(func,y0,t,args=(123, 456))
There’s another answer here with an example of calling odeint with arguments.
I know this is old, but here is my solution:
-
first, we must segment the time we are going to use according to the parameters we are going to use.
-
then, we made a zip(time, a, b) loop, therefore, a and b must have the same length.
def time_segment(tobesimilar, dt_float=0.01): '''time_segment produces an array of dt-length time intervals, the shape of the output is ( len(tobesimilar), 2) inputs: tobesimilar: array or list, which length (len) is only used as a reference to be duplicated dt: float, dt refers to what is going to be our integration step, in this case, it is used as the interval length. output: tmp: array, such as tmp.shape= (len(tobesimilar),2) ''' newt=np.arange(0,len(tobesimilar)*dt, dt) #segments the nwet[a,b] in n intervals [a_i,b_i] #whith a_0 = 0 and b_n = newt[-1] if len(newt)%2==0: #if len(newt) is even, no problem tmp = [[newt[i],newt[i+1]] for i in range(len(newt)-1)] else: #if len(newt) is odd, just add an item newt= np.append(newt,newt[-1]+dt) tmp = [[newt[i],newt[i+1]] for i in range(len(newt)-1)] #turns tmp into an array cause <3 (heart) numpy tmp=np.array(tmp) return tmp def func(t,y,a=123,b=456): return print( "what ever your function does" ) a=np.array([1,2,3,45,67,88]) b=np.array([54,4,1,99,42,100]) y_0= 0 #initial condition solution=np.asanyarray([y_0]) #preallocate the output time=time_segment(vol, dt=dt) for ts in zip(time, a, b): y = odeint(func, y0=solution[-1], t=ts[0], args=(ts[1],ts[2],), tfirst= True) solution = np.append(solution, [y.T[0][1]])