lsqcurvefit and anonymous function error

2 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 17 Feb 2014
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 Comments
curoi
curoi on 17 Feb 2014
Star Strider,
I actually do have datasets for rkm and sal. Sal is a matrix that has size numel( rkm ) by numel (time) and stands for salinity; and rkm is river kilometer. However, these are specified before I generated the anonymous hyperbolic tangent function. Must the datasets be specified after the hyperbolic tangent function? It also looks like you specified the anonymous function twice, so I'm not sure where to place it in relation to the datasets.
Star Strider
Star Strider on 18 Feb 2014
Edited: Star Strider on 18 Feb 2014
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!