Search code examples
pythonpython-3.xodeint

Dymension error on code for solving first order coupled ODE with odeint


I've written this code for a first order ODE system, but apparently python understands 'y' as an integer when I meant it to be a list. Can someone see the mistake? I simply don't know where it is.

import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np
from math import exp

def pfr_model_deltaP(P, X):
    alpha = [0.019, 0.0075]
    P0 = 1.013e6

    dmdX = 5.40/(0.03591*exp((8.39 - 37.78/(4.5 + 5*X)))*((1-X)/(1+X))*(4.5/(4.5 + 5*X))*(P/P0))
    dPdm = -(alpha[0]/2)*(X**2 + 1.90*X + 0.901)*(P0/P)

    return [dmdX, dPdm]

y0 = 1.013e6
X = np.linspace(0, 0.95, 100)
y = odeint(pfr_model_deltaP, y0, X)

m = y[:, 0]
P = y[:, 1]

plt.plot(X, m)
plt.xlabel('Conversion')
plt.ylabel('Mass')
plt.show()

plt.plot(m, P)
plt.xlabel('Mass (kg)')
plt.ylabel('Pressure')
plt.show()

The error message:

RuntimeError                              Traceback (most recent call last)
<ipython-input-6-3d4f22debf1d> in <module>
     15 y0 = 1.013e6
     16 X = np.linspace(0, 0.95, 100)
---> 17 y[m, P] = odeint(pfr_model_deltaP, y0, X)
     18 
     19 plt.plot(X, m)

c:\users\idril\appdata\local\programs\python\python36\lib\site-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)
    242                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
    243                              ixpr, mxstep, mxhnil, mxordn, mxords,
--> 244                              int(bool(tfirst)))
    245     if output[-1] < 0:
    246         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.

Solution

  • Try this: Its just an example of what your function should return

    import matplotlib.pyplot as plt
    from scipy.integrate import odeint
    import numpy as np
    from math import exp
    
    def pfr_model_deltaP(P, X):
        alpha = [0.019, 0.0075]
        P0 = 1.013e6
    
        dmdX = 5.40/(0.03591*exp((8.39 - 37.78/(4.5 + 5*X)))*((1-X)/(1+X))*(4.5/(4.5 + 5*X))*(P/P0))
        dPdm = -(alpha[0]/2)*(X**2 + 1.90*X + 0.901)*(P0/P)
    
        return dPdm 
    
    y0 = 1.013e6
    X = np.linspace(0, 0.95, 100)
    y = odeint(pfr_model_deltaP, y0, X)
    
    m = y[:]
    P = y[:]
    
    plt.plot(X, m)
    plt.xlabel('Conversion')
    plt.ylabel('Mass')
    plt.show()
    
    plt.plot(m, P)
    plt.xlabel('Mass (kg)')
    plt.ylabel('Pressure')
    plt.show()
    

    At this moment you are returning 2 values that is 2 dim array but the odeint takes a function which returns just a single value so i returned just one of your var and it worked but i am not sure that is the exact output you are looking for. So just try to return a single value from your function.

    Hope this helps...