User supplied gradient to lsqcurvefit

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

 Accepted Answer

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

Hi Alan,
Thank you very much for your answer. I modified my code with:
options = optimset('Jacobian','on','SpecifyObjectiveGradient','true', 'Algorithm', 'levenberg-marquardt', 'Display','off', 'TolCon', 1e-4, 'MaxIter',1e3,'MaxFunEvals',1e3,'FunValCheck','on', 'TolFun', 1e-4, 'TolX', 1e-4);
and in my fithboth2.m file I added:
J = zeros(length(X));
Re = x(1);
Ri = x(2);
C = x(3);
if nargout > 1
J(1) = -(1 + 1i * omega * Ri * C).^2 ./ (1 + 1i * omega * (Re + Ri) * C).^2;
J(2) = (omega.^2 * Re^2 * C^2) ./ (1 + 1i * omega* (Re + Ri) * C).^2;
J(3) = 1i * omega * Re^2 ./ (1 + 1i * omega * (Re + Ri) * C).^2;
end
but after I run my new code, I have error:
Error using optimset (line 203)
Unrecognized parameter name 'SpecifyObjectiveGradient'. Please see the optimset reference
page in the documentation for a list of acceptable option parameters. Link to reference page.
Error in journal_april_2016_2R1C (line 79)
options = optimset('Jacobian','on','SpecifyObjectiveGradient','true', 'Algorithm',
'levenberg-marquardt', 'Display','off', 'TolCon', 1e-4,
'MaxIter',1e3,'MaxFunEvals',1e3,'FunValCheck','on', 'TolFun'
Any suggestions? Thanks in advance.
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

Sign in to comment.

More Answers (1)

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

Okay, thank you very much.
You certainly can set a Jacobian. The reason Star Strider was confused is that the name changed in the latest documentation. See my answer for details.
Alan Weiss
MATLAB mathematical toolbox documentation
That was buried deep in the Release notes. I didn’t see it when I first looked through them. And now there are two ways to set option structures as well.
It wasn’t in the function documentation. Why rename it anyway?
From the R2016a Release Notes:
  • Current and Legacy Option Name Tables
  • Many option names changed in R2016a. optimset uses only legacy option names. optimoptions accepts both legacy and current names. However, when you set an option using a legacy name-value pair, optimoptions displays the current equivalent value. For example, the legacy TolX option is equivalent to the current StepTolerance option:
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
@Slade — The current online documentation is for R2016a. If you have an earlier version, 'SpecifyObjectiveGradient' will not apply, since it is new to R2016a.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!