Strange error using lsqcurvefit

1 view (last 30 days)
Gunvor Røkke
Gunvor Røkke on 16 Dec 2015
Commented: Gunvor Røkke on 16 Dec 2015
I am using lsqcurvefit to fit a data set to a linear combination of exponential curves, and I keep getting errors that I don't understand. The data I am going to analyze is in a matrix called Signal. I have 123 data sets that should be analyzed, and each data set has 3000 data points. Signal is thus a 3000x123 matrix. I am using a loop to do the analysis:
for i = 1:size(Signal,2)
t = 0.01:0.01:30;
y = Signal(:,i);
func = @biexpRise;
guess = [0.2, 100, 10, 1, 2];
fit_region = [1, 1.75];
fit_range = find(t == fit_region(1)):find(t == fit_region(2));
opts = optimset('lsqcurvefit');
optimset(opts, 'Display', 'off');
[x, resnorm] = lsqcurvefit(func, guess, t(fit_range), y(fit_range), [], [], opts)
ResRise(i,:) = x;
end
The matrix Signal is attached, if anyone is interested. The function I use (@biexpRise) is in a separate function file, and looks like this:
function y = biexpRise(x, xdata)
y = x(1)*(-exp(-x(2)*(xdata-1))+exp(-x(3)*(xdata-1))) + x(4)*(1-exp(-x(5)*(xdata-1)));
end
When I run this piece of code, it sometimes generates the following error:
Error using feval Undefined function 'biexpRise' for input arguments of type 'double'.
Error in lsqcurvefit (line 199) initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
The strange thing is that it does not always generate this error. Sometimes the script runs just fine, and then suddenly, without changing anything, I will get an error again. Hope someone can help me!

Answers (1)

Steven Lord
Steven Lord on 16 Dec 2015
Is the file in which you've defined your biexpRise function named biexpRise.m? Is the biexpRise function the first (main) function in that file? If the answer to either of those questions is no, the code in this other file will not be able to access biexpRise (at least not under that name, and perhaps not at all without some function handle trickery.)
  1 Comment
Gunvor Røkke
Gunvor Røkke on 16 Dec 2015
Hi! Yes, my biexpRise function is named biexpRise.m, and is located in the same folder as my main script. And the function is also the main function in the file. I have some other functions there too, because I tested out several functions before I found the best one. But I put a percentage sign in front of the others in order to make them comments instead of code, so there is only one active function in the file biexpRise.m

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!