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

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

 Respuesta aceptada

dpb
dpb el 25 de Feb. de 2015
Editada: dpb el 26 de Feb. de 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.

6 comentarios

Thanks for the reply.
I enetred the initial guesses like this
coeff0=[15000 20]
coeff=nlinfit([X,Y],@model,coeff0);
but it showing the error as...
Error using nlinfit (line 183)
NLINFIT requires four input arguments.
Error in isothermalfitting (line 13)
coeff=nlinfit([X,Y],@model,coeff0);
In your code where we are using D1?
My aim is to minimize the sum of the errors by fitting the 'a'& 'b' .
S_error=[D1-D2]^2...should be minimum.
dpb
dpb el 26 de Feb. de 2015
Editada: dpb el 26 de Feb. de 2015
My bad; I see for some reason I left out the observations vector...
coeff=nlinfit([X,Y],D1,@model,coeff0);
D2 is implicit in the model evaluation; internally it will compute the model at the input points and minimize the sum of squares. Don't have to manually compute them in the Matlab implementation, that's done automagically.
I fixed up the oversight in the Answer section...I think as I was typing when I got the Y in the independent variables array I unconsciously was thinking of it as being the response variable in a classroom y=f(x) vein...
R7 DR
R7 DR el 27 de Feb. de 2015
Editada: R7 DR el 27 de Feb. de 2015
Hi
Is this''dhat=a*exp(b./x).*y;'' format is correct..I didnt get this equation.
Is it not required not to use y in this function? function dhat=model(coef,x)
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...
Thanks a lot...
Its working..
Good; figured it would if you'd just go ahead and try it... :)

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 25 de Feb. de 2015

Comentada:

dpb
el 28 de Feb. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by