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

**URL:** <https://discourse.julialang.org/t/cant-fit-nonlinear-function-including-in-lsqfit/49199>\
**Category:** Optimization (Mathematical)\
**Created:** [October 28, 2020, 6:43pm UTC](https://discourse.julialang.org/t/cant-fit-nonlinear-function-including-in-lsqfit/49199 "2020-10-28T18:43:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gedankenexperiment](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@gedankenexperiment](https://discourse.julialang.org/u/gedankenexperiment)\
**Post date:** [October 28, 2020, 6:43pm UTC](https://discourse.julialang.org/t/cant-fit-nonlinear-function-including-in-lsqfit/49199/1 "2020-10-28T18:43:49Z")

</div>

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.

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

```

gives

```julia

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

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 28, 2020, 9:11pm UTC](https://discourse.julialang.org/t/cant-fit-nonlinear-function-including-in-lsqfit/49199/2 "2020-10-28T21:11:51Z")

</div>

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

```julia
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)

```

leads to:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7bb148d0c533a7a4883a3f5ec0c52a5dd8d8a105.png)

Next, doing model fitting:

```julia
julia> @. H(x,p) = p[1] - (p[1]-p[2]) / ( 1. + (x/p[3])^p[4] )
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[1] - (par[1]-par[2])/(1+(x/par[3])^par[4]),label="fitted model",legend=:bottomright)

```

leads to:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/c/fcaf0e3aae958e64bc808ffde7d908c562f51e23.png)

Of course, if you have negative elements in `xdata`, you will have problems as `x/p[3]` 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[3]` to have zero as lower bound, hence allows the fitting algorithm to divide by zero in `x/p[3]`.

So, I’d check the minimal value in `xdata`, and change the lower value of `p[3]` 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??

---

<div class="post-metadata">

**Author:** ![gedankenexperiment](https://avatars.discourse-cdn.com/v4/letter/g/a183cd/32.png) [@gedankenexperiment](https://discourse.julialang.org/u/gedankenexperiment)\
**Post date:** [October 29, 2020, 8:33am UTC](https://discourse.julialang.org/t/cant-fit-nonlinear-function-including-in-lsqfit/49199/3 "2020-10-29T08:33:18Z")

</div>

There were indeed a few negative values in xdata. Thanks!
