-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_Deriv.m
844 lines (727 loc) · 29.2 KB
/
test_Deriv.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
function [ success ] = test_Deriv( )
% Runs a series of finite difference tests to verify the Deriv Matlab class.
step = 1e-9; % finite difference step size
tol = 2.2e-6; % relative tolerance that must be met to pass the test
verbose = false; % flag that determines the output level
success = true;
tcase.arithmetic = 0;
tcase.array_manip = 0;
tcase.sparse = 0;
tcase.chol = 0; % matrix factorizations / equations
tcase.eig = 0;
tcase.lqr = 0;
tcase.lyap = 0;
tcase.norm = 0;
tcase.hess = 0;
tcase.qr = 0;
tcase.svd = 0;
tcase.abs = 0; % nondifferentiable Matlab functions
tcase.interp1 = 0;
tcase.ode = 1;
%% ---------------------------------------------------------------------------
% Testing arithmetic operations
%%----------------------------------------------------------------------------
if (tcase.arithmetic)
fprintf(' Testing arithmetic operations\n')
fprintf(' ldivide:\n')
n = 3;
A = Deriv( rand(n,n), rand(n,n) );
B = Deriv( rand(n,n), rand(n,n) );
c = Deriv( rand(n,1), rand(n,1) );
Ap = Get_value(A) + step*Get_deriv(A);
Bp = Get_value(B) + step*Get_deriv(B);
cp = Get_value(c) + step*Get_deriv(c);
C = rand(n,n);
D = A.\B; Dp = Ap.\Bp;
rel_error1 = norm( (Dp-Get_value(D))/step - Get_deriv(D) )/norm( Get_deriv(D) );
D = A.\C; Dp = Ap.\C;
rel_error2 = norm( (Dp-Get_value(D))/step - Get_deriv(D) )/norm( Get_deriv(D) );
D = C.\A; Dp = C.\Ap;
rel_error3 = norm( (Dp-Get_value(D))/step - Get_deriv(D) )/norm( Get_deriv(D) );
if ( verbose )
fprintf(' Deriv ldivide Deriv test: the relative error is %g\n',rel_error1);
fprintf(' Deriv ldivide double test: the relative error is %g\n',rel_error2);
fprintf(' double ldivide Deriv test: the relative error is %g\n',rel_error3);
end
if ( rel_error1 > tol || rel_error2 > tol )
fprintf(' ldivide test failed\n')
fprintf(' Deriv ldivide Deriv test: relative error %g\n',rel_error1);
fprintf(' Deriv ldivide double test: relative error %g\n',rel_error2);
fprintf(' double ldivide Deriv test: relative error %g\n',rel_error3);
success = false;
end
fprintf(' cumprod:\n')
d = cumprod(c); dp = cumprod(cp);
rel_error1 = norm( (dp-Get_value(d))/step - Get_deriv(d) )/norm( Get_deriv(d) );
d = cumprod(A,1); dp = cumprod(Ap,1);
rel_error2 = norm( (dp-Get_value(d))/step - Get_deriv(d) )/norm( Get_deriv(d) );
d = cumprod(A,2); dp = cumprod(Ap,2);
rel_error3 = norm( (dp-Get_value(d))/step - Get_deriv(d) )/norm( Get_deriv(d) );
if ( verbose )
fprintf(' vector test: the relative error is %g\n',rel_error1);
fprintf(' matrix test: the relative error is %g\n',rel_error2);
fprintf(' matrix test: the relative error is %g\n',rel_error3);
end
if ( rel_error1 > tol || rel_error2 > tol || rel_error3 > tol )
fprintf(' cumprod test failed\n')
fprintf(' vector test: relative error %g\n',rel_error1);
fprintf(' matrix test: relative error %g\n',rel_error2);
fprintf(' matrix test: relative error %g\n',rel_error3);
success = false;
end
fprintf(' prod:\n')
d = prod(c); dp = prod(cp);
rel_error1 = norm( (dp-Get_value(d))/step - Get_deriv(d) )/norm( Get_deriv(d) );
if ( verbose )
fprintf(' vector test: the relative error is %g\n',rel_error1);
end
if ( rel_error1 > tol )
fprintf(' prod test failed\n')
fprintf(' vector test: relative error %g\n',rel_error1);
success = false;
end
fprintf(' diff:\n')
d = diff(c); dp = diff(cp);
rel_error1 = norm( (dp-Get_value(d))/step - Get_deriv(d) )/norm( Get_deriv(d) );
if ( verbose )
fprintf(' standard test: the relative error is %g\n',rel_error1);
end
fprintf('\n')
end
%% ---------------------------------------------------------------------------
% Testing Array Manipulations
%%----------------------------------------------------------------------------
if ( tcase.array_manip )
fprintf(' Testing array manipulations\n');
Ax = rand(2,1); Adx = rand(2,1); A = Deriv(Ax,Adx);
Bx = rand(2,3); Bdx = rand(2,3); B = Deriv(Bx,Bdx);
Cx = rand(2,2); Cdx = rand(2,2); C = Deriv(Cx,Cdx);
fprintf(' cat:\n')
M = cat(2,A,B,C);
Mx = cat(2,Ax,Bx,Cx);
Mdx = cat(2,Adx,Bdx,Cdx);
error1 = norm( Get_value(M)-Mx );
error2 = norm( Get_deriv(M)-Mdx );
if ( verbose )
fprintf(' cat test: the error is %g\n',error1);
fprintf(' cat test: the error is %g\n',error2);
end
if ( error1 > tol || error2 > tol )
fprintf(' cat test failed\n')
fprintf(' first test: error %g\n',error1);
fprintf(' second test: error %g\n',error2);
success = false;
end
fprintf(' horzcat:\n');
M = [A B C C C ];
Mx = [Ax Bx Cx Cx Cx ];
Mdx = [Adx Bdx Cdx Cdx Cdx];
error1 = norm( Get_value(M)-Mx );
error2 = norm( Get_deriv(M)-Mdx );
if ( verbose )
fprintf(' cat test: the error is %g\n',error1);
fprintf(' cat test: the error is %g\n',error2);
end
if ( error1 > tol || error2 > tol )
fprintf(' cat test failed\n')
fprintf(' first test: error %g\n',error1);
fprintf(' second test: error %g\n',error2);
success = false;
end
fprintf(' vertcat:\n');
M = [A' ; B' ; C ; C ; C ];
Mx = [Ax' ; Bx' ; Cx ; Cx ; Cx ];
Mdx = [Adx' ; Bdx'; Cdx; Cdx; Cdx];
error1 = norm( Get_value(M)-Mx );
error2 = norm( Get_deriv(M)-Mdx );
if ( verbose )
fprintf(' cat test: the error is %g\n',error1);
fprintf(' cat test: the error is %g\n',error2);
end
if ( error1 > tol || error2 > tol )
fprintf(' cat test failed\n')
fprintf(' first test: error %g\n',error1);
fprintf(' second test: error %g\n',error2);
success = false;
end
fprintf('\n')
end
%% ---------------------------------------------------------------------------
% Testing Matrix Functions
%%----------------------------------------------------------------------------
% CHOL
%-----------------------------------------------------------------------------
if (tcase.chol || tcase.eig || tcase.lqr || tcase.lyap || tcase.hess || ...
tcase.qr || tcase.svd )
fprintf(' Testing matrix factorizations\n');
end
if (tcase.chol)
fprintf(' chol:\n')
n = 50;
X = randn(n,n); Y = randn(n,n);
% symmetric case (differentiable)
fprintf(' symmetric derivative case\n')
Ax = X*X'; Adx = Y+Y'; % subcase1: symmetric matrix derivative
A = Deriv( Ax, Adx );
[R] = chol(A);
[Rp] = chol(Ax + step*Adx);
rel_error1s = norm( (Rp-Get_value(R))/step - Get_deriv(R) )/norm( Get_deriv(R) );
% Adx = Y; % subcase2: nonsymmetric matrix derivative
% fprintf(' nonsymmetric derivative case\n')
% A = Deriv( Ax, Adx );
%
% [R] = chol(A);
% [Rp] = chol(Ax + step*Adx);
% rel_error1n = norm( (Rp-Get_value(R))/step - Get_deriv(R) )/norm( Get_deriv(R) );
if ( verbose )
fprintf(' first chol test: the relative error is %g\n',rel_error1s);
% fprintf(' second chol test: the relative error is %g\n',rel_error1n);
end
if ( rel_error1s > tol ) %|| rel_error1n > tol )
fprintf(' first chol test: failed with relative error %g\n',rel_error1s);
% fprintf(' second chol test: failed with relative error %g\n',rel_error1n);
success = false;
end
fprintf('\n')
end
% EIG
%-----------------------------------------------------------------------------
if (tcase.eig)
fprintf(' eig:\n')
n = 5;
X = randn(n,n); Y = randn(n,n);
% symmetric case (differentiable)
fprintf(' symmetric case\n')
A = Deriv( X+X', Y+Y' ); % subcase 1: symmetric matrix derivative
Ax = Get_value(A);
Adx = Get_deriv(A);
[V,D] = eig(A);
[Vp,Dp] = eig( Ax + step*Adx );
rel_error1d = norm( (Dp-Get_value(D))/step - Get_deriv(D) )/norm( Get_deriv(D) );
rel_error1v = norm( (Vp-Get_value(V))/step - Get_deriv(V) )/norm( Get_deriv(V) );
A = Deriv( X+X', Y ); % subcase 2: nonsymmetric matrix derivative
Ax = Get_value(A);
Adx = Get_deriv(A);
[V,D] = eig(A);
[Vp,Dp] = eig( Ax + step*Adx );
% since Ax+step*Adx is not symmetric, we must sort the eigenvalues for proper fd
[~,index] = sort( diag(Dp) );
Vp = Vp(:,index); Dp = Dp(index,index);
Vx = Get_value(V);
Vp = Vp*diag( sign(Vx(1,:)./Vp(1,:)) );
rel_error2d = norm( (Dp-Get_value(D))/step - Get_deriv(D) )/norm( Get_deriv(D) );
rel_error2v = norm( (Vp-Get_value(V))/step - Get_deriv(V) )/norm( Get_deriv(V) );
% B=Get_deriv(V)
% Ax*B+Adx*Get_value(V) - B*Get_value(D) - Get_value(V)*Get_deriv(D)
if ( verbose )
fprintf(' first eig test: the relative error is %g\n',rel_error1d);
fprintf(' the relative error is %g\n',rel_error1v);
fprintf(' second eig test: the relative error is %g\n',rel_error2d);
fprintf(' the relative error is %g\n',rel_error2v);
end
if ( rel_error1d > tol || rel_error1v > tol )
fprintf(' first eig test: failed with relative error %g\n',rel_error1d);
fprintf(' %g\n',rel_error1v);
success = false;
end
if ( rel_error2d > tol || rel_error2v > tol )
fprintf(' second eig test: failed with relative error %g\n',rel_error2d);
fprintf(' %g\n',rel_error2v);
success = false;
end
end
% LQR
%-----------------------------------------------------------------------------
if (tcase.lqr)
fprintf(' lqr:\n')
n = 30;
m = 3;
p = 4;
dd = n*step;
A = Deriv( rand(n,n), rand(n,n) );
B = Deriv( rand(n,m), rand(n,m) );
C = Deriv( rand(p,n), rand(p,n) );
Q = C'*C;
R = Deriv( rand(m,m), rand(m,m) );
R = R'*R;
N = Deriv( rand(n,m), rand(n,m) );
[K,S,E] = lqr(A,B,Q,R);
[Kp,Sp,Ep] = lqr(Get_value(A)+dd*Get_deriv(A),Get_value(B)+dd*Get_deriv(B),...
Get_value(Q)+dd*Get_deriv(Q),Get_value(R)+dd*Get_deriv(R));
rel_error1 = norm( Get_deriv(K) - ( Kp-Get_value(K) )/dd ) / norm( Get_deriv(K) );
rel_error2 = norm( Get_deriv(S) - ( Sp-Get_value(S) )/dd ) / norm( Get_deriv(S) );
if ( verbose )
fprintf(' first lqr test: the relative error is %g\n',rel_error1);
fprintf(' the relative error is %g\n',rel_error2);
end
if ( rel_error1 > tol || rel_error2 > tol )
fprintf(' first lqr test: failed with relative error %g\n',rel_error1);
fprintf(' %g\n',rel_error2);
success = false;
end
end
% LYAP
%-----------------------------------------------------------------------------
if (tcase.lyap)
fprintf(' lyap:\n')
n = 4;
A = Deriv( rand(n,n), rand(n,n) );
B = Deriv( rand(n,n), rand(n,n) );
C = Deriv( rand(n,n), rand(n,n) );
D = Deriv( rand(n,n), rand(n,n) );
X = lyap(A,B);
Xp = lyap( Get_value(A)+step*Get_deriv(A), Get_value(B)+step*Get_deriv(B) );
rel_error = norm( Get_deriv(X) - ( Xp-Get_value(X) )/step ) / norm( Get_deriv(X) );
if ( verbose )
fprintf(' first lyap test: the relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' first lyap test: failed with relative error %g\n',rel_error);
success = false;
end
X = lyap(A,B,C);
Xp = lyap( Get_value(A)+step*Get_deriv(A), Get_value(B)+step*Get_deriv(B), ...
Get_value(C)+step*Get_deriv(C) );
rel_error = norm( Get_deriv(X) - ( Xp-Get_value(X) )/step ) / norm( Get_deriv(X) );
if ( verbose )
fprintf(' second lyap test: the relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' second lyap test: failed with relative error %g\n',rel_error);
success = false;
end
B = B+B'; % must be self-adjoint
X = lyap(A,B,[],D);
Xp = lyap( Get_value(A)+step*Get_deriv(A), Get_value(B)+step*Get_deriv(B), [], Get_value(D)+step*Get_deriv(D) );
rel_error = norm( Get_deriv(X) - ( Xp-Get_value(X) )/step ) / norm( Get_deriv(X) );
if ( verbose )
fprintf(' third lyap test: the relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' third lyap test: failed with relative error %g\n',rel_error);
success = false;
end
fprintf('\n')
end
% HESS
%-----------------------------------------------------------------------------
if (tcase.hess)
fprintf(' hess:\n')
m = 5;
Ax = randn(m,m); Adx = randn(m,m);
A = Deriv( Ax, Adx );
[P,H] = hess(A);
[Pp,Hp] = hess(Ax+step*Adx);
Hprime = ( Hp-Get_value(H) )/step;
Pprime = ( Pp-Get_value(P) )/step;
rel_error1 = norm( Get_deriv(P)'*Get_value(P) + Get_value(P)'*Get_deriv(P) );
rel_error2 = norm( Pprime-Get_deriv(P) ) / norm( Get_deriv(P) );
rel_error3 = norm( Hprime-Get_deriv(H) ) / norm( Get_deriv(H) );
if ( verbose )
fprintf(' first hess test: the relative error is %g\n',rel_error1);
fprintf(' the relative error is %g\n',rel_error2);
fprintf(' the relative error is %g\n',rel_error3);
end
if ( rel_error1 > tol || rel_error2 > tol || rel_error3 > tol )
fprintf(' first hess test: failed with relative error %g\n',rel_error1);
fprintf(' failed with relative error %g\n',rel_error2);
fprintf(' failed with relative error %g\n',rel_error3);
success = false;
end
% testing one output argument.
H = hess(A);
Hp = hess(Ax+step*Adx);
Hprime = ( Hp-Get_value(H) )/step;
rel_error3 = norm( Hprime-Get_deriv(H) ) / norm( Get_deriv(H) );
if ( rel_error3 > tol )
fprintf(' second hess test: failed with relative error %g\n',rel_error3);
success = false;
end
fprintf('\n')
end
% QR
%-----------------------------------------------------------------------------
if (tcase.qr)
fprintf(' qr:\n')
m = 5;
n = 5;
A = Deriv( rand(m,n), rand(m,n) );
[Q,R] = qr(A);
[~,Rp] = qr(Get_value(A)+step*Get_deriv(A));
Rprime = ( Rp-Get_value(R) )/step;
rel_error1 = norm( Get_deriv(Q)'*Get_value(Q) + Get_value(Q)'*Get_deriv(Q) );
rel_error2 = norm( Get_deriv(R) - Rprime) / norm( Get_deriv(R) );
if ( verbose )
fprintf(' first qr test: the relative error is %g\n',rel_error1);
fprintf(' the relative error is %g\n',rel_error2);
end
if ( rel_error1 > tol || rel_error2 > tol )
fprintf(' first qr test: failed with relative error %g\n',rel_error1);
fprintf(' %g\n',rel_error2);
success = false;
end
fprintf('\n')
end
% SVD
%-----------------------------------------------------------------------------
if (tcase.svd)
fprintf(' svd:\n')
m = 10;
n = 7;
r = min(m,n);
A = Deriv( rand(m,n), rand(m,n) );
[U,S,V] = svd(Get_value(A));
[Up,Sp,Vp] = svd(Get_value(A)+1e-8*Get_deriv(A));
Ud = (Up-U)/1e-8; Sd = (Sp-S)/1e-8; Vd = (Vp-V)/1e-8;
rel_error1 = abs( (U(:,1)'*Get_deriv(A)*V(:,1)) - Sd(1,1) )/abs( Sd(1,1) );
rel_error2 = abs( (U(:,2)'*Get_deriv(A)*V(:,2)) - Sd(2,2) )/abs( Sd(2,2) );
rel_error3 = abs( (U(:,r)'*Get_deriv(A)*V(:,r)) - Sd(r,r) )/abs( Sd(r,r) );
if ( verbose )
fprintf(' the first svd test: the relative error is %g\n',rel_error1);
fprintf(' %g\n',rel_error2);
fprintf(' %g\n',rel_error3);
end
if ( rel_error1 > tol || rel_error2 > tol || rel_error3 > tol )
fprintf(' first svd test: failed with relative error %g\n',rel_error1);
fprintf(' %g\n',rel_error2);
fprintf(' %g\n',rel_error3);
success = false;
end
fprintf('\n')
end
% ABS
%-----------------------------------------------------------------------------
if (tcase.abs)
fprintf(' Testing abs()\n')
n = 10;
c = Deriv( rand(n,1)-.5, rand(n,1)-.5 );
c = abs(c);
rel_error1 = norm( ( abs(Get_value(c)+1e-7*Get_deriv(c))-abs(Get_value(c)))/1e-7 - Get_deriv(c) )/norm( abs(Get_value(c)) );
% ( abs(Get_value(c)+1e-7*Get_deriv(c))-abs(Get_value(c)))/1e-7
% Get_deriv(c)
if ( verbose )
fprintf(' the first abs test: the relative error is %g\n',rel_error1);
end
if ( rel_error1 > tol )
fprintf(' first abs test: failed with relative error %g\n',rel_error1);
success = false;
end
fprintf('\n')
end
%% ---------------------------------------------------------------------------
% Testing Matrix Functions
% NORM
%-----------------------------------------------------------------------------
if (tcase.norm)
fprintf(' norm: matrix cases\n')
n = 5;
A = Deriv( rand(n,n), rand(n,n) );
Ap = Get_value(A)+step*Get_deriv(A);
x = norm(A,'fro');
rel_error = abs(( (norm(Ap,'fro')-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Matrix ''fro'' norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Matrix ''fro'' norm: failed with relative error %g\n',rel_error);
success = false;
end
x = norm(A,1);
rel_error = abs(( (norm(Ap,1)-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Matrix 1 norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Matrix 1 norm: failed with relative error %g\n',rel_error);
success = false;
end
x = norm(A,2);
rel_error = abs(( (norm(Ap,2)-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Matrix 2 norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Matrix 2 norm: failed with relative error %g\n',rel_error);
success = false;
end
x = norm(A,inf);
rel_error = abs(( (norm(Ap,inf)-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Matrix inf norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Matrix inf norm: failed with relative error %g\n',rel_error);
success = false;
end
fprintf(' norm: vector cases\n')
v = Deriv( randn(n,1), randn(n,1) );
vp = Get_value(v) + step*Get_deriv(v);
x = norm(v,1);
rel_error = abs(( (norm(vp,1)-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Vector 1 norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Vector 1 norm: failed with relative error %g\n',rel_error);
success = false;
end
x = norm(v,2);
rel_error = abs(( (norm(vp,2)-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Vector 2 norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Vector 2 norm: failed with relative error %g\n',rel_error);
success = false;
end
p = 4.5;
x = norm(v,p);
rel_error = abs(( (norm(vp,p)-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Vector p norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Vector p norm: failed with relative error %g\n',rel_error);
success = false;
end
x = norm(v,Inf);
rel_error = abs(( (norm(vp,Inf)-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Vector Inf norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Vector Inf norm: failed with relative error %g\n',rel_error);
success = false;
end
x = norm(v,-Inf);
rel_error = abs(( (norm(vp,-Inf)-Get_value(x) )/step - Get_deriv(x) )/ Get_deriv(x));
if ( verbose )
fprintf(' Vector -Inf norm: relative error is %g\n',rel_error);
end
if ( rel_error > tol )
fprintf(' Vector -Inf norm: failed with relative error %g\n',rel_error);
success = false;
end
fprintf('\n')
end
%%----------------------------------------------------------------------------
% INTERP1
%-----------------------------------------------------------------------------
if (tcase.interp1)
fprintf(' Testing interpolation operations\n')
fprintf(' interp1:\n')
x = linspace(0,pi,20);
y = Deriv( cos(x), -sin(x) );
xi = linspace(0,pi,5);
% nearest neighbor case (note that finite difference fails if
% x = linspace(0,pi,20); and xi = linspace(0,pi,5); above)
yi = interp1(x,y,xi,'nearest');
yip = interp1(x,Get_value(y)+step*Get_deriv(y),xi,'nearest');
rel_error1 = norm( (yip-Get_value(yi))/step - Get_deriv(yi) )/norm( Get_deriv(yi) );
if ( verbose )
fprintf(' nearest test: the relative error is %g\n',rel_error1);
end
% linear case (note that finite difference fails if
% x = linspace(0,pi,20); and xi = linspace(0.pi,5); above)
yi = interp1(x,y,xi,'linear');
yip = interp1(x,Get_value(y)+step*Get_deriv(y),xi,'linear');
rel_error2 = norm( (yip-Get_value(yi))/step - Get_deriv(yi) )/norm( Get_deriv(yi) );
if ( verbose )
fprintf(' linear test: the relative error is %g\n',rel_error2);
end
% spline case
yi = interp1(x,y,xi,'spline');
% Get_value(yi)
% interp1(x,Get_value(y),xi,'spline')
yip = interp1(x,Get_value(y)+1000000*step*Get_deriv(y),xi,'spline');
% Get_deriv(yi)
% (yip-Get_value(yi))/(1000000*step)
% norm( (yip-Get_value(yi))/(1000000*step) - Get_deriv(yi) )
rel_error3 = norm( (yip-Get_value(yi))/(1000000*step) - Get_deriv(yi) )/norm( Get_deriv(yi) );
if ( verbose )
fprintf(' spline test: the relative error is %g\n',rel_error3);
end
fprintf('\n')
end
%% ---------------------------------------------------------------------------
% Testing ode/dae solvers
%-----------------------------------------------------------------------------
if (tcase.ode)
fprintf(' Testing ode solvers\n')
% ODE23
%---------------------------------------------------------------------------
fprintf(' ode23:\n')
tspan = [0,1];
% y0 = Deriv(3,1);
% ydot = @(t,y)(3*y);
% [T,Y] = ode23(ydot,tspan,y0);
% plot(T,Get_value(Y),'+',T,3*exp(3*T),'-')
% hold on
% plot(T,Get_deriv(Y),'b*',T,exp(3*T),'-')
% pause
% a = Set_variable(4);
% y0 = a;
% ydot = @(t,y)(a*y);
% [T,Y] = ode23(ydot,tspan,y0);
% plot(T,Get_value(Y),'+',T,Get_value(a)*exp(Get_value(a)*T))
% hold on
% plot(T,Get_deriv(Y),'b+',T,exp(Get_value(a)*T)+Get_value(a)^2*exp(Get_value(a)*T))
% pause
y0 = Deriv(.999,.0000001);
% y0 = Deriv(.999,1);
y0x = Get_value(y0);
y0dx = Get_deriv(y0);
ydot = @(t,y)(y.^2);
[T,Y] = ode23(ydot,tspan,y0);
[Tp,Yp] = ode23(ydot,tspan,y0x+step*y0dx);
Yp_dot = feval(ydot,Tp,Yp);
Yp = Yp - Yp_dot.*(Tp-T);
fd = ( Yp-Get_value(Y) )/step;
Sexact = ( y0x*T./(1-y0x*T).^2 + 1./(1-y0x*T) )*y0dx;
semilogy(T,Get_deriv(Y),'b+',T,fd,'r^',T,Sexact,'k-')
legend('Deriv','Finite Differences','Exact')
title('Differentiating ode23')
xlabel('x'); ylabel('\partial y/\partial y_0')
fprintf('\n')
% figure
% hold on
% plot(Tp,'ko')
% plot(T,'b+')
fprintf(' ode45:\n')
figure
[T ,Y ] = ode45(ydot,tspan,y0 );
[Tp,Yp] = ode45(ydot,tspan,y0x+step*y0dx);
Yp_dot = feval(ydot,Tp,Yp);
Yp = Yp - Yp_dot.*(Tp-T);
fd = ( Yp-Get_value(Y) )/step;
Sexact = ( y0x*T./(1-y0x*T).^2 + 1./(1-y0x*T) )*y0dx;
semilogy(T,Get_deriv(Y),'b+',T,fd,'r^',T,Sexact,'k-')
legend('Deriv','Finite Differences','Exact')
title('Differentiating ode45')
xlabel('x'); ylabel('\partial y/\partial y_0')
fprintf('\n')
% figure
% hold on
% plot(Tp,'ko')
% plot(T,'b+')
%---------------------------------------------------------------------------
% Testing ODE Solvers Using y' = y^2, y(0)=y0 s = dy/d y0.
% Therefore, y(t)=y0/(1-y0*t) and s(t) = y0*t/(1-y0*t)^2 + 1/(1-y0*t)
%---------------------------------------------------------------------------
% Testing ode23:
%---------------------------------------------------------------------------
fprintf(' Testing ode23:\n')
tspan = [0,.1];
y0 = Deriv(.999,1);
y0x = Get_value(y0);
y0dx = Get_deriv(y0);
ydot = @(t,y)(y.^2);
[Tode23,Yode23] = ode23(ydot,tspan,y0);
yDeriv = Get_value(Yode23);
sDeriv = Get_deriv(Yode23);
sExact = zeros(size(Yode23));
for i=1:length(Tode23)
sExact(i) = (y0x*Tode23(i))./(1-y0x*Tode23(i)).^2 + 1./(1-y0x*Tode23(i));
end
fprintf(' T Sode23 sExact \n')
for i=1:length(Tode23)
fprintf(' %12.4f %12.8f %12.8f\n',Tode23(i),sDeriv(i),sExact(i));
end
fprintf('\n\n')
% %---------------------------------------------------------------------------
% % Testing ode113:
% %---------------------------------------------------------------------------
% fprintf(' Testing ode113:\n')
% [Tode113,Yode113] = ode113Test1(ydot,tspan,y0);
% yDeriv = Get_value(Yode113);
% sDeriv = Get_deriv(Yode113);
%
% sExact = zeros(size(Yode113));
% for i=1:length(Tode113)
% sExact(i) = (y0x*Tode113(i))./(1-y0x*Tode113(i)).^2 + 1./(1-y0x*Tode113(i));
% end
%
% fprintf(' T Sode113 sExact \n')
% for i=1:length(Tode113)
% fprintf(' %12.4f %12.8f %12.8f\n',Tode113(i),sDeriv(i),sExact(i));
% end
%
% fprintf('\n\n')
%---------------------------------------------------------------------------
% Testing ode23s:
%---------------------------------------------------------------------------
fprintf(' Testing ode23s:\n')
[Tode23s,Yode23s] = ode23s(ydot,tspan,y0);
yDeriv = Get_value(Yode23s);
sDeriv = Get_deriv(Yode23s);
sExact = zeros(size(Yode23s));
for i=1:length(Tode23s)
sExact(i) = (y0x*Tode23s(i))./(1-y0x*Tode23s(i)).^2 + 1./(1-y0x*Tode23s(i));
end
fprintf(' T Sode23s sExact \n')
for i=1:length(Tode23s)
fprintf(' %12.4f %12.8f %12.8f\n',Tode23s(i),sDeriv(i),sExact(i));
end
fprintf('\n\n')
%---------------------------------------------------------------------------
% Testing ode23t:
%---------------------------------------------------------------------------
fprintf(' Testing ode23t:\n')
[Tode23t,Yode23t] = ode23t(ydot,tspan,y0);
yDeriv = Get_value(Yode23t);
sDeriv = Get_deriv(Yode23t);
sExact = zeros(size(Yode23t));
for i=1:length(Tode23t)
sExact(i) = (y0x*Tode23t(i))./(1-y0x*Tode23t(i)).^2 + 1./(1-y0x*Tode23t(i));
end
fprintf(' T Sode23s sExact \n')
for i=1:length(Tode23t)
fprintf(' %12.4f %12.8f %12.8f\n',Tode23t(i),sDeriv(i),sExact(i));
end
fprintf('\n\n')
%---------------------------------------------------------------------------
% Testing ode23tb:
%---------------------------------------------------------------------------
fprintf(' Testing ode23tb:\n')
[Tode23tb,Yode23tb] = ode23tb(ydot,tspan,y0);
yDeriv = Get_value(Yode23tb);
sDeriv = Get_deriv(Yode23tb);
sExact = zeros(size(Yode23tb));
for i=1:length(Tode23tb)
sExact(i) = (y0x*Tode23tb(i))./(1-y0x*Tode23tb(i)).^2 + 1./(1-y0x*Tode23tb(i));
end
fprintf(' T Sode23s sExact \n')
for i=1:length(Tode23tb)
fprintf(' %12.4f %12.8f %12.8f\n',Tode23tb(i),sDeriv(i),sExact(i));
end
fprintf('\n\n')
%---------------------------------------------------------------------------
% Testing ode45:
%---------------------------------------------------------------------------
fprintf(' Testing ode45:\n')
[Tode45,Yode45] = ode45(ydot,tspan,y0);
yDeriv = Get_value(Yode45);
sDeriv = Get_deriv(Yode45);
sExact = zeros(size(Yode45));
for i=1:length(Tode45)
sExact(i) = (y0x*Tode45(i))./(1-y0x*Tode45(i)).^2 + 1./(1-y0x*Tode45(i));
end
fprintf(' T Sode23 sExact \n')
for i=1:length(Tode45)
fprintf(' %12.4f %12.8f %12.8f\n',Tode45(i),sDeriv(i),sExact(i));
end
fprintf('\n\n')
end
%%
if ( success )
fprintf(' Deriv passed all of the following tests\n')
if (tcase.arithmetic), fprintf(' arithmetic\n'); end
if (tcase.eig ), fprintf(' eig\n'); end
if (tcase.interp1 ), fprintf(' interp1\n'); end
if (tcase.lqr ), fprintf(' lqr\n'); end
if (tcase.lyap ), fprintf(' lyap\n'); end
if (tcase.norm ), fprintf(' norm\n'); end
if (tcase.qr ), fprintf(' qr\n'); end
if (tcase.svd ), fprintf(' svd\n'); end
end
end