I am trying to solve a differential equation and I get this following error
/usr/local/lib/python3.7/dist-packages/scipy/integrate/odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
243 full_output, rtol, atol, tcrit, h0, hmax, hmin,
244 ixpr, mxstep, mxhnil, mxordn, mxords,
245 int(bool(tfirst)))
246 if output[-1] < 0:
247 warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."
RuntimeError: The array return by func must be one-dimensional, but got ndim=2.**
My code is
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
f_ini = [0.09,0.09]
C0 = 0.083/f
k = 0.04
v = 1.05
def rxn1(C,t):
return np.array([f*C0/v-f*C[0]/v-k*C[0], f*C[0]/v-f*C[1]/v-k*C[1]])
t_points = np.linspace(0,500,1000)
c_points = scipy.integrate.odeint(rxn1, f_ini,t_points)
plt.plot(t_points, c_points[:,0])
plt.plot(t_points,c_points[:,1])`
I know this similar question was asked here. How to solve a system of differential equations. However I want to solve it using np.array. Thank you so much!
If "f" is variable and dependent on "t", then you can define it as a function of "t" and use that function instead of "f" in your rxn1 function. For example:
def f(t):
# relation between f and t
return value
def rxn1(C,t):
return np.array([f(t)*C0/v-f(t)*C[0]/v-k*C[0], f(t)*C[0]/v-f(t)*C[1]/v-k*C[1]])