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

8 views (last 30 days)
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:

Answers (1)

Alan Weiss
Alan Weiss on 20 Apr 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 Comments
Mahmoud
Mahmoud on 21 Apr 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 on 21 Apr 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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!