Lsqcurvefit - problem with matrix input

조회 수: 13 (최근 30일)
Maciej Piejko
Maciej Piejko 2023년 7월 8일
댓글: Star Strider 2023년 7월 10일
Hi everyone,
I'm trying to use lsqcurvefit to optimize one set of data to look as close as another set.
For this purpose I try to optimize the difference between a fixed vector empty(:,2) and another vector, which I know can be quite nicely tuned using the function (var(:,1) - x(1).*var(:,2) + x(2)). So, the Ydata is obviously a matrix and even though I saw examples of functions being fitted using lsqcurvefit with matrices and also the manual says it's possible, I still get an error. The sizes and dimensions of all inputs are correct, so it can't be the problem.
Here's my code:
%Uploading data from other sources:
var(:,1) = abs_full;
var(:,2) = absorbance_molecule;
%Defining the optim. function + fitting
myfun = @(x,var) empty(:,2) - (var(:,1) - x(1).*var(:,2) + x(2));
x0 = [0.53,0.1];
x = lsqcurvefit(mufyn,x0,wav_full,var);
Here's the error:
Index in position 2 exceeds array bounds. Index must not exceed 1.
Error in kinetics_UV_shifting_background>@(x,var)empty(:,2)-(var(:,1)-x(1).*var(:,2)+x(2)) (line 290)
myfun = @(x,var) empty(:,2) - (var(:,1) - x(1).*var(:,2) + x(2));
Error in lsqcurvefit (line 225)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in kinetics_UV_shifting_background (line 293)
x = lsqcurvefit(myfun,x0,wav_full,var);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
I would be grateful for any help!

답변 (1개)

Star Strider
Star Strider 2023년 7월 8일
Index in position 2 exceeds array bounds. Index must not exceed 1.
Check to see how many columns ‘empty’ has. It may only have one.
  댓글 수: 7
Maciej Piejko
Maciej Piejko 2023년 7월 10일
Thank you for all the help, it still did not work but I found a workaround.
Star Strider
Star Strider 2023년 7월 10일
What was the workaround?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by