How to perform non linear least square regression of eigenvalues of a matrix (matrix elements being some function of independent variable and parameters to be optimized)?

1 view (last 30 days)
Hi, I am a beginner in MATLAB and struggling with this one code. I have a (4x4) matrix whose elements are function of independent variable and parameters (say a,b,c are parameters) and x is independent variable. e.g. M= [a+b*exp(x*b),c*b*exp(x*c)....;c+a*exp(x*b)....] the eigenvalues of this matrix are y(dependent variables) I want to fit my data to.
I have the data set Xexp=[x1,x2,x3......] and Yexp=[y1,y2....]
I am trying to fit the data set (X,Y) to the eigenvalue "y" (say 2nd eigenvalue) of the matrix (M) using "lsqnonlin" where at the end of the function program I calculate "Y-y" so as to be minimized by the lsqnonlin e.g.
phiLSQ= lsqnonlin(@(phi) lambdafn(phi), initial_param); %command to start regression in main program
function LErr = lambdafn(phi) %start of the function
I am providing the data set (X,Y) in function program itself.
when I run the script I get following error
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in lambdafn (line 31)
Q=[a+b*exp(x*b),c*b*exp(x*c)....;c+a*exp(x*b)....] ; %the complete matrix
Error in @(phi)lambdafn(phi)
Error in lsqnonlin (line 194)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in lambda (line 32)
phiLSQ= lsqnonlin(@(phi) lambdafn(phi), initial_param);
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.
Thanks

Accepted Answer

Matt J
Matt J on 12 Jan 2017
Edited: Matt J on 12 Jan 2017
You should test lambdafn(phi) before you feed it to lsqnonlin to make sure it works, i.e., that it always runs without error messages and returns a finite, real value. LSQNONLIN is complaining that it doesn't.
  7 Comments
Vipultomar
Vipultomar on 13 Jan 2017
As I am giving as input the entire row of xdata, the program is trying to calculate the matrix elements first, and then eigenvalues to be given as output (y) for minimization. The problem I think is that the code is taking the entire row of xdata at once and then processing according to..
Q=[a+b*exp(x*b),c*b*exp(x*c)....;c+a*exp(x*b)....]
after which I calculate my eigencalues. because each element of matrix is being generated for each xdata value, there is problem with concatenation. On the other hand if I try to take just one element of matrix as my function operator (just to check) or just one value of xdata, it works fine.
Vipultomar
Vipultomar on 13 Jan 2017
Edited: Vipultomar on 13 Jan 2017
As I mentioned the program was not taking complete xdata row, I have started a for loop in the function itself. In this way the function calculates the value of y from the model (function) and keeps putting them in a row vector. Finally the entire row vector is output of the function (for lsqcurvefit). And so its working now. Although there is no change in parameter values with each step (which I think should be unrelated to it) so trying to correct that.
Thanks

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!