mardi 22 novembre 2016

Plotting a conditional function using matplotlib in Python

I'm trying to create a line or scatter plot of this algorithm and it gives me the error

"The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()."

I have looked up possible solutions to this error but I don't think any apply to me.

def Psmb(z):
    return rhos*G*(4.0/3.0)*np.pi*(1/z**2)*(rhom*(rm**3) + rhos*(z - rm**3))
def Pmcb(z):
    return rhom*G*(4.0/3.0)*np.pi*(1/z**2)*(rhoc*(rc**3) + rhom*(z - rc**3))
def P(x):
    if x > r:
        return "The point is outside of the planet"
    elif x == r:
        return 1
    elif x > rm and x < r:
        return (integ.quad(1000*gi(x), x, r))[0]
    elif x == rm:
        return (integ.quad(Psmb, x, r))[0]
    elif x > rc and x < rm:
        return (integ.quad(1000*gi(x), x, rm) + P(rm))[0]
    elif x == rc:
        return (integ.quad(Pmcb, x, rm) + P(rm))[0]
    elif x < rc and x != 0:
        return (integ.quad(1000*gi(x), x, rc) + P(rc))[0]
    else:
        return ((2.0/3.0)*np.pi*G*(rhoc**2)*r**2)

xr = np.linspace(0, 1187000, 1000)
plt.plot(xr, P(xr))

Is there a way to plot this function without separating each condition?

Aucun commentaire:

Enregistrer un commentaire