fix for bug 13816
[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 // Copyright (C) 2012 - 2016 - Scilab Enterprises
5 //
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
12
13 function show_margins(h,typ)
14     if argn(2)<2 then typ="bode",end
15     if and(typ<>["nyquist","bode"]) then
16         error(msprintf(_("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
17         "show_margins",2,"""nyquist"", ""bode"""))
18     end
19
20     fig=gcf();
21     immediate_drawing=fig.immediate_drawing;
22     fig.immediate_drawing="off";
23
24     clf();
25     if typ=="bode" then
26         bode(h)
27         f=gcf();
28         axg=f.children(2);
29         axp=f.children(1);
30         fmin=min(axg.x_ticks.locations);
31         fmax=max(axg.x_ticks.locations);
32         gmin=min(axg.y_ticks.locations);
33         gmax=max(axg.y_ticks.locations);
34         pmin=min(axp.y_ticks.locations);
35         pmax=max(axp.y_ticks.locations);
36
37         [gm,fr]=g_margin(h)
38         sca(axp);
39         xpoly([fmin;fmax],[-180;-180])
40         e=gce();e.foreground=color("red");e.line_style=4;
41         if fr<>[]&fr>0 then
42             xpoly([fr;fr],[pmin;pmax])
43             e=gce();e.foreground=color("red");e.line_style=4;
44             sca(axg);
45             xpoly([fr;fr],[gmin;gmax])
46             e=gce();e.foreground=color("red");e.line_style=4;
47             xpoly([fr;fr],[-gm;0])
48             e=gce();e.foreground=color("red");e.thickness=2;
49         end
50
51         [phm,fr]=p_margin(h)
52         sca(axg);
53         xpoly([fmin;fmax],[0;0])
54         e=gce();e.foreground=color("blue");e.line_style=4;
55         if fr<>[]&fr>0 then
56             xpoly([fr;fr],[gmin;gmax])
57             e=gce();e.foreground=color("blue");e.line_style=4;
58             sca(axp);
59             xpoly([fr;fr],[pmin;pmax])
60             e=gce();e.foreground=color("blue");e.line_style=4;
61             xpoly([fr;fr],[-180;phm-180])
62             e=gce();e.foreground=color("blue");e.thickness=2;
63         end
64     else
65         if typeof(h)=="state-space" then
66             h=ss2tf(h);
67         end
68         nyquist(h)
69         ax=gca();
70         [gm,fr]=g_margin(h)
71         xpoly([min(ax.x_ticks.locations);0],[0;0]);
72         e=gce();e.foreground=color("blue");e.line_style=4;
73         if fr<>[] then
74             if h.dt=="c" then
75                 f=horner(h,2*%i*%pi*fr)
76             else
77                 dt=h.dt;if dt=="d" then dt=1,end
78                 f=horner(h,exp(2*%i*%pi*fr*dt))
79             end
80             xpoly([real(f);0],[0;0]);
81             e=gce();e.foreground=color("blue");e.thickness=2;
82         end
83         [phm,fr]=p_margin(h)
84         //unit circle
85         t=linspace(0,2*%pi,100);
86         xpoly(sin(t),cos(t))
87         e=gce();e.foreground=color("red");e.line_style=4;
88         if fr<>[] then
89             t=phm*%pi/180+%pi;
90             xpoly([cos(t);0],[sin(t);0])
91             e=gce();e.foreground=color("red");e.thickness=2;
92         end
93     end
94     fig.immediate_drawing=immediate_drawing;
95
96 endfunction