# How to do curve fitting on a data set with errors associated to each data point (y ± Δ y)

130 views (last 30 days)

Show older comments

I have a data set in the form x (independent variable), y ± Δy (dependent variable). As you can see there's an error associated with each data point in dependent variable. I want to do curve fitting for this data set and can you help me with how to do that.

I know how to use curve fitting tool box to fit a curve for a data set in the form x,y. But I don't know how to use matlab to do curve fitting when there's an error associated with each data point.

##### 3 Comments

SWATI BHARGAVA
on 3 Dec 2019

### Answers (3)

John D'Errico
on 26 Feb 2019

Edited: John D'Errico
on 26 Feb 2019

Since you have explicit bounds on y +/- deltay, you cannot just use simple weights. As well, you cannot use the curve fitting toolbox. Instead, you can set this up as an fmincon problem, with nonlinear constraints. Each data point will provide two nonlinear constraints, an upper and lower bound for the curve at each given point. This is much simpler of course if the problem is a linear one, since then you could use lsqlin instead with simple linear inequality constraints.

So, is your model linear in the parameters? I.e., any polynomial model, is linear in the parameters. So models like y = a0+a1*x, a0+a1*x+a2*x^2, etc. Each of those models are linear in the parameters, though they need not be linear in x.

Alternatively, a model like y=a0+a1*sin(a2*x) is linear in two of the parameters, but nonlinear in a2. This would take somewhat more effort to solve than the simple polynomial models I described, but it is still solvable. Other models, thus perhaps a1*exp(a2*x) wuld also have a solution method that would be pehaps most appropriate.

Now, perhaps you have no a-priori choice of model? In that case, a regression spline model probably makes sense. And I would point out that my SLM toolbox actually allows the suer to provide an explicit set of upper and lower bounds that can be used to constrain the curve at each data point.

The point is, the most appropriate solution method will depend on the model, but I don't know what model you have.

##### 0 Comments

Ankit Labh
on 20 Jan 2020

I have exactly the same problem. I wanted to fit gaussian with all y values as y +- Delta_y. Considering the errors associated to y value, I still do not get the answer to do the fitting. If you get any answers, do post here.

Thank you very much

##### 0 Comments

Boaz Zick
on 20 Apr 2022

Edited: Boaz Zick
on 20 Apr 2022

I just found something that can maybe help you.

watch from 10:10.

Monte Carlo method

the idea is to add noise to your data to simulate alot of measurments that could have been made with your error in measurment, and then curve fit each one of those measurments, and check the mean and standart deviation of your coefficients.

here is an example for a linear fit, in order to fit other curves change fitlm and add coefficients as necessary.

%%

rep=500;

a_lst=[a_coefficient]; %first value can be your curve fitting coefficients from ideal data points

b_lst=[]; %or left blank

for i = 1:rep

xd=X+X_error.*normrnd(0,1,1,length(x));

yd=Y+Y_error.*normrnd(0,1,1,length(y));

d_linear=fitlm(xd,yd);

bd_coefficient = d_linear.Coefficients.Estimate(1);

b_lst=[b_lst,bd_coefficient];

ad_coefficient = d_linear.Coefficients.Estimate(2);

a_lst=[a_lst,ad_coefficient];

i

end

histogram (a_lst,100);

a=mean(a_lst);

a_delta=std(a_lst);

b=mean(b_lst);

b_delta=std(b_lst);

##### 0 Comments

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!