How can I Optimize a multivariable function with Least Square method?

8 visualizaciones (últimos 30 días)
Mahmoud
Mahmoud el 19 de Abr. de 2015
Comentada: Alan Weiss el 21 de Abr. de 2015
Hi
I have a function to measure chloride content in concrete:
cxt =cs-(cs-ci)*erf((x*0.001)/(sqrt(4*da*t))
where cxt is chloride content.ci is a constant, x is a vector for depth containing 11 values,and t is a constant.
Through testing I have collected some data for cxt. I want to know what values of cs and da minimize the sum of least sqaures so this function can fit my collected lab data.
More info:
this is the code I have written to get cxt for some arbitrary values for cs and da but I dont know how to find the optimal values:

Respuestas (1)

Alan Weiss
Alan Weiss el 20 de Abr. de 2015
There are several MATLAB functions for nonlinear data fitting, such as lsqcurvefit from Optimization Toolbox and nlinfit from Statistics and Machine Learning Toolbox.
If you do not have these toolboxes, try fminsearch, following this example.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 comentarios
Mahmoud
Mahmoud el 21 de Abr. de 2015
Hi Alan, Thank you for your help. here is what I did:
if true
% function [estimates, model] = fitcurvedemo(xdata,ci,t,ydata)
% Call fminsearch with a random starting point.
start_point = [3e-12, 0.1];
model = @expfun;
estimates = fminsearch(model, start_point);
% expfun accepts curve parameters as inputs, and outputs sse,
% the sum of squares error for A*exp(-lambda*xdata)-ydata,
% and the FittedCurve. FMINSEARCH only needs sse, but we want
% to plot the FittedCurve at the end.
function [sse, FittedCurve] = expfun(params)
da = params(1);
cs = params(2);
%FittedCurve = A .* exp(-lambda * xdata);
FittedCurve=(cs-(cs-ci)).*erf((xdata*0.001)./(sqrt(4*da*t)));
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
end
So I get some inputs by:
if true
% xdata = [2,5:2:23]';
%lab result
ydata =xlsread('clm.xlsx');
ci=repmat(0.050147511,11,1);
t=repmat(15120000,11,1);
%To fit my function to the data,
[estimates, model] = fitcurvedemo(ci,xdata,t,ydata);
end
but I get these errors :
Error using erf Input must be real and full.
Error in fitcurvedemo/expfun (line 14) FittedCurve=(cs-(cs-ci)).*erf((xdata*0.001)./(sqrt(4*da*t)));
Error in fminsearch (line 309) x(:) = xr; fxr = funfcn(x,varargin{:});
Error in fitcurvedemo (line 5) estimates = fminsearch(model, start_point);
I checked the erf function and it works fine seperately but don't know what happens to it here.
any insights?
thanks
Alan Weiss
Alan Weiss el 21 de Abr. de 2015
I suggest that you learn how to set breakpoints and use the debugger to see what is going on.
Alan Weiss
MATLAB mathematical toolbox documentation

Iniciar sesión para comentar.

Categorías

Más información sobre Nonlinear Optimization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by