Codebase list cppad / debian/2017.00.00.8-1 doc / checkpoint_ode.cpp.xml
debian/2017.00.00.8-1

Tree @debian/2017.00.00.8-1 (Download .tar.gz)

checkpoint_ode.cpp.xml @debian/2017.00.00.8-1raw · history · blame

  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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Checkpointing an ODE Solver: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Checkpointing an ODE Solver: Example and Test"/>
<meta name="keywords" id="keywords" content=" checkpointing ode solver: example test see also purpose problem solver solution "/>
<style type='text/css'>
body { color : black }
body { background-color : white }
A:link { color : blue }
A:visited { color : purple }
A:active { color : purple }
</style>
<script type='text/javascript' language='JavaScript' src='_checkpoint_ode.cpp_xml.js'>
</script>
</head>
<body>
<table><tr>
<td>
<a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a>
</td>
<td><a href="atomic_mul_level.cpp.xml" target="_top">Prev</a>
</td><td><a href="checkpoint_extended_ode.cpp.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</option>
<option>contents</option>
<option>reference</option>
<option>index</option>
<option>search</option>
<option>external</option>
</select>
</td>
<td>
<select onchange='choose_up0(this)'>
<option>Up-&gt;</option>
<option>CppAD</option>
<option>AD</option>
<option>ADValued</option>
<option>atomic</option>
<option>checkpoint</option>
<option>checkpoint_ode.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>ADValued-&gt;</option>
<option>Arithmetic</option>
<option>unary_standard_math</option>
<option>binary_math</option>
<option>CondExp</option>
<option>Discrete</option>
<option>numeric_limits</option>
<option>atomic</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>atomic-&gt;</option>
<option>checkpoint</option>
<option>atomic_base</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>checkpoint-&gt;</option>
<option>checkpoint.cpp</option>
<option>atomic_mul_level.cpp</option>
<option>checkpoint_ode.cpp</option>
<option>checkpoint_extended_ode.cpp</option>
</select>
</td>
<td>checkpoint_ode.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>See Also</option>
<option>Purpose</option>
<option>Problem</option>
<option>ODE Solver</option>
<option>ODE</option>
<option>Solution</option>
</select>
</td>
</tr></table><br/>
<center><b><big><big>Checkpointing an ODE Solver: Example and Test</big></big></b></center>
<br/>
<b><big><a name="See Also" id="See Also">See Also</a></big></b>

<br/>
<a href="checkpoint_extended_ode.cpp.xml" target="_top"><span style='white-space: nowrap'>checkpoint_extended_ode.cpp</span></a>
,

<br/>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
In this example we <a href="checkpoint.xml" target="_top"><span style='white-space: nowrap'>checkpoint</span></a>
 one step of an ODE solver.

<br/>
<br/>
<b><big><a name="Problem" id="Problem">Problem</a></big></b>
<br/>
We consider the initial value problem with parameter 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
</mrow></math>

 defined by,

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>z</mi>
<mo stretchy="false">(</mo>
<mn>0</mn>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<msub><mi mathvariant='italic'>z</mi>
<mn>0</mn>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mrow></math>

,

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mo stretchy="false">&#x02202;</mo>
<mi mathvariant='italic'>t</mi>
</msub>
<mi mathvariant='italic'>z</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mi mathvariant='italic'>h</mi>
<mo stretchy="false">[</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>z</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">]</mo>
</mrow></math>

Note that if 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>t</mi>
</mrow></math>

 needs to be in the equation, one can define
the first component of 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>z</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mrow></math>

 to be equal to 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>t</mi>
</mrow></math>

.

<br/>
<br/>
<b><big><a name="ODE Solver" id="ODE Solver">ODE Solver</a></big></b>
<br/>
For this example, we consider the Fourth order Runge-Kutta ODE solver.
Given an approximation solution at time 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msub><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>k</mi>
</msub>
</mrow></math>

 denoted by

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mi mathvariant='italic'>k</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mrow></math>

, and 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='normal'>&#x00394;</mi>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">=</mo>
<msub><mi mathvariant='italic'>t</mi>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
</mrow>
</msub>
<mo stretchy="false">-</mo>
<msub><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>k</mi>
</msub>
</mrow></math>

,
it defines the approximation solution 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mrow></math>


at time 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msub><mi mathvariant='italic'>t</mi>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
</mrow>
</msub>
</mrow></math>

 by

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mtable rowalign="center" ><mtr><mtd columnalign="right" >
<msub><mi mathvariant='italic'>h</mi>
<mn>1</mn>
</msub>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<mi mathvariant='italic'>h</mi>
<mo stretchy="false">[</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">,</mo>
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mi mathvariant='italic'>k</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">]</mo>
</mtd></mtr><mtr><mtd columnalign="right" >
<msub><mi mathvariant='italic'>h</mi>
<mn>2</mn>
</msub>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<mi mathvariant='italic'>h</mi>
<mo stretchy="false">[</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">,</mo>
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mi mathvariant='italic'>k</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">+</mo>
<mi mathvariant='normal'>&#x00394;</mi>
<mi mathvariant='italic'>t</mi>
<mspace width='.3em'/>
<msub><mi mathvariant='italic'>h</mi>
<mn>1</mn>
</msub>
<mo stretchy="false">/</mo>
<mn>2</mn>
<mo stretchy="false">]</mo>
</mtd></mtr><mtr><mtd columnalign="right" >
<msub><mi mathvariant='italic'>h</mi>
<mn>3</mn>
</msub>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<mi mathvariant='italic'>h</mi>
<mo stretchy="false">[</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">,</mo>
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mi mathvariant='italic'>k</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">+</mo>
<mi mathvariant='normal'>&#x00394;</mi>
<mi mathvariant='italic'>t</mi>
<mspace width='.3em'/>
<msub><mi mathvariant='italic'>h</mi>
<mn>2</mn>
</msub>
<mo stretchy="false">/</mo>
<mn>2</mn>
<mo stretchy="false">]</mo>
</mtd></mtr><mtr><mtd columnalign="right" >
<msub><mi mathvariant='italic'>h</mi>
<mn>4</mn>
</msub>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<mi mathvariant='italic'>h</mi>
<mo stretchy="false">[</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">,</mo>
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mi mathvariant='italic'>k</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">+</mo>
<mi mathvariant='normal'>&#x00394;</mi>
<mi mathvariant='italic'>t</mi>
<mspace width='.3em'/>
<msub><mi mathvariant='italic'>h</mi>
<mn>3</mn>
</msub>
<mo stretchy="false">]</mo>
</mtd></mtr><mtr><mtd columnalign="right" >
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mi mathvariant='italic'>k</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">+</mo>
<mi mathvariant='normal'>&#x00394;</mi>
<mi mathvariant='italic'>t</mi>
<mspace width='.3em'/>
<mo stretchy="false">(</mo>
<msub><mi mathvariant='italic'>h</mi>
<mn>1</mn>
</msub>
<mo stretchy="false">+</mo>
<mn>2</mn>
<msub><mi mathvariant='italic'>h</mi>
<mn>2</mn>
</msub>
<mo stretchy="false">+</mo>
<mn>2</mn>
<msub><mi mathvariant='italic'>h</mi>
<mn>3</mn>
</msub>
<mo stretchy="false">+</mo>
<msub><mi mathvariant='italic'>h</mi>
<mn>4</mn>
</msub>
<mo stretchy="false">)</mo>
<mo stretchy="false">/</mo>
<mn>6</mn>
</mtd></mtr></mtable>
</mrow></math>

If 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mi mathvariant='italic'>k</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<msub><mi mathvariant='italic'>z</mi>
<mi mathvariant='italic'>k</mi>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mrow></math>

,

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msub><mover accent='false'><mrow><mi mathvariant='italic'>z</mi>
</mrow>
<mo stretchy='false'>&#x002DC;</mo></mover>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<msub><mi mathvariant='italic'>z</mi>
<mrow><mi mathvariant='italic'>k</mi>
<mo stretchy="false">+</mo>
<mn>1</mn>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">+</mo>
<mi mathvariant='italic'>O</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='normal'>&#x00394;</mi>
<msup><mi mathvariant='italic'>t</mi>
<mn>5</mn>
</msup>
<mo stretchy="false">)</mo>
</mrow></math>

.
Other ODE solvers can use a similar method to the one used below.

<br/>
<br/>
<b><big><a name="ODE" id="ODE">ODE</a></big></b>
<br/>
For this example the ODE is defined by

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>z</mi>
<mo stretchy="false">(</mo>
<mn>0</mn>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mn>0</mn>
</mrow></math>

 and

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi mathvariant='italic'>h</mi>
<mo stretchy="false">[</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>z</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">]</mo>
<mo stretchy="false">=</mo>
<mrow><mo stretchy="true">(</mo><mrow><mtable rowalign="center" ><mtr><mtd columnalign="center" >
<msub><mi mathvariant='italic'>x</mi>
<mn>0</mn>
</msub>
</mtd></mtr><mtr><mtd columnalign="center" >
<msub><mi mathvariant='italic'>x</mi>
<mn>1</mn>
</msub>
<msub><mi mathvariant='italic'>z</mi>
<mn>0</mn>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mtd></mtr><mtr><mtd columnalign="center" >
<mo stretchy="false">&#x022EE;</mo>
</mtd></mtr><mtr><mtd columnalign="center" >
<msub><mi mathvariant='italic'>x</mi>
<mrow><mi mathvariant='italic'>n</mi>
<mn>-1</mn>
</mrow>
</msub>
<msub><mi mathvariant='italic'>z</mi>
<mrow><mi mathvariant='italic'>n</mi>
<mn>-2</mn>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mtd></mtr></mtable>
</mrow><mo stretchy="true">)</mo></mrow>
<mo stretchy="false">=</mo>
<mrow><mo stretchy="true">(</mo><mrow><mtable rowalign="center" ><mtr><mtd columnalign="center" >
<msub><mo stretchy="false">&#x02202;</mo>
<mi mathvariant='italic'>t</mi>
</msub>
<msub><mi mathvariant='italic'>z</mi>
<mn>0</mn>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mtd></mtr><mtr><mtd columnalign="center" >
<msub><mo stretchy="false">&#x02202;</mo>
<mi mathvariant='italic'>t</mi>
</msub>
<msub><mi mathvariant='italic'>z</mi>
<mn>1</mn>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mtd></mtr><mtr><mtd columnalign="center" >
<mo stretchy="false">&#x022EE;</mo>
</mtd></mtr><mtr><mtd columnalign="center" >
<msub><mo stretchy="false">&#x02202;</mo>
<mi mathvariant='italic'>t</mi>
</msub>
<msub><mi mathvariant='italic'>z</mi>
<mrow><mi mathvariant='italic'>n</mi>
<mn>-1</mn>
</mrow>
</msub>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mtd></mtr></mtable>
</mrow><mo stretchy="true">)</mo></mrow>
</mrow></math>

<br/>
<b><big><a name="Solution" id="Solution">Solution</a></big></b>
<br/>
The solution of the ODE for this example,
which is used to check the results,
can be calculated by
starting with the first row and then using the solution
for the first row to solve the second and so on.
Doing this we obtain

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi mathvariant='italic'>z</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">,</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mrow><mo stretchy="true">(</mo><mrow><mtable rowalign="center" ><mtr><mtd columnalign="center" >
<msub><mi mathvariant='italic'>x</mi>
<mn>0</mn>
</msub>
<mi mathvariant='italic'>t</mi>
</mtd></mtr><mtr><mtd columnalign="center" >
<msub><mi mathvariant='italic'>x</mi>
<mn>1</mn>
</msub>
<msub><mi mathvariant='italic'>x</mi>
<mn>0</mn>
</msub>
<msup><mi mathvariant='italic'>t</mi>
<mn>2</mn>
</msup>
<mo stretchy="false">/</mo>
<mn>2</mn>
</mtd></mtr><mtr><mtd columnalign="center" >
<mo stretchy="false">&#x022EE;</mo>
</mtd></mtr><mtr><mtd columnalign="center" >
<msub><mi mathvariant='italic'>x</mi>
<mrow><mi mathvariant='italic'>n</mi>
<mn>-1</mn>
</mrow>
</msub>
<msub><mi mathvariant='italic'>x</mi>
<mrow><mi mathvariant='italic'>n</mi>
<mn>-2</mn>
</mrow>
</msub>
<mo stretchy="false">&#x02026;</mo>
<msub><mi mathvariant='italic'>x</mi>
<mn>0</mn>
</msub>
<msup><mi mathvariant='italic'>t</mi>
<mi mathvariant='italic'>n</mi>
</msup>
<mo stretchy="false">/</mo>
<mi mathvariant='italic'>n</mi>
<mo stretchy="false">!</mo>
</mtd></mtr></mtable>
</mrow><mo stretchy="true">)</mo></mrow>
</mrow></math>

<code><font color="blue"><pre><tt>
<span style="font-weight: bold"><span style="color: #000080"># include</span></span> <span style="color: #FF0000">&lt;cppad/cppad.hpp&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> <span style="color: #FF0000">{</span>
     <span style="font-weight: bold"><span style="color: #0000FF">using</span></span> CppAD<span style="color: #990000">::</span>AD<span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">AD&lt;double&gt;</span>                     a1double<span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="color: #008080">AD&lt;a1double&gt;</span>                   a2double<span style="color: #990000">;</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="font-weight: bold"><span style="color: #000000">CPPAD_TESTVECTOR</span></span><span style="color: #990000">(</span>   <span style="color: #009900">double</span> <span style="color: #990000">)</span>   a0vector<span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="font-weight: bold"><span style="color: #000000">CPPAD_TESTVECTOR</span></span><span style="color: #990000">(</span> a1double <span style="color: #990000">)</span>   a1vector<span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="font-weight: bold"><span style="color: #000000">CPPAD_TESTVECTOR</span></span><span style="color: #990000">(</span> a2double <span style="color: #990000">)</span>   a2vector<span style="color: #990000">;</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// set once by main and kept that way</span></span>
     <span style="color: #009900">double</span> delta_t_ <span style="color: #990000">=</span> std<span style="color: #990000">::</span>numeric_limits<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;::</span><span style="font-weight: bold"><span style="color: #000000">quiet_NaN</span></span><span style="color: #990000">();</span>
     <span style="color: #008080">size_t</span> n_       <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// The function h( x , y)</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">FloatVector</span><span style="color: #990000">&gt;</span>
     <span style="color: #008080">FloatVector</span> <span style="font-weight: bold"><span style="color: #000000">h</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> FloatVector<span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> FloatVector<span style="color: #990000">&amp;</span> y<span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> x<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_ <span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> y<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_ <span style="color: #990000">);</span>
          <span style="color: #008080">FloatVector</span> <span style="font-weight: bold"><span style="color: #000000">result</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">);</span>
          result<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">];</span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
               result<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> x<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">*</span> y<span style="color: #990000">[</span>i<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">];</span>
          <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> result<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// The 4-th Order Runge-Kutta Step</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">FloatVector</span><span style="color: #990000">&gt;</span>
     <span style="color: #008080">FloatVector</span> <span style="font-weight: bold"><span style="color: #000000">Runge4</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> FloatVector<span style="color: #990000">&amp;</span> x<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> FloatVector<span style="color: #990000">&amp;</span> z0
     <span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> x<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_ <span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> z0<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_ <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> <span style="font-weight: bold"><span style="color: #0000FF">typename</span></span> <span style="color: #008080">FloatVector</span><span style="color: #990000">::</span><span style="color: #008080">value_type</span> Float<span style="color: #990000">;</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="color: #008080">Float</span>  dt <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">Float</span></span><span style="color: #990000">(</span>delta_t_<span style="color: #990000">);</span>
          <span style="color: #008080">size_t</span> m  <span style="color: #990000">=</span> z0<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">();</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="color: #008080">FloatVector</span> <span style="font-weight: bold"><span style="color: #000000">h1</span></span><span style="color: #990000">(</span>m<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">h2</span></span><span style="color: #990000">(</span>m<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">h3</span></span><span style="color: #990000">(</span>m<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">h4</span></span><span style="color: #990000">(</span>m<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">result</span></span><span style="color: #990000">(</span>m<span style="color: #990000">);</span>
          h1 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">h</span></span><span style="color: #990000">(</span> x<span style="color: #990000">,</span> z0 <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
               h2<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> z0<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> dt <span style="color: #990000">*</span> h1<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">/</span> <span style="color: #993399">2.0</span><span style="color: #990000">;</span>
          h2 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">h</span></span><span style="color: #990000">(</span> x<span style="color: #990000">,</span> h2 <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
               h3<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> z0<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> dt <span style="color: #990000">*</span> h2<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">/</span> <span style="color: #993399">2.0</span><span style="color: #990000">;</span>
          h3 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">h</span></span><span style="color: #990000">(</span> x<span style="color: #990000">,</span> h3 <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
               h4<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> z0<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> dt <span style="color: #990000">*</span> h3<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
          h4 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">h</span></span><span style="color: #990000">(</span> x<span style="color: #990000">,</span> h4 <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> m<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
          <span style="color: #FF0000">{</span>     <span style="color: #008080">Float</span> dz <span style="color: #990000">=</span> dt <span style="color: #990000">*</span> <span style="color: #990000">(</span> h1<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">2.0</span><span style="color: #990000">*</span>h2<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> <span style="color: #993399">2.0</span><span style="color: #990000">*</span>h3<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> h4<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">)</span> <span style="color: #990000">/</span> <span style="color: #993399">6.0</span><span style="color: #990000">;</span>
               result<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> z0<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">+</span> dz<span style="color: #990000">;</span>
          <span style="color: #FF0000">}</span>
          <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> result<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// pack x and z into an ode_info vector</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">FloatVector</span><span style="color: #990000">&gt;</span>
     <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">pack</span></span><span style="color: #990000">(</span>
          FloatVector<span style="color: #990000">&amp;</span>         ode_info <span style="color: #990000">,</span>
          <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> FloatVector<span style="color: #990000">&amp;</span>   x        <span style="color: #990000">,</span>
          <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> FloatVector<span style="color: #990000">&amp;</span>   z        <span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> ode_info<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_ <span style="color: #990000">+</span> n_ <span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> x<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span>        <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_      <span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> z<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span>        <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_      <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="color: #008080">size_t</span> offset <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
               ode_info<span style="color: #990000">[</span>offset <span style="color: #990000">+</span> i<span style="color: #990000">]</span> <span style="color: #990000">=</span> x<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
          offset <span style="color: #990000">+=</span> n_<span style="color: #990000">;</span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
               ode_info<span style="color: #990000">[</span>offset <span style="color: #990000">+</span> i<span style="color: #990000">]</span> <span style="color: #990000">=</span> z<span style="color: #990000">[</span>i<span style="color: #990000">];</span>
     <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// unpack an ode_info vector</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">template</span></span> <span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">FloatVector</span><span style="color: #990000">&gt;</span>
     <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">unpack</span></span><span style="color: #990000">(</span>
          <span style="font-weight: bold"><span style="color: #0000FF">const</span></span> FloatVector<span style="color: #990000">&amp;</span>         ode_info <span style="color: #990000">,</span>
          FloatVector<span style="color: #990000">&amp;</span>               x        <span style="color: #990000">,</span>
          FloatVector<span style="color: #990000">&amp;</span>               z        <span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> ode_info<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_ <span style="color: #990000">+</span> n_ <span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> x<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span>        <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_      <span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> z<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span>        <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_      <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="color: #008080">size_t</span> offset <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
               x<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> ode_info<span style="color: #990000">[</span>offset <span style="color: #990000">+</span> i<span style="color: #990000">];</span>
          offset <span style="color: #990000">+=</span> n_<span style="color: #990000">;</span>
          <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
               z<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> ode_info<span style="color: #990000">[</span>offset <span style="color: #990000">+</span> i<span style="color: #990000">];</span>
     <span style="color: #FF0000">}</span>

     <span style="font-style: italic"><span style="color: #9A1900">// Algorithm that z(t, x)</span></span>
     <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">ode_algo</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> a1vector<span style="color: #990000">&amp;</span> ode_info_in<span style="color: #990000">,</span> a1vector<span style="color: #990000">&amp;</span> ode_info_out<span style="color: #990000">)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> ode_info_in<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span>  <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_ <span style="color: #990000">+</span> n_ <span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #000000">assert</span></span><span style="color: #990000">(</span> <span style="font-weight: bold"><span style="color: #000000">size_t</span></span><span style="color: #990000">(</span> ode_info_out<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">size</span></span><span style="color: #990000">()</span> <span style="color: #990000">)</span> <span style="color: #990000">==</span> n_ <span style="color: #990000">+</span> n_ <span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// initial ode information</span></span>
          <span style="color: #008080">a1vector</span> <span style="font-weight: bold"><span style="color: #000000">x</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">z0</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">);</span>
          <span style="font-weight: bold"><span style="color: #000000">unpack</span></span><span style="color: #990000">(</span>ode_info_in<span style="color: #990000">,</span> x<span style="color: #990000">,</span> z0<span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// advance z(t, x)</span></span>
          <span style="color: #008080">a1vector</span> z1 <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">Runge4</span></span><span style="color: #990000">(</span>x<span style="color: #990000">,</span> z0<span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-style: italic"><span style="color: #9A1900">// final ode information</span></span>
          <span style="font-weight: bold"><span style="color: #000000">pack</span></span><span style="color: #990000">(</span>ode_info_out<span style="color: #990000">,</span> x<span style="color: #990000">,</span> z1<span style="color: #990000">);</span>
          <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
          <span style="font-weight: bold"><span style="color: #0000FF">return</span></span><span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>
<span style="font-style: italic"><span style="color: #9A1900">//</span></span>
<span style="color: #009900">bool</span> <span style="font-weight: bold"><span style="color: #000000">ode</span></span><span style="color: #990000">(</span><span style="color: #009900">void</span><span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>     <span style="color: #009900">bool</span> ok <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">using</span></span> CppAD<span style="color: #990000">::</span>NearEqual<span style="color: #990000">;</span>
     <span style="color: #009900">double</span> eps <span style="color: #990000">=</span> std<span style="color: #990000">::</span>numeric_limits<span style="color: #990000">&lt;</span><span style="color: #009900">double</span><span style="color: #990000">&gt;::</span><span style="font-weight: bold"><span style="color: #000000">epsilon</span></span><span style="color: #990000">();</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// number of terms in the differential equation</span></span>
     n_ <span style="color: #990000">=</span> <span style="color: #993399">6</span><span style="color: #990000">;</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// step size for the differentiail equation</span></span>
     <span style="color: #008080">size_t</span> n_step <span style="color: #990000">=</span> <span style="color: #993399">10</span><span style="color: #990000">;</span>
     <span style="color: #009900">double</span> T      <span style="color: #990000">=</span> <span style="color: #993399">1.0</span><span style="color: #990000">;</span>
     delta_t_ <span style="color: #990000">=</span> T <span style="color: #990000">/</span> <span style="color: #009900">double</span><span style="color: #990000">(</span>n_step<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// set parameter value and initial value of the ode</span></span>
     <span style="color: #008080">a1vector</span> <span style="font-weight: bold"><span style="color: #000000">ax</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">az0</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     ax<span style="color: #990000">[</span>i<span style="color: #990000">]</span>  <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">a1double</span></span><span style="color: #990000">(</span>i <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
          az0<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">a1double</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">);</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// pack ode information input vector</span></span>
     <span style="color: #008080">a1vector</span> <span style="font-weight: bold"><span style="color: #000000">ode_info_in</span></span><span style="color: #990000">(</span><span style="color: #993399">2</span> <span style="color: #990000">*</span> n_<span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #000000">pack</span></span><span style="color: #990000">(</span>ode_info_in<span style="color: #990000">,</span> ax<span style="color: #990000">,</span> az0<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// create checkpoint version of the algorithm</span></span>
     <span style="color: #008080">a1vector</span> <span style="font-weight: bold"><span style="color: #000000">ode_info_out</span></span><span style="color: #990000">(</span><span style="color: #993399">2</span> <span style="color: #990000">*</span> n_<span style="color: #990000">);</span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">checkpoint&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">ode_check</span></span><span style="color: #990000">(</span>
          <span style="color: #FF0000">"ode"</span><span style="color: #990000">,</span> ode_algo<span style="color: #990000">,</span> ode_info_in<span style="color: #990000">,</span> ode_info_out
     <span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// set the independent variables for recording</span></span>
     CppAD<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">Independent</span></span><span style="color: #990000">(</span> ax <span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// repack to get dependence on ax</span></span>
     <span style="font-weight: bold"><span style="color: #000000">pack</span></span><span style="color: #990000">(</span>ode_info_in<span style="color: #990000">,</span> ax<span style="color: #990000">,</span> az0<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Now run the checkpoint algorithm n_step times</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> k <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> k <span style="color: #990000">&lt;</span> n_step<span style="color: #990000">;</span> k<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #000000">ode_check</span></span><span style="color: #990000">(</span>ode_info_in<span style="color: #990000">,</span> ode_info_out<span style="color: #990000">);</span>
          ode_info_in <span style="color: #990000">=</span> ode_info_out<span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Unpack the results (must use ax1 so do not overwrite ax)</span></span>
     <span style="color: #008080">a1vector</span> <span style="font-weight: bold"><span style="color: #000000">ax1</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">az1</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #000000">unpack</span></span><span style="color: #990000">(</span>ode_info_out<span style="color: #990000">,</span> ax1<span style="color: #990000">,</span> az1<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// We could record a complicated funciton of x and z(T, x) in f,</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// but make this example simpler we record x -&gt; z(T, x).</span></span>
     CppAD<span style="color: #990000">::</span><span style="color: #008080">ADFun&lt;double&gt;</span> <span style="font-weight: bold"><span style="color: #000000">f</span></span><span style="color: #990000">(</span>ax<span style="color: #990000">,</span> az1<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// check function values</span></span>
     <span style="color: #008080">a0vector</span> <span style="font-weight: bold"><span style="color: #000000">x</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">z1</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
          x<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #009900">double</span><span style="color: #990000">(</span>j <span style="color: #990000">+</span> <span style="color: #993399">1</span><span style="color: #990000">);</span>
     z1 <span style="color: #990000">=</span> f<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Forward</span></span><span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">,</span> x<span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// separate calculation of z(t, x)</span></span>
     <span style="color: #008080">a0vector</span> <span style="font-weight: bold"><span style="color: #000000">check_z1</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">);</span>
     check_z1<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> x<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]</span> <span style="color: #990000">*</span> T<span style="color: #990000">;</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
          check_z1<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> x<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">*</span> T <span style="color: #990000">*</span> check_z1<span style="color: #990000">[</span>i<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">/</span> <span style="color: #009900">double</span><span style="color: #990000">(</span>i<span style="color: #990000">+</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// expected accuracy for each component of of z(t, x)</span></span>
     <span style="color: #008080">a0vector</span> <span style="font-weight: bold"><span style="color: #000000">acc</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="font-weight: bold"><span style="color: #0000FF">if</span></span><span style="color: #990000">(</span> i <span style="color: #990000">&lt;</span> <span style="color: #993399">4</span> <span style="color: #990000">)</span>
          <span style="color: #FF0000">{</span>     <span style="font-style: italic"><span style="color: #9A1900">// Runge-Kutta methos is exact for this case</span></span>
               acc<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">10</span><span style="color: #990000">.</span> <span style="color: #990000">*</span> eps<span style="color: #990000">;</span>
          <span style="color: #FF0000">}</span>
          <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
          <span style="color: #FF0000">{</span>     acc<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1.0</span><span style="color: #990000">;</span>
               <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> k <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> k <span style="color: #990000">&lt;</span> <span style="color: #993399">5</span><span style="color: #990000">;</span> k<span style="color: #990000">++)</span>
                         acc<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">*=</span> x<span style="color: #990000">[</span>k<span style="color: #990000">]</span> <span style="color: #990000">*</span> delta_t_<span style="color: #990000">;</span>
          <span style="color: #FF0000">}</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">// check z1(T, x)</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
          ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">NearEqual</span></span><span style="color: #990000">(</span>z1<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">,</span> check_z1<span style="color: #990000">[</span>i<span style="color: #990000">],</span> acc<span style="color: #990000">[</span>i<span style="color: #990000">],</span> acc<span style="color: #990000">[</span>i<span style="color: #990000">]);</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// Now use f to compute a derivative. For this 'simple' example it is</span></span>
     <span style="font-style: italic"><span style="color: #9A1900">// the derivative of z_{n-1} (T, x) respect to x of the</span></span>
     <span style="color: #008080">a0vector</span> <span style="font-weight: bold"><span style="color: #000000">w</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">),</span> <span style="font-weight: bold"><span style="color: #000000">dw</span></span><span style="color: #990000">(</span>n_<span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> i <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> i <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> i<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     w<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">0.0</span><span style="color: #990000">;</span>
          <span style="font-weight: bold"><span style="color: #0000FF">if</span></span><span style="color: #990000">(</span> i <span style="color: #990000">==</span> n_ <span style="color: #990000">-</span> <span style="color: #993399">1</span> <span style="color: #990000">)</span>
               w<span style="color: #990000">[</span>i<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #993399">1.0</span><span style="color: #990000">;</span>
     <span style="color: #FF0000">}</span>
     dw <span style="color: #990000">=</span> f<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">Reverse</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span> w<span style="color: #990000">);</span>
     <span style="font-weight: bold"><span style="color: #0000FF">for</span></span><span style="color: #990000">(</span><span style="color: #008080">size_t</span> j <span style="color: #990000">=</span> <span style="color: #993399">0</span><span style="color: #990000">;</span> j <span style="color: #990000">&lt;</span> n_<span style="color: #990000">;</span> j<span style="color: #990000">++)</span>
     <span style="color: #FF0000">{</span>     <span style="color: #009900">double</span> check <span style="color: #990000">=</span> z1<span style="color: #990000">[</span>n_ <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">/</span> x<span style="color: #990000">[</span>j<span style="color: #990000">];</span>
          ok <span style="color: #990000">&amp;=</span> <span style="font-weight: bold"><span style="color: #000000">NearEqual</span></span><span style="color: #990000">(</span>dw<span style="color: #990000">[</span>j<span style="color: #990000">]</span> <span style="color: #990000">,</span> check<span style="color: #990000">,</span> <span style="color: #993399">100</span><span style="color: #990000">.*</span>eps<span style="color: #990000">,</span> <span style="color: #993399">100</span><span style="color: #990000">.*</span>eps<span style="color: #990000">);</span>
     <span style="color: #FF0000">}</span>
     <span style="font-style: italic"><span style="color: #9A1900">//</span></span>
     <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> ok<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre>
</font></code>
<hr/>Input File: example/atomic/ode.cpp

</body>
</html>