User supplied gradient to lsqcurvefit

3 次查看(过去 30 天)
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

采纳的回答

Alan Weiss
Alan Weiss 2016-4-5
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 个评论
Slade Wilson
Slade Wilson 2016-4-6
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.
Alan Weiss
Alan Weiss 2016-4-6
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

请先登录,再进行评论。

更多回答(1 个)

Star Strider
Star Strider 2016-4-3
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 个评论
Alan Weiss
Alan Weiss 2016-4-5
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
Star Strider 2016-4-6
@Slade — The current online documentation is for R2016a. If you have an earlier version, 'SpecifyObjectiveGradient' will not apply, since it is new to R2016a.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by