User supplied gradient to lsqcurvefit
2 views (last 30 days)
Show older comments
Hello,
I would like to fit measurement data to Frcike-Morse model of biological tissues. Model consists of three parameters: Re, Ri and Cm. Up to now I have good parameter estimation with following code:
options = optimset('Algorithm', 'levenberg-marquardt', 'Display','off', 'TolCon', 1e-4, 'MaxIter',1e3,'MaxFunEvals',1e3,'FunValCheck','on', 'TolFun', 1e-4, 'TolX', 1e-4);
Y(1:N) = ReZ';
Y(N+1:2*N) = ImZ';
[x,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(@fit_both2, X0, 2*pi*f, Y, [], [], options);
In separated file fit_both2.m I have following code:
function F = fit_both2(x,X)
%x(1) = Re
%x(2) = Ri
%x(3) = Cm
%2R-1C model
f = x(1) * (1 + 1i * X * x(2) * x(3)) ./ (1 + 1i * X * (x(1) + x(2)) *x(3));
F(1:length(X)) = real(f); % Real part
F(length(X)+1:2*length(X)) = imag(f); % Imaginary part
end
Instead of numerically calculated gradient I would like to supply mine, just see if estimation will be better. How to add this?
Slade
0 Comments
Accepted Answer
Alan Weiss
on 5 Apr 2016
Set the Jacobian option to 'on', and ensure that your objective function returns the Jacobian in the second output. For an example, see Least Squares With and Without a Jacobian.
In R2016a and later, set the SpecifyObjectiveGradient option to true.
Alan Weiss
MATLAB mathematical toolbox documentation
2 Comments
Alan Weiss
on 6 Apr 2016
I tried to be explicit, but I guess I was unclear. Sorry.
You undoubtedly have a MATLAB version earlier than R2016a. Do not use 'SpecifyObjectiveGradient' with any MATLAB version earlier than R2016a. Instead, use 'Jacobian'. I mean, remove the following from your option setting:
'SpecifyObjectiveGradient','true',
leaving just
options = optimset('Jacobian','on',...
'Algorithm', 'levenberg-marquardt', 'Display','off',...
'TolCon',1e-4, 'MaxIter',1e3,...
'MaxFunEvals',1e3,'FunValCheck','on',...
'TolFun', 1e-4, 'TolX', 1e-4);
You are certainly not the only person to have trouble with this change in option names. I am now working on cleaning up the mess that I helped create in the option name changes.
Alan Weiss
MATLAB mathematical toolbox documentation
More Answers (1)
Star Strider
on 3 Apr 2016
Apparently, that’s not possible with either lsqcurvefit (or lsqnonlin), and it also doesn’t seem to be an option in nlinfit, at least in R2016a. I don’t use it often, so I’m not certain when that option disappeared.
In my experience, when supplying an analytic Jacobian was an option, it didn’t affect accuracy but it did provide a speed increase.
5 Comments
Alan Weiss
on 5 Apr 2016
I am sorrier than you can know that you didn't find this information more easily. I tried to highlight it in the release notes by putting it as the first item, and explaining that the old names will continue to work. If you, an active and long-time MATLAB afficionado have trouble understanding what happened, then I certainly failed to explain things clearly.
I hope that, now that you have seen the information, you won't find it difficult to use, and might even come to appreciate the new consistency across solvers.
Alan Weiss
MATLAB mathematical toolbox documentation
Star Strider
on 6 Apr 2016
@Slade — The current online documentation is for R2016a. If you have an earlier version, 'SpecifyObjectiveGradient' will not apply, since it is new to R2016a.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!