Graphics macros & demos: replace gcf with scf
[scilab.git] / scilab / modules / cacsd / macros / zgrid.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2005 - INRIA - Farid Belahcene
3 // Copyright (C) 2010 - INRIA - Serge STEER
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.1-en.txt
9
10 function []=zgrid(varargin)
11     // zgrid(["new",] [,Z,Wn [,colors]])
12     // zgrid(Z,Wn [,"new"] [,colors])
13     defaultcolors=[4 12];
14     defaultbounds=[-1,-1;1,1];
15     rhs=argn(2)
16     new=%f
17     if rhs>=1 then
18         if type(varargin(1))==10 then
19             if varargin(1)<>"new" then
20                 error(msprintf(_("%s: Wrong value for input argument #%d: ''%s'' expected.\n"),"zgrid",1,"new"))
21             end
22             varargin(1)=null()
23             rhs=rhs-1
24             new=%t
25         end
26     end
27     if rhs>=3 then
28         if type(varargin(3))==10 then
29             if varargin(3)<>"new" then
30                 error(msprintf(_("%s: Wrong value for input argument #%d: ''%s'' expected.\n"),"zgrid",3,"new"))
31             end
32             varargin(3)=null()
33             rhs=rhs-1
34             new=%t
35         end
36     end
37
38     defaultwn=0:0.1:1;
39     defaultzeta=0:0.1:1;
40
41     select rhs
42     case 0 then  //zgrid() or zgrid("new")
43         wn=defaultwn
44         zeta = defaultzeta
45         colors = defaultcolors
46         fig = scf();
47         my_plot_desc    = "zgrid";
48         fig.figure_name = my_plot_desc;
49         xtitle(my_plot_desc," "," "," ");
50     case 1 then
51         zeta =  varargin(1)
52         if type(zeta)<>1|~isreal(zeta) then
53             error(msprintf(_("%s: Wrong type for input argument #%d : real floating point array expected\n"),"zgrid",1));
54         end
55         wn=defaultwn
56         colors = defaultcolors
57     case 2 then
58         zeta =  varargin(1)
59         if type(zeta)<>1|~isreal(zeta) then
60             error(msprintf(_("%s: Wrong type for input argument #%d : real floating point array expected\n"),"zgrid",1));
61         end
62         wn = varargin(2)
63         if type(wn)<>1|~isreal(wn) then
64             error(msprintf("%s: Wrong type for input argument #%d : real floating point array expected\n"),"zgrid",2);
65         end
66         colors = defaultcolors
67     case 3 then
68         zeta =  varargin(1)
69         if type(zeta)<>1|~isreal(zeta) then
70             error(msprintf(_("%s: Wrong type for input argument #%d : real floating point array expected\n"),"zgrid",1));
71         end
72         wn = varargin(2)
73         if type(wn)<>1|~isreal(wn) then
74             error(msprintf(_("%s: Wrong type for input argument #%d : real floating point array expected\n"),"zgrid",2));
75         end
76         colors = varargin(3);
77         if type(colors)<>1|~isreal(colors) then
78             error(msprintf(_("%s: Wrong type for input argument #%d : real floating point array expected\n"),"zgrid",3));
79         end
80         if size(colors,"*")==1 then colors=colors*ones(1,2),end
81     end
82     wn=wn(wn>0&wn<=1);
83     zeta=zeta(zeta>=0&zeta<=1);
84
85     fig = gcf();
86     immediate_drawing=fig.immediate_drawing;
87     fig.immediate_drawing = "off";
88     axes=gca();drawlater(); show_window();
89     if new&axes.children<>[] then
90         delete(axes.children)
91     end
92     nc=size(axes.children,"*")
93     if nc==0 then
94         axes.data_bounds=defaultbounds
95         axes.axes_visible="on";
96         axes.box="on";
97         axes.title.text=msprintf(_("loci with constant damping and constant natural frequency\nin discrete plane"))
98         axes.x_label.text=_("Real Axis")
99         axes.y_label.text=_("Imaginary Axis")
100     end
101     axes.fractional_font="on"
102     axes.font_size=0.5
103     chart_handles=[]
104
105     //                           2                 2
106     //roots of                  s  + 2*zeta*wn*s +wn
107     //given by : wn*(-zeta+-%i*sqrt(1-sxi*zeta))
108
109     raci=((0:0.05:1)*%pi)'*(-zeta+%i*sqrt(ones(zeta)-zeta.*zeta))
110     // continuous --> discrete
111     raci=exp(raci);[mr,nr]=size(raci);
112     for l=1:nr,
113         r=[raci(:,l);conj(raci($:-1:1,l))]
114         xpoly(real(r),imag(r))
115         ec=gce();
116         ec.display_function = "formatZgridDampingTip";
117         ec.display_function_data = zeta(l);
118         ec.foreground=colors(1),
119         ec.line_style=7;
120         ec.clip_state="clipgrf";
121
122         xstring(real(raci(mr-10,l)),-imag(raci(mr-10,l))," "+string(zeta(l)),0,0);
123         es=gce();
124         es.font_foreground=colors(1),
125         es.clip_state="clipgrf";
126         chart_handles=[glue([es ec]) chart_handles]
127     end;
128
129     e_itheta=exp(%i*(%pi/2:0.05:%pi)')
130     zw=exp(e_itheta*(wn*%pi));[mz,nz]=size(zw)
131
132     for l=1:nz,
133         z=[zw(:,l);zw($:-1:1,l)];
134         xpoly(real(z),imag(z))
135         ec=gce();
136         ec.display_function = "formatZgridFreqTip";
137         ec.display_function_data = wn(l);
138         ec.foreground=colors(2),
139         ec.line_style=7;
140         ec.clip_state="clipgrf";
141         str=msprintf("%0.3gπ/dt",wn(l))
142         xstring(real(zw(1,l)),imag(zw(1,l)),str,0,0);
143         es=gce();
144         es.font_foreground=colors(2),
145         es.clip_state="clipgrf";
146         chart_handles=[glue([es ec]) chart_handles]
147         xpoly(real(z),-imag(z))
148         ec=gce();
149         ec.display_function = "formatZgridFreqTip";
150         ec.display_function_data = wn(l);
151         ec.foreground=colors(2),
152         ec.line_style=7;
153         ec.clip_state="clipgrf";
154
155         xstring(real(zw(1,l)),-imag(zw(1,l)),str,0,0);
156         es=gce();
157         es.font_foreground=colors(2),
158         es.clip_state="clipgrf";
159         chart_handles=[glue([es ec]) chart_handles]
160     end;
161     chart_handles=glue(chart_handles)
162     //reorder axes children to make chart drawn before the  previously
163     // drawn curves if any
164     for k=1:nc
165         swap_handles(axes.children(k),axes.children(k+1))
166     end
167
168     fig.immediate_drawing = immediate_drawing;
169
170 endfunction