Fixed numerical precision problem in unit test cacsd/dscr : difference between releas...
[scilab.git] / scilab / modules / cacsd / tests / unit_tests / dscr.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) ????-2008 - INRIA - Serge Steer
4 //
5 //  This file is distributed under the same license as the Scilab package.
6 // =============================================================================
7
8 //
9 // assert_close --
10 //   Returns 1 if the two real matrices computed and expected are close,
11 //   i.e. if the relative distance between computed and expected is lesser than epsilon.
12 // Arguments
13 //   computed, expected : the two matrices to compare
14 //   epsilon : a small number
15 //
16 function flag = assert_close ( computed, expected, epsilon )
17   if expected==0.0 then
18     shift = norm(computed-expected);
19   else
20     shift = norm(computed-expected)/norm(expected);
21   end
22   if shift < epsilon then
23     flag = 1;
24   else
25     flag = 0;
26   end
27   if flag <> 1 then pause,end
28 endfunction
29
30 // Case #1
31 a=-1/3;
32 b=1;
33 c=1;
34 sl=syslin('c',a,b,c);
35 sld=dscr(sl,0.1);
36 assert_close ( sld.dt , 0.1 , %eps );
37 computed = sld.A;
38 expected = exp(sl.A*0.1);
39 assert_close ( computed , expected , %eps );
40 computed = sld.B;
41 expected = sl.A\(sld.A-eye())*sl.B;
42 assert_close ( computed , expected , %eps );
43
44 // Case #2
45 a=[0.9,0,0.6,-1.4,-4.2;
46    0.2,0.1,-0.2,0.5,0.6;
47    -4.3,0,2.2,0,2.4;
48    -3.7,-0.5,2.4,-0.6,2.7;
49    6.4,0.1,-4,-0.5,-4];
50
51 b=[-0.1,-0.1,0;
52    0,0,0.1;
53    -0.1,0.2,-0.1;
54    0.2,0.2,-0.6;
55    0.2,-0.1,0.1];
56  
57 c=[2,7,-2,5,1
58    0,-1,3,0,2];
59 d=[1,0,0
60    0,0,0];
61
62 sl=syslin('c',a,b,c,d);
63 sld=dscr(sl,0.1);
64 assert_close ( sld.dt , 0.1 , %eps );
65 computed = sld.A;
66 expected = expm(sl.A*0.1);
67 assert_close ( computed , expected , %eps );
68 computed = sld.B;
69 expected = sl.A\(sld.A-eye())*sl.B;
70 assert_close ( computed , expected , 1.e3 * %eps );
71