User supplied gradient to lsqcurvefit
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
Accepted Answer
More Answers (1)
Star Strider
on 3 Apr 2016
1 vote
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
Slade Wilson
on 5 Apr 2016
Alan Weiss
on 5 Apr 2016
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
Star Strider
on 5 Apr 2016
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:
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.
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!