* Bug #11980 fixed - csim returned an error message in some cases: "Submatrix
[scilab.git] / scilab / modules / cacsd / macros / show_margins.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA -  Author: Serge Steer
3 // 
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at    
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
9
10 function show_margins(h,typ)
11   if argn(2)<2 then typ='bode',end
12   if and(typ<>['nyquist','bode']) then
13     error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
14                    "show_margins",2,'""nyquist"", ""bode""'))
15   end
16   
17   fig=gcf();
18   immediate_drawing=fig.immediate_drawing;
19   fig.immediate_drawing="off";
20
21   clf();
22   if typ=="bode" then
23     bode(h)
24     f=gcf();
25     axg=f.children(2);
26     axp=f.children(1);
27     fmin=min(axg.x_ticks.locations);
28     fmax=max(axg.x_ticks.locations);
29     gmin=min(axg.y_ticks.locations);
30     gmax=max(axg.y_ticks.locations);
31     pmin=min(axp.y_ticks.locations);
32     pmax=max(axp.y_ticks.locations);
33
34     [gm,fr]=g_margin(h)
35     sca(axp);
36     xpoly([fmin;fmax],[-180;-180])
37     e=gce();e.foreground=color('red');e.line_style=4;
38     if fr<>[] then
39       xpoly([fr;fr],[pmin;pmax])
40       e=gce();e.foreground=color('red');e.line_style=4;
41       sca(axg);
42       xpoly([fr;fr],[gmin;gmax])
43       e=gce();e.foreground=color('red');e.line_style=4;
44       xpoly([fr;fr],[-gm;0])
45       e=gce();e.foreground=color('red');e.thickness=2;
46     end
47
48     [phm,fr]=p_margin(h)
49     sca(axg);
50     xpoly([fmin;fmax],[0;0])
51     e=gce();e.foreground=color('blue');e.line_style=4;
52     if fr<>[] then
53       xpoly([fr;fr],[gmin;gmax])
54       e=gce();e.foreground=color('blue');e.line_style=4;
55       sca(axp);
56       xpoly([fr;fr],[pmin;pmax])
57       e=gce();e.foreground=color('blue');e.line_style=4;
58       xpoly([fr;fr],[-180;phm-180])
59       e=gce();e.foreground=color('blue');e.thickness=2;
60     end
61   else
62     nyquist(h)
63     ax=gca();
64     [gm,fr]=g_margin(h)
65     xpoly([min(ax.x_ticks.locations);0],[0;0]);
66     e=gce();e.foreground=color('blue');e.line_style=4;
67     if fr<>[] then
68       if h.dt=='c' then
69         f=horner(h,2*%i*%pi*fr)
70       else
71         dt=h.dt;if dt=='d' then dt=1,end
72         f=horner(h,exp(2*%i*%pi*fr*dt))
73       end
74       xpoly([real(f);0],[0;0]);
75       e=gce();e.foreground=color('blue');e.thickness=2;
76     end
77     [phm,fr]=p_margin(h)
78     //unit circle
79     t=linspace(0,2*%pi,100);
80     xpoly(sin(t),cos(t))
81     e=gce();e.foreground=color('red');e.line_style=4;
82     if fr<>[] then
83       t=phm*%pi/180+%pi;
84       xpoly([cos(t);0],[sin(t);0])
85       e=gce();e.foreground=color('red');e.thickness=2;
86     end
87   end
88   fig.immediate_drawing=immediate_drawing; 
89
90 endfunction