# Converting and applying MATLAB lsqnonlin function in Julia with multiple function arguments

**URL:** <https://discourse.julialang.org/t/converting-and-applying-matlab-lsqnonlin-function-in-julia-with-multiple-function-arguments/82440>\
**Category:** Optimization (Mathematical)\
**Tags:** question, matlab, optimization, convert\
**Created:** [June 8, 2022, 3:32pm UTC](https://discourse.julialang.org/t/converting-and-applying-matlab-lsqnonlin-function-in-julia-with-multiple-function-arguments/82440 "2022-06-08T15:32:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JannisHoch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jannishoch/32/37007_2.png) [@JannisHoch](https://discourse.julialang.org/u/JannisHoch)\
**Post date:** [June 8, 2022, 3:32pm UTC](https://discourse.julialang.org/t/converting-and-applying-matlab-lsqnonlin-function-in-julia-with-multiple-function-arguments/82440/1 "2022-06-08T15:32:39Z")

</div>

I am trying to convert some MATLAB code into Julia. For the largest part this went pretty smooth, but I am now stuck with converting the MATLAB [lsqnonlin][1] function.

After scanning available options, I think that the [LsqFit.jl][2] package with its `curve_fit` function is the best way to go. Please correct me if I am wrong.

The original MATLAB code looks like this, where a function `calc_z` with given lower and upper boundaries (`lb` resp. `ub`) and initial guess `z` should yield optimal values of `z`. `**kwargs` contains all other function arguments required for `calc_z`.

```
z_opt = lsqnonlin(@calc_z, z, lb, ub, lsq_options, **kwargs)

```

Internally, the function `calc_z` computes the error between initial `z` and observed values of z (`z_obs` which are part of `**kwargs`) plus two additional penalty terms which should be minimized to obtain the optimal values of z, `z_opt`.

The question now is how to correctly implement this in Julia. Thus far, I understand that I need something like

```
using LsqFit  

fit = curve_fit(calz_z, xdata, ydata, p0, lower=lb, upper=ub)
# calz_z rewritten in Julia

z_opt = fit.param

```

What is not clear to me is how to deal with the (kw)args required for `calc_z`. Do all `**kwargs` go into `p0`? And is `xdata` equivalent to `z` and `ydata` equivalent to `z_obs`?

It is also unclear to me whether Julia `curve_fit` implicitly computes the sum of squares of the components as `lsqnonlin` does in MATLAB?

Many thanks for your help!

---

<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:** [June 8, 2022, 6:05pm UTC](https://discourse.julialang.org/t/converting-and-applying-matlab-lsqnonlin-function-in-julia-with-multiple-function-arguments/82440/2 "2022-06-08T18:05:57Z")

</div>

> [@JannisHoch](#):
>
> What is not clear to me is how to deal with the (kw)args required for `calc_z`.

Just pass a closure to capture any additional arguments: `curve_fit(x -> calz_z(x; kws...), ...)`
