Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Exponential fit with weighted values

Subject: Exponential fit with weighted values

From: Sébastien MAILFERT

Date: 21 Nov, 2012 07:18:17

Message: 1 of 8

Hi,

I've data that I would like to fit with an exponential function, taking into account the error bars of the data.
Here are these data (xdata, error on xdata, y, error on ydata).
0.1483 0.000867 39.1945 1.8228
0.2168 0.001375 47.7910 1.7398
0.3473 0.002344 56.6020 2.2711
0.1567 0.000929 44.9017 2.0166
0.2703 0.001772 47.8569 1.8924
0.3166 0.002117 59.2465 2.3985
0.0496 0.000134 17.7732 0.9696
0.0732 0.000309 21.6460 0.9208
0.0953 0.000473 25.5399 0.8836
0.1290 0.000723 31.8719 1.4389
0.0570 0.000189 19.3770 0.5574
0.0815 0.000370 23.9318 0.7946
0.1129 0.000604 29.4718 0.8592
0.2012 0.001260 50.5815 2.2698
0.3777 0.002570 55.1597 1.8300

Here is my 2 Matlab codes:
----------------------------------------------------------------------
func=@(p,x) p(1)*(1-exp(-x*1000/(4*p(1)*p(2))));
 init_param=[50 1];
ExpoResult1=lsqcurvefit(func,init_param,xdata,ydata);
----------------------------------------------------------------------

or
----------------------------------------------------------------------
function f=exponential(a,x)
f=a(1)*(1-exp(-x*1000/(4*a(1)*a(2))));
end
ExpoResult2=nlinfit(xdata,ydata,@exponential,[50 1]);
----------------------------------------------------------------------

These cades provide 2 results (close to each other, not exactlu equal)
ExpoResult1: [64.9797 0.6415]
ExpoResult2: [64.9801 0.6415]

My question is: How can I take into account the error bars in ydata (maybe a solution exists for the x values?) with a weighted fit?

Bioth of these two functions (lsqcurvefit and nlinfit) don't provide a possibility to enter the weighted values.
In Octave, you have this function: leasqr
(http://octave.sourceforge.net/optim/function/leasqr.html):
function [f,p,cvg,iter,corp,covp,covr,stdresid,Z,r2]=
leasqr(x,y,pin,F,{stol,niter,wt,dp,dFdp,options})
Where wt are the weighted values in ydata.

Can someone help me please?

Thanks in advance.

Sébastien

Subject: Exponential fit with weighted values

From: Bruno Luong

Date: 21 Nov, 2012 07:29:10

Message: 2 of 8

"Sébastien MAILFERT" wrote in message <k8hv7p$d40$1@newscl01ah.mathworks.com>...
> Hi,
>
> I've data that I would like to fit with an exponential function, taking into account the error bars of the data.
> Here are these data (xdata, error on xdata, y, error on ydata).
> 0.1483 0.000867 39.1945 1.8228
> 0.2168 0.001375 47.7910 1.7398
> 0.3473 0.002344 56.6020 2.2711
> 0.1567 0.000929 44.9017 2.0166
> 0.2703 0.001772 47.8569 1.8924
> 0.3166 0.002117 59.2465 2.3985
> 0.0496 0.000134 17.7732 0.9696
> 0.0732 0.000309 21.6460 0.9208
> 0.0953 0.000473 25.5399 0.8836
> 0.1290 0.000723 31.8719 1.4389
> 0.0570 0.000189 19.3770 0.5574
> 0.0815 0.000370 23.9318 0.7946
> 0.1129 0.000604 29.4718 0.8592
> 0.2012 0.001260 50.5815 2.2698
> 0.3777 0.002570 55.1597 1.8300
>
> Here is my 2 Matlab codes:
> ----------------------------------------------------------------------
> func=@(p,x) p(1)*(1-exp(-x*1000/(4*p(1)*p(2))));
> init_param=[50 1];
> ExpoResult1=lsqcurvefit(func,init_param,xdata,ydata);
> ----------------------------------------------------------------------
>
> or
> ----------------------------------------------------------------------
> function f=exponential(a,x)
> f=a(1)*(1-exp(-x*1000/(4*a(1)*a(2))));
> end
> ExpoResult2=nlinfit(xdata,ydata,@exponential,[50 1]);
> ----------------------------------------------------------------------
>
> These cades provide 2 results (close to each other, not exactlu equal)
> ExpoResult1: [64.9797 0.6415]
> ExpoResult2: [64.9801 0.6415]
>
> My question is: How can I take into account the error bars in ydata (maybe a solution exists for the x values?) with a weighted fit?

Normalize your ydata by the inverse of the weight (or the square-root of it, depending how you define the weight), then feed the lsqcurvefit with normalized data.

Bruno

Subject: Exponential fit with weighted values

From: Sébastien MAILFERT

Date: 21 Nov, 2012 09:31:08

Message: 3 of 8


> Normalize your ydata by the inverse of the weight (or the square-root of it, depending how you define the weight), then feed the lsqcurvefit with normalized data.



Hi,
Thanks for your answer.

is it really equal to normalize my data and to fit my data with a model that takes into account the weight?

Thanks.

Subject: Exponential fit with weighted values

From: Torsten

Date: 21 Nov, 2012 10:14:08

Message: 4 of 8

"Sébastien MAILFERT" wrote in message <k8hv7p$d40$1@newscl01ah.mathworks.com>...
> Hi,
>
> I've data that I would like to fit with an exponential function, taking into account the error bars of the data.
> Here are these data (xdata, error on xdata, y, error on ydata).
> 0.1483 0.000867 39.1945 1.8228
> 0.2168 0.001375 47.7910 1.7398
> 0.3473 0.002344 56.6020 2.2711
> 0.1567 0.000929 44.9017 2.0166
> 0.2703 0.001772 47.8569 1.8924
> 0.3166 0.002117 59.2465 2.3985
> 0.0496 0.000134 17.7732 0.9696
> 0.0732 0.000309 21.6460 0.9208
> 0.0953 0.000473 25.5399 0.8836
> 0.1290 0.000723 31.8719 1.4389
> 0.0570 0.000189 19.3770 0.5574
> 0.0815 0.000370 23.9318 0.7946
> 0.1129 0.000604 29.4718 0.8592
> 0.2012 0.001260 50.5815 2.2698
> 0.3777 0.002570 55.1597 1.8300
>
> Here is my 2 Matlab codes:
> ----------------------------------------------------------------------
> func=@(p,x) p(1)*(1-exp(-x*1000/(4*p(1)*p(2))));
> init_param=[50 1];
> ExpoResult1=lsqcurvefit(func,init_param,xdata,ydata);
> ----------------------------------------------------------------------
>
> or
> ----------------------------------------------------------------------
> function f=exponential(a,x)
> f=a(1)*(1-exp(-x*1000/(4*a(1)*a(2))));
> end
> ExpoResult2=nlinfit(xdata,ydata,@exponential,[50 1]);
> ----------------------------------------------------------------------
>
> These cades provide 2 results (close to each other, not exactlu equal)
> ExpoResult1: [64.9797 0.6415]
> ExpoResult2: [64.9801 0.6415]
>
> My question is: How can I take into account the error bars in ydata (maybe a solution exists for the x values?) with a weighted fit?
>
> Bioth of these two functions (lsqcurvefit and nlinfit) don't provide a possibility to enter the weighted values.
> In Octave, you have this function: leasqr
> (http://octave.sourceforge.net/optim/function/leasqr.html):
> function [f,p,cvg,iter,corp,covp,covr,stdresid,Z,r2]=
> leasqr(x,y,pin,F,{stol,niter,wt,dp,dFdp,options})
> Where wt are the weighted values in ydata.
>
> Can someone help me please?
>
> Thanks in advance.
>
> Sébastien

The usual way is to use lsqnonlin with the functions f_i defined as
f_i = (y_data(i)-y_exponential(i))/sigma(i)
where sigma(i) is the standard deviation of the i-th measurement.

Best wishes
Torsten.

Subject: Exponential fit with weighted values

From: Bruno Luong

Date: 21 Nov, 2012 13:21:06

Message: 5 of 8

"Torsten" wrote in message <k8i9hg$g6e$1@newscl01ah.mathworks.com>...

>
> The usual way is to use lsqnonlin with the functions f_i defined as
> f_i = (y_data(i)-y_exponential(i))/sigma(i)
> where sigma(i) is the standard deviation of the i-th measurement.
>
> Best wishes
> Torsten.

I would say the correct normalization is:
f_i = (y_data(i)-y_exponential(i))/sqrt(sigma(i))

Bruno

Subject: Exponential fit with weighted values

From: Torsten

Date: 21 Nov, 2012 14:18:08

Message: 6 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k8ikg2$li5$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <k8i9hg$g6e$1@newscl01ah.mathworks.com>...
>
> >
> > The usual way is to use lsqnonlin with the functions f_i defined as
> > f_i = (y_data(i)-y_exponential(i))/sigma(i)
> > where sigma(i) is the standard deviation of the i-th measurement.
> >
> > Best wishes
> > Torsten.
>
> I would say the correct normalization is:
> f_i = (y_data(i)-y_exponential(i))/sqrt(sigma(i))
>
> Bruno

In my notation, sigma is the standard deviation, not the variance.

I refer to
http://en.wikipedia.org/wiki/Least_squares#Weighted_least_squares
for more details.

Best wishes
Torsten.

Subject: Exponential fit with weighted values

From: Bruno Luong

Date: 21 Nov, 2012 18:24:10

Message: 7 of 8

"Torsten" wrote in message <k8inr0$4ft$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k8ikg2$li5$1@newscl01ah.mathworks.com>...
> > "Torsten" wrote in message <k8i9hg$g6e$1@newscl01ah.mathworks.com>...
> >
> > >
> > > The usual way is to use lsqnonlin with the functions f_i defined as
> > > f_i = (y_data(i)-y_exponential(i))/sigma(i)
> > > where sigma(i) is the standard deviation of the i-th measurement.
> > >
> > > Best wishes
> > > Torsten.
> >
> > I would say the correct normalization is:
> > f_i = (y_data(i)-y_exponential(i))/sqrt(sigma(i))
> >
> > Bruno
>
> In my notation, sigma is the standard deviation, not the variance.

I use the same notation (only sqrt of standard deviation makes sense)

>
> I refer to
> http://en.wikipedia.org/wiki/Least_squares#Weighted_least_squares
> for more details.

Either the inverse of the standard-deviation is in the least squares function;

J(x) = (ymodel - ydata)' * inv(Sigma) * (ymodel - ydata)'.

which is equivalent to scaling the data by the variance.

J(x) = | (y_data(i)-y_model(i))/sqrt(sigma(i)) |^2 (assuming Sigma is diagonal).

Bruno

Subject: Exponential fit with weighted values

From: Torsten

Date: 22 Nov, 2012 07:38:14

Message: 8 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k8j68a$smh$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <k8inr0$4ft$1@newscl01ah.mathworks.com>...
> > "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k8ikg2$li5$1@newscl01ah.mathworks.com>...
> > > "Torsten" wrote in message <k8i9hg$g6e$1@newscl01ah.mathworks.com>...
> > >
> > > >
> > > > The usual way is to use lsqnonlin with the functions f_i defined as
> > > > f_i = (y_data(i)-y_exponential(i))/sigma(i)
> > > > where sigma(i) is the standard deviation of the i-th measurement.
> > > >
> > > > Best wishes
> > > > Torsten.
> > >
> > > I would say the correct normalization is:
> > > f_i = (y_data(i)-y_exponential(i))/sqrt(sigma(i))
> > >
> > > Bruno
> >
> > In my notation, sigma is the standard deviation, not the variance.
>
> I use the same notation (only sqrt of standard deviation makes sense)
>
> >
> > I refer to
> > http://en.wikipedia.org/wiki/Least_squares#Weighted_least_squares
> > for more details.
>
> Either the inverse of the standard-deviation is in the least squares function;
>
> J(x) = (ymodel - ydata)' * inv(Sigma) * (ymodel - ydata)'.
>
> which is equivalent to scaling the data by the variance.
>
> J(x) = | (y_data(i)-y_model(i))/sqrt(sigma(i)) |^2 (assuming Sigma is diagonal).
>
> Bruno

Sorry to insist here, but so far I only found that the inverse of the
variance is in the east-squares function
J(x) = (ymodel - ydata)' * inv(Sigma^2) * (ymodel - ydata)
which is equivalent to scaling the data by the standard deviation
J(x) = || (ymodel(i)-ydata(i))/sigma(i) ||^2
(assuming the covariance matrix is diagonal).

Torsten.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us