Lsqcurvefit - problem with matrix input
显示 更早的评论
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
2023-7-8
0 个投票
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
2023-7-9
编辑:Maciej Piejko
2023-7-9
Maciej Piejko
2023-7-9
编辑:Maciej Piejko
2023-7-9
Star Strider
2023-7-9
编辑:Star Strider
2023-7-9
Having all the data and all the necessary code to work with would definitely help.
EDIT — (9 Jul 2023 at 13:44)
You are creating the ‘var’ matrix, so I doubt that it is the problem. The problem appears to be the ‘empty’ variable, since it appears to have only one column. That throws the error.
.
Maciej Piejko
2023-7-9
Star Strider
2023-7-9
You define ‘empty’ here:
empty = final(:,2);
so it only has one column (as I suspected).
Just use:
myfun = @(x,var) empty - (var(:,1) - x(1).*var(:,2) + x(2));
instead, without the subscript reference to ‘empty’, although this:
myfun = @(x,var) empty(:,1) - (var(:,1) - x(1).*var(:,2) + x(2));
should also work.
I would replace the dlmread calls with readmatrix however without the data and the rest of the code, I cannot determine what any remaining problems are, if any remain, or obviously how to fix them.
Maciej Piejko
2023-7-10
Star Strider
2023-7-10
What was the workaround?
类别
在 帮助中心 和 File 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!