1
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
2
<!DOCTYPE MAN SYSTEM "../../manrev.dtd">
4
<LANGUAGE>eng</LANGUAGE>
6
<TYPE>Scilab Function</TYPE>
8
<SHORT_DESCRIPTION name="dsearch">binary search (aka dichotomous search in french)</SHORT_DESCRIPTION>
10
<CALLING_SEQUENCE_ITEM>[ind, occ, info] = dsearch(X, val [, ch ]) </CALLING_SEQUENCE_ITEM>
15
<PARAM_NAME>X</PARAM_NAME>
17
<SP>: a real vector or matrix</SP>
21
<PARAM_NAME>val</PARAM_NAME>
23
<SP>: a real (row or column) vector with n components in strictly increasing
24
order val(1) < val(2) < ... < val(n)</SP>
28
<PARAM_NAME>ch</PARAM_NAME>
30
<SP>: (optionnal) a character "c" or "d" (default value "c")</SP>
34
<PARAM_NAME>ind</PARAM_NAME>
36
<SP>: a real vector or matrix with the same dimensions than X</SP>
40
<PARAM_NAME>occ</PARAM_NAME>
42
<SP>: a real vector with the same format than val (but with n-1 components in the case ch="c")</SP>
46
<PARAM_NAME>info</PARAM_NAME>
54
<P>This function is useful to search in an ordered table and/or to count the number of components
55
of a vector falling in some classes (a class being an interval or a value).
57
<P>By default or when <VERB>ch="c"</VERB>, this is the interval case, that is, for
58
each X(i) search in which of the n-1 intervals it falls, the intervals being defined by:
61
<![CDATA[ I1 = [val(1), val(2)]
62
Ik = (val(k), val(k+1)] for 1 < k <= n-1 ; ]]>
68
<SP>: is the interval number of X(i) (0 if X(i) is not in
72
<SP>: is the number of components of X which are in Ik</SP>
75
<SP>: is the number of components of X which are not in [val(1),val(n)]</SP>
78
<P>When <VERB>ch="d"</VERB> case, this is the discrete case, that is, for
79
each X(i) search if it is equal to one val(k) and:
83
<SP>: is equal to the index of the component of val which matches X(i)
84
(ind(i) = k if X(i)=val(k)) or 0 if X(i) is not in val.</SP>
87
<SP>: is the number of components of X equal to val(k)</SP>
90
<SP>: is the number of components of X which are not in the set {val(1),...,val(n)}</SP>
96
// example #1 (elementary stat for U(0,1))
99
val = linspace(0,1,n+1)';
100
[ind, occ] = dsearch(X, val);
101
xbasc() ; plot2d2(val, [occ/m;0]) // no normalisation : y must be near 1/n
104
// example #2 (elementary stat for B(N,p))
105
N = 8 ; p = 0.5; m = 50000;
106
X = grand(m,1,"bin",N,p); val = (0:N)';
107
[ind, occ] = dsearch(X, val, "d");
108
Pexp = occ/m; Pexa = binomial(p,N);
109
xbasc() ; hm = 1.1*max(max(Pexa),max(Pexp));
110
plot2d3([val val+0.1], [Pexa' Pexp],[1 2],"111", ...
111
"Pexact@Pexp", [-1 0 N+1 hm],[0 N+2 0 6])
112
xtitle( "binomial distribution B("+string(N)+","+string(p)+") :" ...
113
+" exact probability versus experimental ones")
116
// example #3 (piecewise Hermite polynomial)
117
x = [0 ; 0.2 ; 0.35 ; 0.5 ; 0.65 ; 0.8 ; 1];
118
y = [0 ; 0.1 ;-0.1 ; 0 ; 0.4 ;-0.1 ; 0];
119
d = [1 ; 0 ; 0 ; 1 ; 0 ; 0 ; -1];
120
X = linspace(0, 1, 200)';
122
// define Hermite base functions
123
deff("y=Ll(t,k,x)","y=(t-x(k+1))./(x(k)-x(k+1))") // Lagrange left on Ik
124
deff("y=Lr(t,k,x)","y=(t-x(k))./(x(k+1)-x(k))") // Lagrange right on Ik
125
deff("y=Hl(t,k,x)","y=(1-2*(t-x(k))./(x(k)-x(k+1))).*Ll(t,k,x).^2")
126
deff("y=Hr(t,k,x)","y=(1-2*(t-x(k+1))./(x(k+1)-x(k))).*Lr(t,k,x).^2")
127
deff("y=Kl(t,k,x)","y=(t-x(k)).*Ll(t,k,x).^2")
128
deff("y=Kr(t,k,x)","y=(t-x(k+1)).*Lr(t,k,x).^2")
130
Y = y(ind).*Hl(X,ind) + y(ind+1).*Hr(X,ind) + d(ind).*Kl(X,ind) + d(ind+1).*Kr(X,ind);
131
xbasc(); plot2d(X,Y,2) ; plot2d(x,y,-9,"000")
132
xtitle("an Hermite piecewise polynomial")
133
// NOTE : you can verify by adding these ones :
134
// YY = interp(X,x,y,d); plot2d(X,YY,3,"000")
145
<AUTHOR>B.P. </AUTHOR>