lsqcurvefit and anonymous function error
1 次查看(过去 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 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!