checkGradients, but the objective function has two inputs: x and xdata?

35 次查看(过去 30 天)
I'm using lsqcurvefit with the following objective function and Jacobian:
function [f, jacF] = semiCircle(p, Q)
P0 = p(1);
Q0 = p(2);
r = p(3);
f = P0 + sqrt(r^2 - (Q-Q0).^2);
if nargout > 1 % need Jacobian
jacF = [1, (Q-Q0)./sqrt(r^2-(Q0-Q).^2), r./sqrt(r^2-(Q0-Q).^2)];
end
end
I'd like to use checkGradients to verify if the Jacobian is correct. However, all of the examples in the documentation just have objective functions with one input, the parameters 'x'. Whereas my function semiCircle has two inputs: the parameters 'p' and the xdata 'Q'. Is there a way to use checkGradients for such a function?

采纳的回答

Torsten
Torsten 2024-11-14,10:31
valid = checkGradients(@(p)semiCircle(p, Q),p0)
  6 个评论
Benjamin Pepper
Benjamin Pepper 2024-11-14,14:13
移动:Steven Lord 2024-11-14,14:33
Does it throw an error if you don't check the gradients, but set 'SpecifyObjectiveGradient' to true ?
No, it seems it does not throw an error for this.
Torsten
Torsten 2024-11-14,15:21
That's strange and seems to indicate that "lsqcurvefit" does not use your analytical gradients. Can you supply the full code to test ?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by