# Nonlinear fiting with posibility to fix some of the parameters

**URL:** <https://discourse.julialang.org/t/nonlinear-fiting-with-posibility-to-fix-some-of-the-parameters/86874>\
**Category:** General Usage\
**Tags:** optim\
**Created:** [September 7, 2022, 7:50am UTC](https://discourse.julialang.org/t/nonlinear-fiting-with-posibility-to-fix-some-of-the-parameters/86874 "2022-09-07T07:50:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [September 7, 2022, 7:50am UTC](https://discourse.julialang.org/t/nonlinear-fiting-with-posibility-to-fix-some-of-the-parameters/86874/1 "2022-09-07T07:50:41Z")

</div>

Dear all,

I am trying to figure out how to fit nonlinear function to my data using Optim package.

Here is example

```julia

# data x and y

using Optim

model(x,p) = p[1] .+ p[2] .* x .+ p[3] .*x.^2

loss_function(p) = sum((model(x,p) .- y).^2

p0 = [0., 0., 0.]

result = optimize(loss_function, p0 ) 

```

Now I would like to fix one of the fitting parametrs, e.g. p[2] = 1.0  
And fit the data just using parameters p[1] and p[3].

Is there any method to achieve it?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 7, 2022, 8:51am UTC](https://discourse.julialang.org/t/nonlinear-fiting-with-posibility-to-fix-some-of-the-parameters/86874/2 "2022-09-07T08:51:41Z")

</div>

You could define a new `loss_function` for example

```julia
loss_function_projected(p2) = q -> loss_function( (q[1], p2, q[2]) )

q0 = [0.0, 0.0]
result = optimize(loss_function_projected(1.0), q0 ) 

```

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [September 7, 2022, 12:25pm UTC](https://discourse.julialang.org/t/nonlinear-fiting-with-posibility-to-fix-some-of-the-parameters/86874/3 "2022-09-07T12:25:11Z")

</div>

> [@SteffenPL](#):
>
> ```julia
> loss_function_projected(p2) = q -> loss_function( (q[1], p2, q[2]) )
> 
> q0 = [0.0, 0.0]
> result = optimize(loss_function_projected(1.0), q0 ) 
> 
> ```

Oh thanks that is really nice. For fixing one of the parameter it works pretty nice.  
But I don’t know how to fix more parametrs.

```julia

loss_function_projected(p2) = q -> loss_function( (p2[1], p2[2], q[2]) )

pp = [1, 2]
q0 = [0.0]
result = optimize(loss_function_projected(pp), q0 ) 

```

gave me BoundsError: attempt to access 1-element Vector{Float64} at index [2]

So I probably miss the concept of the function projection.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 7, 2022, 12:36pm UTC](https://discourse.julialang.org/t/nonlinear-fiting-with-posibility-to-fix-some-of-the-parameters/86874/4 "2022-09-07T12:36:17Z")

</div>

The error was just that `q` is then only of length one, i.e. instead of using `q[2]` you need `q[1]`:

```julia
loss_function_projected(p12) = q -> loss_function( (p12[1], p12[2], q[1]) )

p12 = [1, 2]
q0 = [0.0]
result = optimize(loss_function_projected(p12), q0 ) 

```

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [September 8, 2022, 8:22am UTC](https://discourse.julialang.org/t/nonlinear-fiting-with-posibility-to-fix-some-of-the-parameters/86874/5 "2022-09-08T08:22:19Z")

</div>

I see, thank you very much.
