# Can't fit nonlinear function including '^' in LsqFit

Hi there,

When I try to fit to a concentration-response curve (Hill equation) and use the exponent as a free parameter, Julia throws me an error complaining that the base should be a complex number. Even using lower bounds for curve_fit that prohibit a negative base or an exponent < 1, it still throws an error. Can anyone help? Thanks in advance.

``````@. H(x,p) = p - (p-p) / ( 1. + (x/p)^p )
Hfit = LsqFit.curve_fit(H,xdata,ydata,[1.,0.,0.5,1.],lower=[0.,0.,0.,1.])

``````

gives

``````
DomainError with -...:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
``````

It works fine for me. Since you don’t give example data, I generated some:

``````fx(x) = 1.5 - 1.3 / (1. + (x/0.7)^1.3)
xdata = range(0,3,length=50)
ydata = fx.(xdata).*(1 .+0.1randn(50))
plot(xdata,ydata,legend=:bottomright)
``````

Next, doing model fitting:

``````julia> @. H(x,p) = p - (p-p) / ( 1. + (x/p)^p )
julia> Hfit = LsqFit.curve_fit(H,xdata,ydata,[1.,0.,0.5,1.],lower=[0.,0.,0.,1.])
julia> par = coef(Hfit)
4-element Array{Float64,1}:
1.4779674751492053
0.21988771220240352
0.6945565635031009
1.6532411388662613

julia> plot(xdata,ydata,label="data")
julia> plot!(xdata,x-> par - (par-par)/(1+(x/par)^par),label="fitted model",legend=:bottomright)
``````

Of course, if you have negative elements in `xdata`, you will have problems as `x/p` will be negative, and raising this to a number that is not an even integer will produce a complex number.
Another problem is that you allow `p` to have zero as lower bound, hence allows the fitting algorithm to divide by zero in `x/p`.
So, I’d check the minimal value in `xdata`, and change the lower value of `p` to `1e-2` or something.
You do indicate that `xdata` contains concentrations, which should be positive at the outset. Maybe there are some errors in the measurements so that some of them are (slightly) negative??