# How to fit functions with same parameters?

**URL:** <https://discourse.julialang.org/t/how-to-fit-functions-with-same-parameters/68192>\
**Category:** General Usage\
**Tags:** question, fit, physics, curve-fitting\
**Created:** [September 15, 2021, 6:03am UTC](https://discourse.julialang.org/t/how-to-fit-functions-with-same-parameters/68192 "2021-09-15T06:03:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![y-iizawa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/y-iizawa/32/25626_2.png) [@y-iizawa](https://discourse.julialang.org/u/y-iizawa)\
**Post date:** [September 15, 2021, 6:03am UTC](https://discourse.julialang.org/t/how-to-fit-functions-with-same-parameters/68192/1 "2021-09-15T06:03:22Z")

</div>

Hello.  
I have some functions with the same parameters and want to fit these parameters with each data.  
Example:  
p is parameters set. Now I consider two parameters p[1] and p[2] as examples.

```julia
example_1(x) = p[1] + p[2] * x 
example_2(x) = p[1] * x^2 - p[2] * x
example_3(x) = p[1] + p[2] - x^2  

```

And I have data sets for each function:

```julia
x_1, y_1, error_y_1 #data for function example_1
x_2, y_2, error_y_2 #data for function example_2
x_3, y_3, error_y_3 #data for function example_3

```

How do you fit parameters in such a situation? What package do you use?

Thank you for your kindness 🥰

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 15, 2021, 7:12am UTC](https://discourse.julialang.org/t/how-to-fit-functions-with-same-parameters/68192/3 "2021-09-15T07:12:59Z")

</div>

“fit” can mean many things. Maybe start with [GitHub - JuliaNLSolvers/LsqFit.jl: Simple curve fitting in Julia](https://github.com/JuliaNLSolvers/LsqFit.jl) ? But that package cannot make use of the `error_y_1` info, for that you could code your own objective and use Optim.jl to minimize this. Or use some Bayesian approach.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 15, 2021, 7:35am UTC](https://discourse.julialang.org/t/how-to-fit-functions-with-same-parameters/68192/4 "2021-09-15T07:35:22Z")

</div>

I think I understand your problem.  
You have only one parameter tuple p=(p1,p2) which must fit three data sets  
{x\_1,y\_1},{x2,y2} and {x3,y3} simutanenously.

The solution is to write your own fitness function fitness(p)  
then use BlackBoxOptim to find the optimum value of the p tuple

[https://github.com/robertfeldt/BlackBoxOptim.jl](https://github.com/robertfeldt/BlackBoxOptim.jl)
