# How do I use LsqFit to fit points with an associated error?

I have these points I want to fit:

``````x      y          err
24.0   0.0386341  4.29118e-5
24.33  0.03952    2.10541e-5
24.66  0.0398329  1.76137e-5
25.0   0.0399503  1.75643e-5
25.33  0.0399654  1.63191e-5
25.66  0.0398724  1.72015e-5
26.0   0.0397342  2.06237e-5
``````

I’m using LsqFit.jl to do this. The problem is that I’m not sure how to use the errors. I think I have to give a weight vector to the `curve_fit` function, but I get crazy results when I try this. For example, fitting this data without weights, only using the y column, with the model

``````(x, p) -> p[2] .+ p[3] .* (x .- p[1]) .^ 2 + p[4] .* (x .- p[1]) .^ 3 + p[5] .* (x .- p[1]) .^ 4
``````

yields these parameters:

``````25.111393704165895
0.039954617396037284
-0.00027948006042561755
0.0003111490269699087
-0.00035643137150145855
``````

with these errors (which are provided by LsqFit’s `stderror` function)

``````0.0869412489407782
2.055454237390897e-5
0.00016140094273954191
0.00019062903506274383
0.00010671690424624444
``````

I then tried to build a weight vector. My idea was to give the maximum weight to the point with the least error, and viceversa. So, I took the inverse squared of the `err` column above, and I normalized the result by dividing each number by the maximum of the vector. I got:

``````0.14462239237947602
0.6007810802559573
0.8584025972308643
0.8632298289063326
1.0
0.9000334378603128
0.6261211360104819
``````

Using this vector as weights in `curve_fit`, yielded these model parameters:

``````25.120191638543336
0.03995798049346908
-0.00030331529662780605
0.0002925450095476317
-0.0003231972927598868
``````

which are close enough, but the associated errors for these parameters are nonsensical:

`````` 3146.8126470564166
0.7860552955285658
6.117611017553491
6.862314788647446
4.823917655489848
``````

Clearly, I’m using weights in the wrong way. What should I do?

what is `err` in your first table?

Nevermind, I found this post that explains how to do it. Basically, I don’t have to normalize the vector of weights. I simply take the inverse squared of each measurement error.