* Bug #11941 fixed - show_margins() failed in 'nyquist' mode if the system is
[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         if typeof(h)=='state-space' then
63             h=ss2tf(h);
64         end
65         nyquist(h)
66         ax=gca();
67         [gm,fr]=g_margin(h)
68         xpoly([min(ax.x_ticks.locations);0],[0;0]);
69         e=gce();e.foreground=color('blue');e.line_style=4;
70         if fr<>[] then
71             if h.dt=='c' then
72                 f=horner(h,2*%i*%pi*fr)
73             else
74                 dt=h.dt;if dt=='d' then dt=1,end
75                 f=horner(h,exp(2*%i*%pi*fr*dt))
76             end
77             xpoly([real(f);0],[0;0]);
78             e=gce();e.foreground=color('blue');e.thickness=2;
79         end
80         [phm,fr]=p_margin(h)
81         //unit circle
82         t=linspace(0,2*%pi,100);
83         xpoly(sin(t),cos(t))
84         e=gce();e.foreground=color('red');e.line_style=4;
85         if fr<>[] then
86             t=phm*%pi/180+%pi;
87             xpoly([cos(t);0],[sin(t);0])
88             e=gce();e.foreground=color('red');e.thickness=2;
89         end
90     end
91     fig.immediate_drawing=immediate_drawing; 
92
93 endfunction