I am trying to plot the picewise function below:

  rho0 = 3*10e-11  
  Rd = 3.15
  Rc = 7


    def Density(rho0, r, Rc, Rd):        
        if r < Rc :
          return  rho0 

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


   r = np.linspace(0,20, 1000)
   y= Density(rho0, r, Rc, Rd)
   plt.plot(r, y)
   plt.show()

I am getting this error:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
I do not know how can I solve this problem?or

Recommended Answers

All 2 Replies

I suggest

def Density(rho0, r, Rc, Rd):
    return rho0 * np.exp(-np.maximum(r-Rc, 0)/Rd)

Thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.