solving system of coupled equation using RK 45 method

Viewed 92

I am trying to solve the System of coupled differential equation but I got following error can any one resolve this problem

dY[1] = (1/M*C_p)((m_c*C_p*(Y[0]-Y[1]))-(U_l*(A_s_2)*(Y[1]-Ta))) TypeError: 'float' object is not callable

import math
import scipy.integrate as spi
import numpy as np
import pandas as pd
def odes(t,Y):                                                                           
    m_c=0.9                                          
    C_p =4200
    Tc_O=91                                                                                   
    U_l =0.8                                          
    D_st=2                                         
    L_s=1
    rho=1000
    V=0.5
    M=(rho*V)/3                                            
    A_s_1=((math.pi*pow(D_st,2))/4)+((math.pi*D_st*L_s)/3)
    A_s_2=((math.pi*D_st*L_s))/3
    A_s_3=((math.pi*pow(D_st,2))/4)+((math.pi*D_st*L_s)/3)
    Ta =20 
    dY = np.zeros((3))
    dY[0] = (1/M*C_p)*((m_c*C_p*(Tc_O-Y[0]))-(U_l*(A_s_1)*(Y[0]-Ta)))
    dY[1] = (1/M*C_p)((m_c*C_p*(Y[0]-Y[1]))-(U_l*(A_s_2)*(Y[1]-Ta)))
    dY[2] = (1/M*C_p)((m_c*C_p*(Y[1]-Y[2]))-(U_l*(A_s_3)*(Y[2]-Ta)))
    return dY
t_start, t_end = 0, 3600.0
Y = np.array([91,89,75]); 
Yres = spi.solve_ivp(odes, [t_start, t_end], Y, method='RK45', max_step=60)
#---- Results ---------------------
yy = pd.DataFrame(Yres.y).T
tt = np.linspace(t_start,t_end,yy.shape[0])
print(yy)
2 Answers

The problem is here:

dY[1] = (1/M*C_p)((m_c*C_p*(Y[0]-Y[1]))-(U_l*(A_s_2)*(Y[1]-Ta)))
                 ^
                 |
              No multiplication sign!

Python isn't like math. If you have two groups of parentheses next next to each other without an operator between them, it doesn't mean multiply. It means 'call function'

So if you do this:

print((3)(4))

You'll get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable

And you have the same problem on the line below that one.

you can make it cleaner, for example:

def odes(t, Y):
    Tc_O = 91
    D_st = 2
    L_s = 1
    A_s_1 = A_s_3 = ((math.pi * pow(D_st, 2)) / 4) + ((math.pi * D_st * L_s) / 3)
    A_s_2 = ((math.pi * D_st * L_s)) / 3
    dY = np.zeros((3))
    dY[0] = calc(Tc_O, Y[0], A_s_1)
    dY[1] = calc(Y[0], Y[1], A_s_2)
    dY[2] = calc(Y[1], Y[2], A_s_3)
    return dY


def calc(Y1, Y2, A_s):
    m_c = 0.9
    C_p = 4200
    U_l = 0.8
    rho = 1000
    V = 0.5
    M = (rho * V) / 3
    Ta = 20
    x = (1 / M * C_p) * ((m_c * C_p * (Y1 - Y2)) - (U_l * (A_s) * (Y2 - Ta)))
    return x


t_start, t_end = 0, 3600.0
Y = np.array([91, 89, 75])
Yres = spi.solve_ivp(odes, [t_start, t_end], Y, method="RK45", max_step=60)
# ---- Results ---------------------
yy = pd.DataFrame(Yres.y).T
tt = np.linspace(t_start, t_end, yy.shape[0])
print(yy)
Related