Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

MethodError: isless has no method matching isless(::Float64, ::LinSpace{Float64}) #44

Closed
thushalala opened this issue Feb 27, 2017 · 4 comments

Comments

@thushalala
Copy link

Hello, i not managed to find solution for this issue.
When i run this code:

using LsqFit

Ms=70/4000
H=4000
mu=(4/3)*pi*((4.0E-9)^3)
sigma=0.3*mu
Kb=1.38E-23
Ku=24000
M=800/4000

langiv(x)=(coth(x)-1/x)
function Mzfc(T,prms)
    count=10000
    M=prms[1]
    Ku=prms[2]
    Vc=(25)*Kb*T/Ku
    temp=Array(Float64,count)
    V=linspace(mu-3.2*sigma,mu+3.2*sigma,count)
    res=0
    for i in 1:count
        if V[i]<Vc
            temp[i]=Ms*langiv((M*V[i]*H)/(T*Kb))*exp(-(V[i]-mu)*(V[i]-mu))/sqrt(2*pi*sigma*sigma)
        else
            temp[i]=Ms*H*M./(3.*Ku)*exp(-(V[i]-mu)*(V[i]-mu))/sqrt(2*pi*sigma*sigma)
        end
    end
    for i in 2:count
        res=(temp[i-1]+temp[i])*(V[i]-V[i-1])/2+res
    end
    return res
end

T=linspace(5,405,401)
res=Array(Float64,length(T))

params=[M,Ku]
for i in 1:401
    res[i]=Mzfc(T[i],params)
end

 fit=curve_fit(Mzfc,T,res,[0.5,20000])

It results in error:

MethodError: `isless` has no method matching isless(::Float64, ::LinSpace{Float64})
Closest candidates are:
  isless(::Float64, ::Float64)
  isless(::AbstractFloat, ::AbstractFloat)
  isless(::Real, ::AbstractFloat)
...
in < at operators.jl:33
in Mzfc at In[7]:22
in f at /home/vlad/.julia/v0.4/LsqFit/src/curve_fit.jl:39
in levenberg_marquardt at /home/vlad/.julia/v0.4/LsqFit/src/levenberg_marquardt.jl:62
in lmfit at /home/vlad/.julia/v0.4/LsqFit/src/curve_fit.jl:30
in curve_fit at /home/vlad/.julia/v0.4/LsqFit/src/curve_fit.jl:40

Looks like i do something wrong and it somehow connected with my integration inside function.

@blakejohnson
Copy link
Contributor

The problem is that curve_fit expects a vectorized model function (see issue #12). i.e. it expects that Mzfc(T, params), will work.

You can construct a wrapper around Mzfc to fix this, e.g.:

Mzfc_vec(T, params) = [Mzfc(t, params) for t in T]

Since Julia base has moved away from vectorized functions, it is likely that a future version of LsqFit will work the way you expect.

@blakejohnson
Copy link
Contributor

See also issue #12.

@thushalala
Copy link
Author

Thank you for your help!
I added 1 thing:
Mzfc_vec(T, params) = Array{Float64}([Mzfc(t, params) for t in T])
and now this works.

@blakejohnson
Copy link
Contributor

Even better would be to drop one set of parens:

Mzfc_vec(T, params) = Array{Float64}[Mzfc(t, params) for t in T]

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants