-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathComplexStepGradients.m
40 lines (35 loc) · 1.08 KB
/
ComplexStepGradients.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
% ComplexStepGradients.m: Computes the gradient of a function at a given
% point using the complex step method
%
% Ross Allem, ASL, Stanford University
% Based on work by: Michael Colonno, ADL, Stanford University
%
% Started: 12/1/2013
%
% Inputs: f function for gradient evaluation
% x point of gradient evaluation
% dimf dimension of output of f
% params additional params needed to comput f(x) optional
%
% Outputs: gradf gradient of f at x
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function gradf = ComplexStepGradients(f,x,dimf,params)
dimx = length(x);
gradf = zeros(dimx,dimf);
h = 1e-20;
x0 = x;
if nargin == 4
for n = 1:dimx
x = x0;
x(n) = x(n) + h*1i;
gradf(n,:) = imag(f(x,params))./h;
end
elseif nargin == 3
for n = 1:dimx
x = x0;
x(n) = x(n) + h*1i;
gradf(n,:) = imag(f(x))./h;
end
end
return;