lsqcurvefit and anonymous function error
조회 수: 2 (최근 30일)
이전 댓글 표시
I'm trying to use lsqcurvefit with an anonymous function within the script to determine the best fit for a hyperbolic tangent function. However, I keep receiving the following error as outlined by the exception variable:
'Objective function is returning undefined values at initial point. lsqcurvefit cannot continue.'
Here is the code:
% Estimate hyperbolic tangent parameters using least squares curve fit.
xo = [ 15; 20; 15 ]; % initial guess of tangent curve parameters.
lb = [ 16; 0; 0 ]; ub = [ 17; 40; 40 ]; % Bounds for the parameters
d = 0:0.1:65;
hyper = @( c, rkm ) c( 1 ).*( 1 - tanh( ( rkm - c( 2 ) ./ c( 3 ) ) ) );
for n = 1:2%size( sal, 1 )
try
% Fit the hyperbolic tangent parameters using the least squares curve fit.
[ param, resnorm ] = lsqcurvefit( hyper, xo, rkm, sal(n,:), lb, ub );
BSo(n) = param(1);
Bxc(n) = param(2);
BxL(n) = param(3);
catch exception2
continue
end
end
댓글 수: 0
채택된 답변
Star Strider
2014년 2월 17일
You don’t give values for rkm or sal. When I created values for them, I had no problems with the following code (essentially yours, with a couple lines added to create the data):
xo = [ 15; 20; 15 ]; % initial guess of tangent curve parameters.
lb = [ 16; 0; 0 ]; ub = [ 17; 40; 40 ]; % Bounds for the parameters
hyper = @( c, rkm ) c( 1 ).*( 1 - tanh( ( rkm - c( 2 ) ./ c( 3 ) ) ) );
rkm = -10:2:10;
sal = [hyper([16.25 11 37],rkm); hyper([16.75 19 31],rkm)];
d = 0:0.1:65;
hyper = @( c, rkm ) c( 1 ).*( 1 - tanh( ( rkm - c( 2 ) ./ c( 3 ) ) ) );
for n = 1:2 %size( sal, 1 )
try
% Fit the hyperbolic tangent parameters using the least squares curve fit.
[ param, resnorm ] = lsqcurvefit( hyper, xo, rkm, sal(n,:), lb, ub );
BSo(n) = param(1);
Bxc(n) = param(2);
BxL(n) = param(3);
catch exception2
continue
end
end
댓글 수: 2
Star Strider
2014년 2월 18일
편집: Star Strider
2014년 2월 18일
You specified it correctly. I had to generate data to test the rest of your code.
I copied your function and placed it before the lines generating the data because MATLAB has to have the anonymous function defined first, before it is used. (It’s easiest to use your function to generate the data.) I should have listed it instead as:
% GENERATE DATA SET
hyper = @( c, rkm ) c( 1 ).*( 1 - tanh( ( rkm - c( 2 ) ./ c( 3 ) ) ) );
rkm = -10:2:10;
sal = [hyper([16.25 11 37],rkm); hyper([16.75 19 31],rkm)];
% CONTINUE WITH SCRIPT
I apologise for the confusion.
Since I didn’t reproduce your error with my data, I suggest that you temporarily use:
lb = ones(1,3)*-1E8;
ub = ones(1,3)* 1E8;
I do not see anything wrong with your code, so giving lsqcurvefit more generous constraints might allow it to converge. Try different starting values as well, unless you know from prior experiments that the ones you specified as xo are realistic.
It is not uncommon to have to experiment with several different xo vectors before a nonlinear parameter estimation problem such as yours will converge.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!