# how to minimizing the error and find the constants by fitting the data

66 views (last 30 days)
R7 DR on 25 Feb 2015
Commented: dpb on 28 Feb 2015
I have two data sets, D1 and D2. where D1 and D2 has the experimental and Calculated values. How to fit the data to minimize the error and find the constant values.
D1=[......] %experiemntal
D2=[......] % calculated
X =[......] % dimensions are same as D1&D2
Y =[......] % dimensions are same as D1&D2
D2(A, B, X,Y)= A*exp(B/X)*Y % D2 is a function of A,B,X,Y
How to find the values of A and B by minimizing the error between D1 and D2?
Thanks

dpb on 25 Feb 2015
Edited: dpb on 26 Feb 2015
Presuming have the Stat Toolbox, ninfit makes it pretty simple directly...
If you write
function dhat=model(coef,x)
% some convenient internal shorthand definitions...
a=coef(1);
b=coef(2);
X=x(:,1);
Y=x(:,2);
% predicted model
dhat=a*exp(b./x).*y;
then
coeff=nlinfit([X,Y],D1,@model,coeff0);
where coeff0 is an initial guess/estimate for the coefficients a and b
Alternatively, the above form can be linearized by log transformation into
ln(A) + B/X + ln(Y)
which can be solved by OLS for coefficients in transformed space.

dpb on 27 Feb 2015
NB: in the call of nlinfit the first argument is [X,Y]. Thus they're passed to the function as a single array. Note also in the function definition that at the top for clarity I wrote
function dhat=model(coef,x)
% some convenient internal shorthand definitions...
a=coef(1);
b=coef(2);
X=x(:,1);
Y=x(:,2);
So, yes, the model is correct as you gave it originally and y is in there...
R7 DR on 27 Feb 2015
Thanks a lot...
Its working..
dpb on 28 Feb 2015
Good; figured it would if you'd just go ahead and try it... :)