AL-Zetoun AL-Zetoun - 7 months ago 8
Python Question

define three functions into one in python

I have defined these functions in python :

def Density(rho0,r,Rc,Rd):

if r < Rc :
return rho0

elif r> Rc :
return rho0*np.exp(-(r-Rc)/Rd)

def Thickness_H(H0,r,Rh):
return H0*np.exp(r/Rh)


Now I want to use these two functions to write the final equation using these two function:

the function look like this( I don't know how can I put the first and second function into the function below:

def Density_distribution(H0,Rh,r,z,rho0,Rc,Rd):

return firstfunction*np.exp(-z/secondfunction)


and plot it:

r = np.linspace(0.,10., 100)
p =Density_distribution(H0,Rh,r,z,rho0,Rc,Rd)
plt.plot(r, p)
plt.show()


Thank you for your help

Answer

Simply call the functions to get their return value in any statement:

def Density_distribution(H0, Rh, r, z, rho0, Rc, Rd):
      return Density(rho0, r, Rc, Rd) * np.exp(-z / Thickness_H(H0, r, Rh))

Or in case you receive the rho0 parameter as string in Density_distribution, just convert it to a float before calling the Density function:

def Density_distribution(H0, Rh, r, z, rho0, Rc, Rd):
      return Density(float(rho0), r, Rc, Rd) * np.exp(-z / Thickness_H(H0, r, Rh))
Comments