Skip to content

Commit c1c7e63

Browse files
committed
backup
1 parent c4b3379 commit c1c7e63

38 files changed

+1434
-89
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ doc_NDT tech
3434
`g = igradient(f,x0)`\
3535
`Dv = idirectional(f,x0,v)`\
3636
`J = ijacobian(f,x0)`\
37-
`H = ihessian(f,x0)`
37+
`H = ihessian(f,x0)`\
38+
`H = ivechessian(f,x0)`
3839

3940

4041
## "Complexified" Functions
@@ -54,7 +55,8 @@ doc_NDT tech
5455
`g = cgradient(f,x0)`\
5556
`Dv = cdirectional(f,x0,v)`\
5657
`J = cjacobian(f,x0)`\
57-
`H = chessian(f,x0)`
58+
`H = chessian(f,x0)`\
59+
`H = cvechessian(f,x0)`
5860

5961
## Forward Difference Differentiation Functions
6062

@@ -63,4 +65,5 @@ doc_NDT tech
6365
`g = fgradient(f,x0)`\
6466
`Dv = fdirectional(f,x0,v)`\
6567
`J = fjacobian(f,x0)`\
66-
`H = fhessian(f,x0)`
68+
`H = fhessian(f,x0)`\
69+
`H = fvechessian(f,x0)`

docs/Differentiator_doc.html

+584
Large diffs are not rendered by default.
Loading
Loading
Loading
929 Bytes
Loading
Loading
Loading
Loading
762 Bytes
Loading
Loading
941 Bytes
Loading
850 Bytes
Loading
Loading
Loading

docs/index.html

+5-3
Large diffs are not rendered by default.

docs/thumbnail_resized.png

0 Bytes
Loading

toolbox/Differentiator.m

+96-76
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
% Differentiator Class defining a differentiator.
44
%
55
% Copyright © 2022 Tamas Kis
6-
% Last Update: 2022-08-31
6+
% Last Update: 2022-09-10
77
% Website: https://tamaskis.github.io
88
99
%
@@ -15,39 +15,35 @@
1515
%
1616
%==========================================================================
1717

18-
classdef Differentiator
18+
classdef Differentiator < handle
1919

2020
% -----------
2121
% Properties.
2222
% -----------
2323

2424
properties
25-
method % (char) differentiation method
26-
hi % (1×1 double) step size for complex-step approximation
27-
hc % (1×1 double) relative step size for central difference approximation
28-
hf % (1×1 double) relative step size for forward difference approximation
29-
hf2 % (1×1 double) relative step size for calculating Hessian using forward difference approximation
3025
derivative % (1×1 function_handle) derivative of a univariate, vector-valued function
3126
partial % (1×1 function_handle) partial derivative of a multivariate, vector-valued function
3227
gradient % (1×1 function_handle) gradient of a multivariate, scalar-valued function
3328
directional % (1×1 function_handle) directional derivative of a multivariate, scalar-valued function
3429
jacobian % (1×1 function_handle) Jacobian of a multivariate, vector-valued function
3530
hessian % (1×1 function_handle) Hessian of a multivariate, scalar-valued function
3631
vechessian % (1×1 function_handle) vector Hessian of a multivariate, vector-valued function
32+
method % (char) differentiation method
33+
hi % (1×1 double) step size for complex-step approximation (defaults to 10⁻²⁰⁰)
34+
hc % (1×1 double) relative step size for central difference approximation (defaults to ε¹ᐟ³)
35+
hf % (1×1 double) relative step size for forward difference approximation (defaults to √ε)
36+
hf2 % (1×1 double) relative step size for calculating Hessian using forward difference approximation (defaults to ε¹ᐟ³)
3737
end
3838

39-
% --------
40-
% Methods.
41-
% --------
39+
% ---------------
40+
% Public methods.
41+
% ---------------
4242

43-
methods
43+
methods (Access = public)
4444

45-
% ------------
46-
% Constructor.
47-
% ------------
48-
49-
function obj = Differentiator(method,hi,hc,hf,hf2)
50-
% obj = Differentiator(method,hi,hc,hf,hf2)
45+
function obj = Differentiator(method)
46+
% obj = Differentiator(method)
5147
%
5248
% Constructor.
5349
%--------------------------------------------------------------
@@ -59,16 +55,6 @@
5955
% 'central difference', 'complex-step', or
6056
% 'forward difference' (defaults to
6157
% 'central difference')
62-
% hi - (OPTIONAL) (1×1 double) step size for
63-
% complex-step approximation (defaults to 10⁻²⁰⁰)
64-
% hc - (OPTIONAL) (1×1 double) relative step size for
65-
% central difference approximation (defaults to
66-
% ε¹ᐟ³)
67-
% hf - (OPTIONAL) (1×1 double) relative step size for
68-
% forward difference approximation (defaults to √ε)
69-
% hf2 - (OPTIONAL) (1×1 double) relative step size for
70-
% calculating Hessian using forward difference
71-
% approximation (defaults to ε¹ᐟ³)
7258
%
7359
% -------
7460
% OUTPUT:
@@ -78,65 +64,99 @@
7864
%--------------------------------------------------------------
7965

8066
% defaults "method" to 'central difference' if not input
81-
if (nargin < 1) || isempty(method)
82-
method = 'central difference';
67+
if (nargin == 0) || isempty(method)
68+
obj.method = 'central difference';
69+
else
70+
obj.method = method;
8371
end
8472

85-
% defaults "hi" to 10⁻²⁰⁰ if not input
86-
if (nargin < 2) || isempty(hi)
87-
hi = 1e-200;
88-
end
73+
% default step sizes
74+
obj.hc = eps^(1/3);
75+
obj.hi = 1e-200;
76+
obj.hf = sqrt(eps);
77+
obj.hf2 = eps^(1/3);
8978

90-
% defaults "hc" to ε¹ᐟ³ if not input
91-
if nargin == 2 || isempty(hc)
92-
hc = eps^(1/3);
93-
end
79+
% sets up differentiation functions
80+
obj.set_differentiation_functions;
9481

95-
% defaults "hf" to √ε if not input
96-
if nargin == 2 || isempty(hf)
97-
hf = sqrt(eps);
98-
end
82+
end
83+
84+
function obj = set_step_size(obj,h)
85+
% Differentiator.set_step_size(h)
86+
%
87+
% Sets the step size.
88+
%--------------------------------------------------------------
89+
%
90+
% ------
91+
% INPUT:
92+
% ------
93+
% h - (1×1 double) step size (for complex-step
94+
% approximation) OR relative step size (for central
95+
% and forward difference approximations)
96+
%
97+
%--------------------------------------------------------------
9998

100-
% defaults "hf2" to ε¹ᐟ³ if not input
101-
if nargin == 2 || isempty(hf2)
102-
hf2 = eps^(1/3);
99+
% step size for complex-step approximation
100+
if strcmpi(obj.method,'complex-step')
101+
obj.hi = h;
102+
103+
% relative step size for central difference approximation
104+
elseif strcmpi(obj.method,'central difference')
105+
obj.hc = h;
106+
107+
% relative step sizes for forward-difference approximation
108+
else
109+
obj.hf = h;
110+
obj.hf2 = h;
111+
103112
end
104113

114+
% updates differentiation functions
115+
obj.set_differentiation_functions;
116+
117+
end
118+
119+
end
120+
121+
% ---------------
122+
% Private methods.
123+
% ---------------
124+
125+
methods (Access = private)
126+
127+
function obj = set_differentiation_functions(obj)
128+
% Differentiator.set_differentiation_functions
129+
%
130+
% Sets up the differentiation functions.
131+
%--------------------------------------------------------------
132+
105133
% sets up differentiation functions
106-
if strcmpi(method,'central difference')
107-
obj.derivative = @(f,x) cderivative(f,x,hc);
108-
obj.partial = @(f,x,k) cpartial(f,x,k,hc);
109-
obj.gradient = @(f,x) cgradient(f,x,hc);
110-
obj.directional = @(f,x,v) cdirectional(f,x,v,hc);
111-
obj.jacobian = @(f,x) cjacobian(f,x,hc);
112-
obj.hessian = @(f,x) chessian(f,x,hc);
113-
obj.vechessian = @(f,x) cvechessian(f,x,hc);
114-
elseif strcmpi(method,'complex-step')
115-
obj.derivative = @(f,x) iderivative(f,x,hi);
116-
obj.partial = @(f,x,k) ipartial(f,x,k,hi);
117-
obj.gradient = @(f,x) igradient(f,x,hi);
118-
obj.directional = @(f,x,v) idirectional(f,x,v,hi);
119-
obj.jacobian = @(f,x) ijacobian(f,x,hi);
120-
obj.hessian = @(f,x) ihessian(f,x,hi,hc);
121-
obj.vechessian = @(f,x) ivechessian(f,x,hi,hc);
122-
elseif strcmpi(method,'forward difference')
123-
obj.derivative = @(f,x) fderivative(f,x,hf);
124-
obj.partial = @(f,x,k) fpartial(f,x,k,hf);
125-
obj.gradient = @(f,x) fgradient(f,x,hf);
126-
obj.directional = @(f,x,v) fdirectional(f,x,v,hf);
127-
obj.jacobian = @(f,x) fjacobian(f,x,hf);
128-
obj.hessian = @(f,x) fhessian(f,x,hf2);
129-
obj.vechessian = @(f,x) fvechessian(f,x,hf2);
134+
if strcmpi(obj.method,'central difference')
135+
obj.derivative = @(f,x) cderivative(f,x,obj.hc);
136+
obj.partial = @(f,x,k) cpartial(f,x,k,obj.hc);
137+
obj.gradient = @(f,x) cgradient(f,x,obj.hc);
138+
obj.directional = @(f,x,v) cdirectional(f,x,v,obj.hc);
139+
obj.jacobian = @(f,x) cjacobian(f,x,obj.hc);
140+
obj.hessian = @(f,x) chessian(f,x,obj.hc);
141+
obj.vechessian = @(f,x) cvechessian(f,x,obj.hc);
142+
elseif strcmpi(obj.method,'complex-step')
143+
obj.derivative = @(f,x) iderivative(f,x,obj.hi);
144+
obj.partial = @(f,x,k) ipartial(f,x,k,obj.hi);
145+
obj.gradient = @(f,x) igradient(f,x,obj.hi);
146+
obj.directional = @(f,x,v) idirectional(f,x,v,obj.hi);
147+
obj.jacobian = @(f,x) ijacobian(f,x,obj.hi);
148+
obj.hessian = @(f,x) ihessian(f,x,obj.hi,obj.hc);
149+
obj.vechessian = @(f,x) ivechessian(f,x,obj.hi,obj.hc);
150+
elseif strcmpi(obj.method,'forward difference')
151+
obj.derivative = @(f,x) fderivative(f,x,obj.hf);
152+
obj.partial = @(f,x,k) fpartial(f,x,k,obj.hf);
153+
obj.gradient = @(f,x) fgradient(f,x,obj.hf);
154+
obj.directional = @(f,x,v) fdirectional(f,x,v,obj.hf);
155+
obj.jacobian = @(f,x) fjacobian(f,x,obj.hf);
156+
obj.hessian = @(f,x) fhessian(f,x,obj.hf2);
157+
obj.vechessian = @(f,x) fvechessian(f,x,obj.hf2);
130158
end
131159

132-
% sets method and step sizes as properties so they may be
133-
% queried/edited
134-
obj.method = method;
135-
obj.hc = hc;
136-
obj.hi = hi;
137-
obj.hf = hf;
138-
obj.hf2 = hf2;
139-
140160
end
141161

142162
end

toolbox/DifferentiatorOld.m

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
%==========================================================================
2+
%
3+
% Differentiator Class defining a differentiator object.
4+
%
5+
% Copyright © 2022 Tamas Kis
6+
% Last Update: 2022-09-10
7+
% Website: https://tamaskis.github.io
8+
9+
%
10+
% TOOLBOX DOCUMENTATION:
11+
% https://tamaskis.github.io/Numerical_Differentiation_Toolbox-MATLAB/
12+
%
13+
% TECHNICAL DOCUMENTATION:
14+
% https://tamaskis.github.io/files/Numerical_Differentiation.pdf
15+
%
16+
%==========================================================================
17+
18+
classdef Differentiator
19+
20+
% -----------
21+
% Properties.
22+
% -----------
23+
24+
properties
25+
derivative % (1×1 function_handle) derivative of a univariate, vector-valued function
26+
partial % (1×1 function_handle) partial derivative of a multivariate, vector-valued function
27+
gradient % (1×1 function_handle) gradient of a multivariate, scalar-valued function
28+
directional % (1×1 function_handle) directional derivative of a multivariate, scalar-valued function
29+
jacobian % (1×1 function_handle) Jacobian of a multivariate, vector-valued function
30+
hessian % (1×1 function_handle) Hessian of a multivariate, scalar-valued function
31+
vechessian % (1×1 function_handle) vector Hessian of a multivariate, vector-valued function
32+
method % (char) differentiation method
33+
hi % (1×1 double) step size for complex-step approximation
34+
hc % (1×1 double) relative step size for central difference approximation
35+
hf % (1×1 double) relative step size for forward difference approximation
36+
hf2 % (1×1 double) relative step size for calculating Hessian using forward difference approximation
37+
end
38+
39+
% --------
40+
% Methods.
41+
% --------
42+
43+
methods
44+
45+
% ------------
46+
% Constructor.
47+
% ------------
48+
49+
function obj = Differentiator(method,hi,hc,hf,hf2)
50+
% obj = Differentiator(method,hi,hc,hf,hf2)
51+
%
52+
% Constructor.
53+
%--------------------------------------------------------------
54+
%
55+
% ------
56+
% INPUT:
57+
% ------
58+
% method - (OPTIONAL) (char) differentiation method;
59+
% 'central difference', 'complex-step', or
60+
% 'forward difference' (defaults to
61+
% 'central difference')
62+
% hi - (OPTIONAL) (1×1 double) step size for
63+
% complex-step approximation (defaults to 10⁻²⁰⁰)
64+
% hc - (OPTIONAL) (1×1 double) relative step size for
65+
% central difference approximation (defaults to
66+
% ε¹ᐟ³)
67+
% hf - (OPTIONAL) (1×1 double) relative step size for
68+
% forward difference approximation (defaults to √ε)
69+
% hf2 - (OPTIONAL) (1×1 double) relative step size for
70+
% calculating Hessian using forward difference
71+
% approximation (defaults to ε¹ᐟ³)
72+
%
73+
% -------
74+
% OUTPUT:
75+
% -------
76+
% obj - (1×1 Differentiator) differentiator object
77+
%
78+
%--------------------------------------------------------------
79+
80+
% defaults "method" to 'central difference' if not input
81+
if (nargin < 1) || isempty(method)
82+
method = 'central difference';
83+
end
84+
85+
% defaults "hi" to 10⁻²⁰⁰ if not input
86+
if (nargin < 2) || isempty(hi)
87+
hi = 1e-200;
88+
end
89+
90+
% defaults "hc" to ε¹ᐟ³ if not input
91+
if (nargin < 3) || isempty(hc)
92+
hc = eps^(1/3);
93+
end
94+
95+
% defaults "hf" to √ε if not input
96+
if (nargin < 4) || isempty(hf)
97+
hf = sqrt(eps);
98+
end
99+
100+
% defaults "hf2" to ε¹ᐟ³ if not input
101+
if (nargin < 5) || isempty(hf2)
102+
hf2 = eps^(1/3);
103+
end
104+
105+
% sets up differentiation functions
106+
if strcmpi(method,'central difference')
107+
obj.derivative = @(f,x) cderivative(f,x,hc);
108+
obj.partial = @(f,x,k) cpartial(f,x,k,hc);
109+
obj.gradient = @(f,x) cgradient(f,x,hc);
110+
obj.directional = @(f,x,v) cdirectional(f,x,v,hc);
111+
obj.jacobian = @(f,x) cjacobian(f,x,hc);
112+
obj.hessian = @(f,x) chessian(f,x,hc);
113+
obj.vechessian = @(f,x) cvechessian(f,x,hc);
114+
elseif strcmpi(method,'complex-step')
115+
obj.derivative = @(f,x) iderivative(f,x,hi);
116+
obj.partial = @(f,x,k) ipartial(f,x,k,hi);
117+
obj.gradient = @(f,x) igradient(f,x,hi);
118+
obj.directional = @(f,x,v) idirectional(f,x,v,hi);
119+
obj.jacobian = @(f,x) ijacobian(f,x,hi);
120+
obj.hessian = @(f,x) ihessian(f,x,hi,hc);
121+
obj.vechessian = @(f,x) ivechessian(f,x,hi,hc);
122+
elseif strcmpi(method,'forward difference')
123+
obj.derivative = @(f,x) fderivative(f,x,hf);
124+
obj.partial = @(f,x,k) fpartial(f,x,k,hf);
125+
obj.gradient = @(f,x) fgradient(f,x,hf);
126+
obj.directional = @(f,x,v) fdirectional(f,x,v,hf);
127+
obj.jacobian = @(f,x) fjacobian(f,x,hf);
128+
obj.hessian = @(f,x) fhessian(f,x,hf2);
129+
obj.vechessian = @(f,x) fvechessian(f,x,hf2);
130+
end
131+
132+
% sets method and step sizes as properties so they may be
133+
% queried/edited
134+
obj.method = method;
135+
obj.hc = hc;
136+
obj.hi = hi;
137+
obj.hf = hf;
138+
obj.hf2 = hf2;
139+
140+
end
141+
142+
end
143+
144+
end

0 commit comments

Comments
 (0)