0

Here is the code, I am using python 3.5

from scipy.optimize import fsolve
from scipy.integrate import dblquad
from numpy import sqrt,cos,pi,absolute

Ueh=2320.5
Uhh=2192.5
ED=120
LCP=-59.28179796
UCP=-58.96721714


def Ih(mu,x):
    var,err= dblquad(lambda ky, kx: 1/sqrt((ED-mu-2540*(2-cos(kx)-cos(ky)))**2+x**2)/8/pi/pi, -pi, pi, lambda kx:-pi,lambda kx: pi)
    return var

def Ie(mu,x):
    var,err= dblquad(lambda ky, kx: 1/sqrt((2540*(2-cos(kx)-cos(ky))-mu)**2+x**2)/8/pi/pi, -pi, pi, lambda kx:-pi,lambda kx: pi)
    return var

def spp(mu,p):
    x, y = p
    return (absolute(x)-2*Ueh*absolute(y)*Ih(mu,y),4*Ueh**2*Ie(mu,x)*Ih(mu,y)/(1+Uhh*Ih(mu,y))-1)

def spm(mu,y):
    return Uhh*Ih(mu,y)-1    

def sis(mu,p):
    x,y=p
    return(Uhh*Ih(mu,y)-1,  Ie(mu,x)*2*Ueh**2/Uhh-1)


def solution(mu):
    if mu<=LCP:
        Dele=0
        Delh=fsolve(lambda y:spm(mu,y),4)
        phase=0
    elif mu> LCP and mu<UCP:
        Dele,Delh=fsolve(lambda p: sis(mu,p),(4,4))
        phase=-Uhh/2/Ueh*Dele/Delh
    elif mu>=UCP:
        Dele,Delh=fsolve(lambda p: spp(mu,p),(9,4)) 
        phase=-1        
    return Dele,Delh,phase 

if I take mu=-60, following if mu<=LCP,

print(solution(-60))
(0, array([ 4.1349274]), 0)

it I take mu in two other elif, the return is without array

print(solution(-59))
(8.2553379473241311, 4.1210736864119193, -0.94635157721569463)

print(solution(-58))
(8.9677408867771558, 4.2580061198120394, -1)

How to change it to make the first return has same format as other two? I know I could take [0] from a array to get the number. It would be better If I could understand what is the reason the fsolve return array in the first case?

p.s
  • 51
  • 3
  • You need to work on your debugging skills. You can start by reading this: https://stackoverflow.com/help/mcve. The difference comes from the value of the `Delh` variable. Do you see what you're doing differently with the `Delh` variable in the `if` versus the `elif`s? – mercator Oct 23 '17 at 20:51
  • In the if, Delh come from fsolve for a variable. in elif, Delh together with Dele come from fsolve for two two variable. – p.s Oct 23 '17 at 21:56
  • `fsolve = lambda: [1,2]; a, b = fsolve(); x = fsolve(); print a, b, x`. Compare the values of `a`, `b` and `x`. Is this not exactly the same as what's happening in your code? – mercator Oct 23 '17 at 23:05
  • The answer is correct as I wanted. Why it is an array instead of a number in the if mu – p.s Oct 23 '17 at 23:12
  • `fsolve` _always_ returns an array: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html#scipy.optimize.fsolve Also see https://stackoverflow.com/questions/431866/ignore-python-multiple-return-value – mercator Oct 24 '17 at 07:01

0 Answers0