How do I solve partitioned or separable least squares problems using the LSQCURVEFIT function within the Optimization Toolbox?

3 views (last 30 days)
I would like to solve partitioned or separable least squares problems using the LSQCURVEFIT function within the Optimization Toolbox.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
For partitioned or separable least squares problems, the equation has two parts; the linear part ("A" and "B") and a nonlinear part ("C").
Sometimes fitting the linear term(s) separately gives better results.
Below is an example of how to solve a separable least squares problem using the LSQCURVEFIT function in the Optimization Toolbox. This example finds solutions with the separable least squares method (SEP_FIT_SIMP) and without the separable least squares method (FIT_SIMP) and then compares the results.
The difference between SEP_FIT_SIMP and FIT_SIMP is the way the functions are parameterized. FIT_SIMP is a function of three parameters: A, B, and C. SEP_FIT_SIMP is a function of only 1 parameter: C. During the optimization routine, LSQCURVEFIT optimizes FIT_SIMP around all three parameters, but it optimizes SEP_FIT_SIMP around only one parameter. The backslash operator ('\') is used to find optimal values of A and B.
Note that the second output is of SEP_FIT_SIMP is necessary because that is the way of obtaining the parameters A and B when using the SEP_FIT_SIMP objective function. Once the optimized value of C is found, SEP_FIT_SIMP is called again in the line "[Y_new2,lincoeff] = sep_fit_simp(C,XDATA,YDATA); " to get the values A and B that correspond to that optimized C.
Note that besides performing separable least squares, you also have to be careful when selecting your starting point. If you run the files below starting with "C = 1" (rather than "C = 0"), then the linear system is extremely ill-conditioned.
To run the example, tirst, create the following MATLAB functions:
function diff = fit_simp(x,XDATA)
% This function is called by lsqcurvefit.
% x is a vector which contains the coefficients of the
% equation. XDATA is the data set
% passed to lscurvefit.
A = x(1);
B = x(2);
C = x(3);
diff = A + B.*exp(C.*XDATA);
function [diff,lincoeff] = sep_fit_simp(x,XDATA,YDATA)
% This function is called by. lsqcurvefit
% x is a vector which contains the coefficients of the
% equation. XDATA is the data set
% passed to lsqcurvefit.
C=x;
% Separately solve for the linear coefficients
systemeq =[ones(length(XDATA),1) exp(C*XDATA(:))];
lincoeff = systemeq\YDATA(:);
A=lincoeff(1);
B=lincoeff(2);
diff = A + B.*exp(C.*XDATA);
Now, execute the following commands at the command line:
XDATA = [15.3400 21.1400 30.4400 44.8900 65.5800 91.6800]/5;
YDATA =[90.0000 59.1000 36.2000 19.8000 9.0000 3.1000]/5;
opt = optimset('Display','iter','large','off');
warning off
x = lsqcurvefit('fit_simp',[2;3;4],XDATA, YDATA, [],[],opt);
Y_new1 = fit_simp(x,XDATA);
C = lsqcurvefit('sep_fit_simp',1,XDATA,YDATA,[],[],opt,YDATA);
[Y_new2,lincoeff] = sep_fit_simp(C,XDATA,YDATA);
semilogy(XDATA,YDATA,'+r',XDATA,Y_new1,'b',XDATA,Y_new2,'g')
legend('data','ordinary fit','separable fit')

More Answers (0)

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!