How to pass additional parameters to curve_fit in Julia?

Anonymous functions and closures / lexical scoping are your friends here:

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.)

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. I think it’s a basic gap in computer-science education for people doing computational science.

3 Likes