Skip to content

Instantly share code, notes, and snippets.

@matthieuheitz
Last active February 2, 2017 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthieuheitz/c79300ab99de832e7549640a2a77848e to your computer and use it in GitHub Desktop.
Save matthieuheitz/c79300ab99de832e7549640a2a77848e to your computer and use it in GitHub Desktop.
Compute numerical derivatives on Matlab
% Jacobian functor
J = @(x,h,F)(F(repmat(x,size(x'))+diag(h))-F(repmat(x,size(x'))))./h';
% Function to differentiate
T = @(x) x./sum(x);
% Evaluation point
x = [0.9134;0.6324;0.0975;0.2785;0.5469];
% Step in each dimension (has to be small enough compared to x)
h = 1e-5*ones(size(x));
% Compute the Jacobian
J(x,h,T)
% Other method to compare the result (need Optimization Toolbox)
[~,~,~,~,~,~,jacobian] = lsqnonlin(T,x,[],[],optimset('MaxFunEvals',0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment