|
178 | 178 | <tr>
|
179 | 179 | <td style="text-align:center"><TT>method</TT></td>
|
180 | 180 | <td style="text-align:center">-</td>
|
181 |
| - <td>numerical differentiation method</td> |
| 181 | + <td>numerical differentiation method (<TT>'central difference'</TT>, <TT>'forward difference'</TT>, or <TT>'complex-step'</TT>) (defaults to <TT>'central difference'</TT>)</td> |
182 | 182 | <td style="text-align:center">char</td>
|
183 | 183 | </tr>
|
184 |
| - <tr> |
185 |
| - <td style="text-align:center"><TT>hi</TT></td> |
186 |
| - <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{i}" title="" /></td> |
187 |
| - <td>step size for complex-step approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{-200}" title="" />)</td> |
188 |
| - <td style="text-align:center">1×1<BR>double</td> |
189 |
| - </tr> |
190 | 184 | <tr>
|
191 | 185 | <td style="text-align:center"><TT>hc</TT></td>
|
192 | 186 | <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{c}" title="" /></td>
|
|
202 | 196 | <tr>
|
203 | 197 | <td style="text-align:center"><TT>hf2</TT></td>
|
204 | 198 | <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{f_{2}}" title="" /></td>
|
205 |
| - <td>elative step size for foward difference approximation (specifically for Hessians) (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td> |
| 199 | + <td>relative step size for foward difference approximation (specifically for Hessians) (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td> |
| 200 | + <td style="text-align:center">1×1<BR>double</td> |
| 201 | + </tr> |
| 202 | + <tr> |
| 203 | + <td style="text-align:center"><TT>hi</TT></td> |
| 204 | + <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{i}" title="" /></td> |
| 205 | + <td>step size for complex-step approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{-200}" title="" />)</td> |
206 | 206 | <td style="text-align:center">1×1<BR>double</td>
|
207 | 207 | </tr>
|
208 | 208 | </table>
|
209 |
| -</p><h2 id="2">Constructor</h2><pre class="language-matlab">differentiator = Differentiator |
210 |
| -differentiator = Differentiator(method) |
| 209 | +</p><h2 id="2">Constructor</h2><pre class="language-matlab">d = Differentiator |
| 210 | +d = Differentiator(method) |
211 | 211 | </pre><p>
|
212 | 212 | <table border=1>
|
213 | 213 | <tr>
|
|
219 | 219 | <tr>
|
220 | 220 | <td style="text-align:center"><TT>method</TT></td>
|
221 | 221 | <td style="text-align:center">-</td>
|
222 |
| - <td>(OPTIONAL) numerical differentiation method (defaults to <TT>'central difference'</TT>)</td> |
| 222 | + <td>(OPTIONAL) numerical differentiation method (<TT>'central difference'</TT>, <TT>'forward difference'</TT>, or <TT>'complex-step'</TT>) (defaults to <TT>'central difference'</TT>)</td> |
223 | 223 | <td style="text-align:center">char</td>
|
224 | 224 | </tr>
|
225 | 225 | </table>
|
|
239 | 239 | <td style="text-align:center">1×1<BR>double</td>
|
240 | 240 | </tr>
|
241 | 241 | </table>
|
242 |
| -</p><h2 id="4">Note</h2><div><ul><li>The <tt>Differentiator</tt> class uses the central difference approximation by default as a compromise between accuracy and compatibility. The central difference approximation, like the forward difference approximation, can be used with any function, but it is also of a higher order than the forward difference approximation. The complex-step approximation is more accurate than the central difference approximation, but it is <i>not</i> compatible with all functions (in some cases, functions have to be replaced with their "complexified" versions to be compatible with the complex-step approximation).</li><li>The complex-step approximation is still the most accurate approximation by far, and should be used if all functions that are incompatible with the complex-step approximation have been replaced with their complexified equivalent (i.e. replacing <tt>atan2</tt> with <tt>iatan2</tt>).</li></ul></div><h2 id="5">Example #1: Default <tt>Differentiator</tt> object.</h2><p>Let's define a default <tt>Differentiator</tt> object.</p><pre class="codeinput">D = Differentiator |
| 242 | +</p><h2 id="4">Note</h2><div><ul><li>The <tt>Differentiator</tt> class uses the central difference approximation by default as a compromise between accuracy and compatibility. The central difference approximation, like the forward difference approximation, can be used with any function, but it is also of a higher order than the forward difference approximation. The complex-step approximation is more accurate than the central difference approximation, but it is <i>not</i> compatible with all functions (in some cases, functions have to be replaced with their "complexified" versions to be compatible with the complex-step approximation).</li><li>The complex-step approximation is still the most accurate approximation by far, and should be used if all functions that are incompatible with the complex-step approximation have been replaced with their complexified equivalent (i.e. replacing <tt>atan2</tt> with <tt>iatan2</tt>).</li><li>The reason that the differentiation functions are defined as properties rather than as methods is for speed. If they were defined as methods, each time they were called they would have to do checking of which differentiation method to use and call then call the appropriate functions. Defining them as properties avoids this repeated logical checking.</li></ul></div><h2 id="5">Example #1: Default <tt>Differentiator</tt> object.</h2><p>Let's define a default <tt>Differentiator</tt> object.</p><pre class="codeinput">d = Differentiator |
243 | 243 | </pre><pre class="codeoutput">
|
244 |
| -D = |
| 244 | +d = |
245 | 245 |
|
246 | 246 | Differentiator with properties:
|
247 | 247 |
|
|
253 | 253 | hessian: @(f,x)chessian(f,x,obj.hc)
|
254 | 254 | vechessian: @(f,x)cvechessian(f,x,obj.hc)
|
255 | 255 | method: 'central difference'
|
256 |
| - hi: 1.0000e-200 |
257 | 256 | hc: 6.0555e-06
|
258 | 257 | hf: 1.4901e-08
|
259 | 258 | hf2: 6.0555e-06
|
| 259 | + hi: 1.0000e-200 |
260 | 260 |
|
261 | 261 | </pre><p>Note that the <tt>Differentiator</tt> class uses the central difference approximation by default. Evaluating the derivative of <img src="Differentiator_doc_eq17084161653528035876.png" alt="$f(x)=x^{2}$" style="width:46px;height:12px;"> at <img src="Differentiator_doc_eq04851200360885152548.png" alt="$x=2$" style="width:26px;height:8px;">,</p><pre class="codeinput">f = @(x) x^2;
|
262 |
| -df = D.derivative(f,2) |
| 262 | +df = d.derivative(f,2) |
263 | 263 | </pre><pre class="codeoutput">
|
264 | 264 | df =
|
265 | 265 |
|
|
271 | 271 |
|
272 | 272 | 3.5696e-12
|
273 | 273 |
|
274 |
| -</pre><h2 id="8">Example #2: Specificying a differentiation method.</h2><p>Let's define a <tt>Differentiation</tt> object that uses the forward difference approximation.</p><pre class="codeinput">D = Differentiator(<span class="string">'forward difference'</span>); |
| 274 | +</pre><h2 id="8">Example #2: Specificying a differentiation method.</h2><p>Let's define a <tt>Differentiation</tt> object that uses the forward difference approximation.</p><pre class="codeinput">d = Differentiator(<span class="string">'forward difference'</span>); |
275 | 275 | </pre><p>Evaluating the derivative of <img src="Differentiator_doc_eq07863013981449083213.png" alt="$f(x)=x^{3}$" style="width:46px;height:12px;"> at <img src="Differentiator_doc_eq14752139412850437817.png" alt="$x=3$" style="width:26px;height:8px;">,</p><pre class="codeinput">f = @(x) x^3;
|
276 |
| -df = D.derivative(f,3) |
| 276 | +df = d.derivative(f,3) |
277 | 277 | </pre><pre class="codeoutput">
|
278 | 278 | df =
|
279 | 279 |
|
|
285 | 285 |
|
286 | 286 | 5.3644e-07
|
287 | 287 |
|
288 |
| -</pre><h2 id="11">Example #3: Adjusting the step size.</h2><p>Let's define a <tt>Differentiation</tt> object that uses the complex-step approximation.</p><pre class="codeinput">D = Differentiator(<span class="string">'complex-step'</span>); |
| 288 | +</pre><h2 id="11">Example #3: Adjusting the step size.</h2><p>Let's define a <tt>Differentiation</tt> object that uses the complex-step approximation.</p><pre class="codeinput">d = Differentiator(<span class="string">'complex-step'</span>); |
289 | 289 | </pre><p>Evaluating the derivative of <img src="Differentiator_doc_eq07501950415006138384.png" alt="$f(x)=\sin{(x)}$" style="width:65px;height:11px;"> at <img src="Differentiator_doc_eq15469668660187898548.png" alt="$x=\pi$" style="width:27px;height:6px;">,</p><pre class="codeinput">f = @(x) sin(x);
|
290 |
| -df = D.derivative(f,pi) |
| 290 | +df = d.derivative(f,pi) |
291 | 291 | </pre><pre class="codeoutput">
|
292 | 292 | df =
|
293 | 293 |
|
|
299 | 299 |
|
300 | 300 | 0
|
301 | 301 |
|
302 |
| -</pre><p>Now, let's increase the step-size from <img src="Differentiator_doc_eq09646552950603438366.png" alt="$10^{-200}$" style="width:28px;height:10px;"> (the default step size that is used with the complex-step approximation) to <img src="Differentiator_doc_eq00842521489666112223.png" alt="$h=0.001$" style="width:45px;height:8px;">.</p><pre class="codeinput">D.set_step_size(0.001) |
| 302 | +</pre><p>Now, let's increase the step-size from <img src="Differentiator_doc_eq09646552950603438366.png" alt="$10^{-200}$" style="width:28px;height:10px;"> (the default step size that is used with the complex-step approximation) to <img src="Differentiator_doc_eq00842521489666112223.png" alt="$h=0.001$" style="width:45px;height:8px;">.</p><pre class="codeinput">d.set_step_size(0.001) |
303 | 303 | </pre><pre class="codeoutput">
|
304 | 304 | ans =
|
305 | 305 |
|
|
313 | 313 | hessian: @(f,x)ihessian(f,x,obj.hi,obj.hc)
|
314 | 314 | vechessian: @(f,x)ivechessian(f,x,obj.hi,obj.hc)
|
315 | 315 | method: 'complex-step'
|
316 |
| - hi: 1.0000e-03 |
317 | 316 | hc: 6.0555e-06
|
318 | 317 | hf: 1.4901e-08
|
319 | 318 | hf2: 6.0555e-06
|
| 319 | + hi: 1.0000e-03 |
320 | 320 |
|
321 |
| -</pre><p>We can see that <tt>hi</tt> has been updated to <tt>1.0000e-03</tt>. Now, evaluating the same derivative as before,</p><pre class="codeinput">df = D.derivative(f,pi) |
| 321 | +</pre><p>We can see that <tt>hi</tt> has been updated to <tt>1.0000e-03</tt>. Now, evaluating the same derivative as before,</p><pre class="codeinput">df = d.derivative(f,pi) |
322 | 322 | </pre><pre class="codeoutput">
|
323 | 323 | df =
|
324 | 324 |
|
|
448 | 448 | % <tr>
|
449 | 449 | % <td style="text-align:center"><TT>method</TT></td>
|
450 | 450 | % <td style="text-align:center">-</td>
|
451 |
| -% <td>numerical differentiation method</td> |
| 451 | +% <td>numerical differentiation method (<TT>'central difference'</TT>, <TT>'forward difference'</TT>, or <TT>'complex-step'</TT>) (defaults to <TT>'central difference'</TT>)</td> |
452 | 452 | % <td style="text-align:center">char</td>
|
453 | 453 | % </tr>
|
454 | 454 | % <tr>
|
455 |
| -% <td style="text-align:center"><TT>hi</TT></td> |
456 |
| -% <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{i}" title="" /></td> |
457 |
| -% <td>step size for complex-step approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{-200}" title="" />)</td> |
458 |
| -% <td style="text-align:center">1×1<BR>double</td> |
459 |
| -% </tr> |
460 |
| -% <tr> |
461 | 455 | % <td style="text-align:center"><TT>hc</TT></td>
|
462 | 456 | % <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{c}" title="" /></td>
|
463 | 457 | % <td>relative step size for central difference approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td>
|
|
472 | 466 | % <tr>
|
473 | 467 | % <td style="text-align:center"><TT>hf2</TT></td>
|
474 | 468 | % <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{f_{2}}" title="" /></td>
|
475 |
| -% <td>elative step size for foward difference approximation (specifically for Hessians) (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td> |
| 469 | +% <td>relative step size for foward difference approximation (specifically for Hessians) (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{1/3}" title="" />)</td> |
| 470 | +% <td style="text-align:center">1×1<BR>double</td> |
| 471 | +% </tr> |
| 472 | +% <tr> |
| 473 | +% <td style="text-align:center"><TT>hi</TT></td> |
| 474 | +% <td style="text-align:center"><img src="https://latex.codecogs.com/svg.latex?\inline&space;h_{i}" title="" /></td> |
| 475 | +% <td>step size for complex-step approximation (defaults to <img src="https://latex.codecogs.com/svg.latex?\inline&space;\varepsilon^{-200}" title="" />)</td> |
476 | 476 | % <td style="text-align:center">1×1<BR>double</td>
|
477 | 477 | % </tr>
|
478 | 478 | % </table>
|
479 | 479 | % </html>
|
480 | 480 | %% Constructor
|
481 |
| -% differentiator = Differentiator |
482 |
| -% differentiator = Differentiator(method) |
| 481 | +% d = Differentiator |
| 482 | +% d = Differentiator(method) |
483 | 483 | %
|
484 | 484 | % <html>
|
485 | 485 | % <table border=1>
|
|
492 | 492 | % <tr>
|
493 | 493 | % <td style="text-align:center"><TT>method</TT></td>
|
494 | 494 | % <td style="text-align:center">-</td>
|
495 |
| -% <td>(OPTIONAL) numerical differentiation method (defaults to <TT>'central difference'</TT>)</td> |
| 495 | +% <td>(OPTIONAL) numerical differentiation method (<TT>'central difference'</TT>, <TT>'forward difference'</TT>, or <TT>'complex-step'</TT>) (defaults to <TT>'central difference'</TT>)</td> |
496 | 496 | % <td style="text-align:center">char</td>
|
497 | 497 | % </tr>
|
498 | 498 | % </table>
|
|
532 | 532 | % by far, and should be used if all functions that are incompatible with
|
533 | 533 | % the complex-step approximation have been replaced with their complexified
|
534 | 534 | % equivalent (i.e. replacing |atan2| with |iatan2|).
|
| 535 | +% * The reason that the differentiation functions are defined as properties |
| 536 | +% rather than as methods is for speed. If they were defined as methods, |
| 537 | +% each time they were called they would have to do checking of which |
| 538 | +% differentiation method to use and call then call the appropriate |
| 539 | +% functions. Defining them as properties avoids this repeated logical |
| 540 | +% checking. |
535 | 541 | %% Example #1: Default |Differentiator| object.
|
536 | 542 | % Let's define a default |Differentiator| object.
|
537 |
| -D = Differentiator |
| 543 | +d = Differentiator |
538 | 544 | %%
|
539 | 545 | % Note that the |Differentiator| class uses the central difference
|
540 | 546 | % approximation by default. Evaluating the derivative of $f(x)=x^{2}$ at
|
541 | 547 | % $x=2$,
|
542 | 548 | f = @(x) x^2;
|
543 |
| -df = D.derivative(f,2) |
| 549 | +df = d.derivative(f,2) |
544 | 550 | %%
|
545 | 551 | % Taking a look at the error (the expected result is 4),
|
546 | 552 | err = abs(df-4)
|
547 | 553 | %% Example #2: Specificying a differentiation method.
|
548 | 554 | % Let's define a |Differentiation| object that uses the forward difference
|
549 | 555 | % approximation.
|
550 |
| -D = Differentiator('forward difference'); |
| 556 | +d = Differentiator('forward difference'); |
551 | 557 | %%
|
552 | 558 | % Evaluating the derivative of $f(x)=x^{3}$ at $x=3$,
|
553 | 559 | f = @(x) x^3;
|
554 |
| -df = D.derivative(f,3) |
| 560 | +df = d.derivative(f,3) |
555 | 561 | %%
|
556 | 562 | % Taking a look at the error (the expected result is 27),
|
557 | 563 | err = abs(df-27)
|
558 | 564 | %% Example #3: Adjusting the step size.
|
559 | 565 | % Let's define a |Differentiation| object that uses the complex-step
|
560 | 566 | % approximation.
|
561 |
| -D = Differentiator('complex-step'); |
| 567 | +d = Differentiator('complex-step'); |
562 | 568 | %%
|
563 | 569 | % Evaluating the derivative of $f(x)=\sin{(x)}$ at $x=\pi$,
|
564 | 570 | f = @(x) sin(x);
|
565 |
| -df = D.derivative(f,pi) |
| 571 | +df = d.derivative(f,pi) |
566 | 572 | %%
|
567 | 573 | % Taking a look at the error (the expected result is -1),
|
568 | 574 | err = abs(df-(-1))
|
569 | 575 | %%
|
570 | 576 | % Now, let's increase the step-size from $10^{-200}$ (the default step size
|
571 | 577 | % that is used with the complex-step approximation) to $h=0.001$.
|
572 |
| -D.set_step_size(0.001) |
| 578 | +d.set_step_size(0.001) |
573 | 579 | %%
|
574 | 580 | % We can see that |hi| has been updated to |1.0000e-03|. Now, evaluating
|
575 | 581 | % the same derivative as before,
|
576 |
| -df = D.derivative(f,pi) |
| 582 | +df = d.derivative(f,pi) |
577 | 583 | %%
|
578 | 584 | % Taking a look at the error once more,
|
579 | 585 | err = abs(df-(-1))
|
|
0 commit comments