1
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
2
<!DOCTYPE MAN SYSTEM "../../manrev.dtd">
4
<LANGUAGE>eng</LANGUAGE>
8
<SHORT_DESCRIPTION name="dsearch"> dichotomic (binary) search</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 (row or column) vector (1,m) or (m,1)</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 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>
55
<DESCRIPTION_ITEM label="case ch="c"">
56
<SP>this is the interval case, for each X(i) search in which of the
57
n-1 intervals it falls, the intervals being defined by :</SP>
61
Ik = (val(k), val(k+1)] for 1 < k <= n-1 ;
65
<DESCRIPTION_ITEM label="ind(i)">
66
<SP>is the interval number of X(i) (0 if X(i) is not in
69
<DESCRIPTION_ITEM label="occ(k)">
70
<SP>is the number of components of X which are in Ik</SP>
72
<DESCRIPTION_ITEM label="info">
73
<SP>is the number of components of X which are not in [val(1),val(n)]</SP>
77
<DESCRIPTION_ITEM label="case ch="d"">
78
<SP>this is the discrete case, for each X(i) search if it is equal to one val(k)</SP>
80
<DESCRIPTION_ITEM label="ind(i)">
81
<SP>is equal to the index of the component of val which matches X(i)
82
(ind(i) = k if X(i)=val(k)) or 0 if X(i) is not in val.</SP>
84
<DESCRIPTION_ITEM label="occ(k)">
85
<SP>is the number of components of X equal to val(k)</SP>
87
<DESCRIPTION_ITEM label="info">
88
<SP>is the number of components of X which are not in val</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 law 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")
142
<AUTHOR>B.P. </AUTHOR>