function [A, fz] = calculate_derivatives(f,v) % [A fz] = calculate_derivatives(f,v) % % calculates the derivatives of a vector function f(x) at the point x = v. % % f is the function handle % v is the input vector % % Output: % A is the array of partial derivatives a(i,j) = partial f(i) / partial v(j) % fz is the vector returned by fz = f(v) % Written by: John S. Loomis % Revised: 6 Feb 2003 fz = f(v); nv = length(v); nd = length(fz); delta = 1e-5; A = zeros(nd,nv); for j=1:nv vsave = v(j); v(j) = vsave+delta; fp = f(v); v(j) = vsave-delta; fn = f(v); A(:,j)=(fp-fn)/(2*delta); v(j)=vsave; end