# How to pass additional parameters to curve\_fit in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-pass-additional-parameters-to-curve-fit-in-julia/103031>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 21, 2023, 1:17pm UTC](https://discourse.julialang.org/t/how-to-pass-additional-parameters-to-curve-fit-in-julia/103031 "2023-08-21T13:17:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [August 21, 2023, 1:17pm UTC](https://discourse.julialang.org/t/how-to-pass-additional-parameters-to-curve-fit-in-julia/103031/1 "2023-08-21T13:17:48Z")

</div>

Hi,  
I have a dataset that I would like to fit to my function `pbase(x,pars)` with Julia’s `curve_fit`, whereby `pars` is supposed to contain a non-fitting parameter that controls which functional model is chosen. The function looks like this:

```julia
function pbase(d,pars)
    typ=pars[1]
    pimb=fill(-5.81,length(d))
    @. x=(length(pars) == 3) ? pars[2]*(log10(d)-log10(pars[3])) : pars[2]*log10(d)
    if typ == "tanh"
        @. pimb*=tanh(x)
    elseif typ == "arctan"
        @. pimb*=atan(x)
    end
    pimb
end

```

i.e., `par[1]` contains the selector that is not to be changed during fitting and `par[2:3]` contain the actual fitting parameters. Is this possible at all?

---

<div class="post-metadata">

**Author:** ![johnb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnb/32/44115_2.png) [@johnb](https://discourse.julialang.org/u/johnb)\
**Post date:** [August 21, 2023, 1:27pm UTC](https://discourse.julialang.org/t/how-to-pass-additional-parameters-to-curve-fit-in-julia/103031/2 "2023-08-21T13:27:07Z")

</div>

I don’t think `curve_fit` is a default Julia function. Without knowing which optimization package you are using, I can’t really give you a specific answer, but you can probably wrap your function in another one as follows:

```julia
function helper(d,pars)
    typ = your_selector_value_here #hardcode your selector value here
    new_pars = vcat(typ,pars) #merge selector and mutable params
    pbase(d,new_pars) #call your original function with selector and mutable params
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 21, 2023, 1:58pm UTC](https://discourse.julialang.org/t/how-to-pass-additional-parameters-to-curve-fit-in-julia/103031/3 "2023-08-21T13:58:55Z")

</div>

> [@trg818](#):
>
> i.e., `par[1]` contains the selector that is not to be changed during fitting and `par[2:3]` contain the actual fitting parameters. Is this possible at all?

Anonymous functions and [closures](https://en.wikipedia.org/wiki/Closure_(computer_programming)) / lexical scoping are your friends here:

```julia-auto
p1 = fixed_parameter
curve_fit((x, p) -> pbase(x, [p1; p]), xdata, ydata, p0)

```

(Assuming you are using the `curve_fit` function from [LsqFit.jl](https://github.com/JuliaNLSolvers/LsqFit.jl).)

In my experience, “how do I pass additional parameters” is _by far_ the most common question for every numerical function that takes a function as a parameter (e.g. numerical integration, root finding, minimization, differential equations), also called [higher-order functions](https://en.wikipedia.org/wiki/Higher-order_function). I think it’s a basic gap in computer-science education for people doing computational science.

---

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [August 22, 2023, 1:00am UTC](https://discourse.julialang.org/t/how-to-pass-additional-parameters-to-curve-fit-in-julia/103031/4 "2023-08-22T01:00:46Z")

</div>

Thanks for the replies. Steven’s suggestion basically settled it for me. Yes, I am using LsqFit.jl.  
I’ll add for completeness and clarity (I hope) that `p1` has to be a vector (at least if there is more than one fixed parameter) and that the compiler took issue with the line

```julia
@. x=(length(pars) == 3) ? pars[2]*(log10(d)-log10(pars[3])) : pars[2]*log10(d)

```

from my function. Changing that into an `if` clause resolved that problem.
