# Problems fitting a model using \`LsqFit.jl\`

**URL:** https://discourse.julialang.org/t/problems-fitting-a-model-using-lsqfit-jl/77003
**Category:** General Usage
**Tags:** curve-fitting
**Created:** [February 24, 2022, 1:47am UTC](https://discourse.julialang.org/t/problems-fitting-a-model-using-lsqfit-jl/77003 "2022-02-24T01:47:40Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [February 24, 2022, 4:59pm UTC](https://discourse.julialang.org/t/problems-fitting-a-model-using-lsqfit-jl/77003/2 "2022-02-24T16:59:22Z")

</div>

The `DomainError` is caused because you are taking the square root of a negative real number, and the workaround is to cast it to complex:

```julia-repl
julia> (-1)^0.5
ERROR: DomainError with -1.0:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
...

julia> (Complex(-1))^0.5 # workaroud
0.0 + 1.0im

```

The `InexactError` is caused because you are trying to store the complex number calculated above in a vector of real numbers. Your `Cvvals` is a vector of `Float64`, but `kterms()` is generating a complex number because it calculates the output using the complex number obtained by square-rooting a negative number as above.

In the error message, the output `Float64(0.5141891571782113 - 1.6389735916551083e-6im)` generated by `kterms()` seems to be nearly real, and the imaginary part seems to be just a rounding error. If you are sure that `kterms()` should return always real numbers, discard its imaginary part and take only the real part when storing in `Cvvals`.

---

_[View the full topic](https://discourse.julialang.org/t/problems-fitting-a-model-using-lsqfit-jl/77003)._
