1
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
2
<!DOCTYPE MAN SYSTEM "../../manrev.dtd">
4
<LANGUAGE>eng</LANGUAGE>
5
<TITLE>linear_interpn</TITLE>
6
<TYPE>Scilab Function</TYPE>
7
<DATE>February 2004</DATE>
8
<SHORT_DESCRIPTION name="linear_interpn">n dimensional linear interpolation</SHORT_DESCRIPTION>
10
<CALLING_SEQUENCE_ITEM>vp = linear_interpn(xp1,xp2,..,xpn, x1, ..., xn, v [,out_mode])</CALLING_SEQUENCE_ITEM>
15
<PARAM_NAME>xp1, xp2, .., xpn</PARAM_NAME>
17
<SP>: real vectors (or matrices) of same size</SP>
21
<PARAM_NAME>x1 ,x2, ..., xn</PARAM_NAME>
23
<SP>: strictly increasing row vectors (with at least 2 components)
24
defining the n dimensional interpolation grid</SP>
28
<PARAM_NAME>v</PARAM_NAME>
30
<SP>: vector (case n=1), matrix (case n=2) or hypermatrix (case n > 2) with the
31
values of the underlying interpolated function at the grid points.</SP>
35
<PARAM_NAME>out_mode</PARAM_NAME>
37
<SP>: (optionnal) string defining the evaluation outside the grid (extrapolation)</SP>
41
<PARAM_NAME>vp</PARAM_NAME>
43
<SP>: vector or matrix of same size than <VERB>xp1, ..., xpn</VERB></SP>
50
Given a n dimensional grid defined by the n vectors <VERB>x1 ,x2, ..., xn</VERB> and the
51
values <VERB>v</VERB> of a function (says <EM>f</EM>) at the grid points :
54
v(i1,i2,...,in) = f(x1(i1),x2(i2), ..., xn(in))
57
this function computes the linear interpolant of <EM>f</EM> from the grid (called <EM>s</EM>
58
in the following) at the points which coordinates are defined by the vectors (or matrices)
59
<VERB>xp1, xp2, ..., xpn</VERB>:
62
vp(i) = s(xp1(i),xp2(i), ..., xpn(i))
64
or vp(i,j) = s(xp1(i,j),xp2(i,j), ..., xpn(i,j)) in case the xpk are matrices
67
The <VERB>out_mode</VERB> parameter set the evaluation rule for extrapolation: if we note
68
<EM>Pi=(xp1(i),xp2(i),...,xpn(i))</EM> then <VERB>out_mode</VERB> defines the
72
P(i) not in [x1(1) x1($)] x [x2(1) x2($)] x ... x [xn(1) xn($)]
75
The different choices are:
77
<ITEMIZE label="out_mode">
78
<ITEM label=""by_zero"">
79
<SP>: an extrapolation by zero is done</SP>
81
<ITEM label=""by_nan"">
82
<SP>: extrapolation by Nan</SP>
84
<ITEM label=""C0"">
85
<SP>: the extrapolation is defined as follows:</SP>
87
s(P) = s(proj(P)) where proj(P) is nearest point from P
88
located on the grid boundary.
91
<ITEM label=""natural"">
92
<SP>: the extrapolation is done by using the nearest n-linear patch from the point.</SP>
94
<ITEM label=""periodic"">
95
<SP>: <VERB>s</VERB> is extended by periodicity.</SP>
101
// example 1 : 1d linear interpolation
102
x = linspace(0,2*%pi,11);
104
xx = linspace(-2*%pi,4*%pi,400)';
105
yy = linear_interpn(xx, x, y, "periodic");
107
plot2d(xx,yy,style=2)
108
plot2d(x,y,style=-9, strf="000")
109
xtitle("linear interpolation of sin(x) with 11 interpolation points")
111
// example 2 : bilinear interpolation
113
x = linspace(0,2*%pi,n); y = x;
114
z = 2*sin(x')*sin(y);
115
xx = linspace(0,2*%pi, 40);
116
[xp,yp] = ndgrid(xx,xx);
117
zp = linear_interpn(xp,yp, x, y, z);
119
plot3d(xx, xx, zp, flag=[2 6 4])
120
[xg,yg] = ndgrid(x,x);
121
param3d1(xg,yg, list(z,-9*ones(1,n)), flag=[0 0])
122
xtitle("Bilinear interpolation of 2sin(x)sin(y)")
123
legends("interpolation points",-9,1)
126
// example 3 : bilinear interpolation and experimentation
127
// with all the outmode features
129
x = linspace(0,1,nx);
130
y = linspace(0,2, ny);
132
z = 0.4*cos(2*%pi*X).*cos(%pi*Y);
133
nxp = 60 ; nyp = 120;
134
xp = linspace(-0.5,1.5, nxp);
135
yp = linspace(-0.5,2.5, nyp);
136
[XP,YP] = ndgrid(xp,yp);
137
zp1 = linear_interpn(XP, YP, x, y, z, "natural");
138
zp2 = linear_interpn(XP, YP, x, y, z, "periodic");
139
zp3 = linear_interpn(XP, YP, x, y, z, "C0");
140
zp4 = linear_interpn(XP, YP, x, y, z, "by_zero");
141
zp5 = linear_interpn(XP, YP, x, y, z, "by_nan");
144
plot3d(x, y, z, leg="x@y@z", flag = [2 4 4])
145
xtitle("initial function 0.4 cos(2 pi x) cos(pi y)")
147
plot3d(xp, yp, zp1, leg="x@y@z", flag = [2 4 4])
150
plot3d(xp, yp, zp2, leg="x@y@z", flag = [2 4 4])
153
plot3d(xp, yp, zp3, leg="x@y@z", flag = [2 4 4])
156
plot3d(xp, yp, zp4, leg="x@y@z", flag = [2 4 4])
159
plot3d(xp, yp, zp5, leg="x@y@z", flag = [2 4 4])
164
// example 4 : trilinear interpolation (see splin3d help
165
// page which have the same example with
166
// tricubic spline interpolation)
167
getf("SCI/demos/interp/interp_demo.sci")
168
func = "v=(x-0.5).^2 + (y-0.5).^3 + (z-0.5).^2";
169
deff("v=f(x,y,z)",func);
171
x = linspace(0,1,n); y=x; z=x;
172
[X,Y,Z] = ndgrid(x,y,z);
174
// compute (and display) the linear interpolant on some slices
176
dir = ["z=" "z=" "z=" "x=" "y="];
177
val = [ 0.1 0.5 0.9 0.5 0.5];
178
ebox = [0 1 0 1 0 1];
180
XF=[]; YF=[]; ZF=[]; VF=[];
181
for i = 1:length(val)
182
[Xm,Xp,Ym,Yp,Zm,Zp] = slice_parallelepiped(dir(i), val(i), ebox, m, m, m);
183
Vm = linear_interpn(Xm,Ym,Zm, x, y, z, V);
184
[xf,yf,zf,vf] = nf3dq(Xm,Ym,Zm,Vm,1);
185
XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf]; VF = [VF vf];
186
Vp = linear_interpn(Xp,Yp,Zp, x, y, z, V);
187
[xf,yf,zf,vf] = nf3dq(Xp,Yp,Zp,Vp,1);
188
XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf]; VF = [VF vf];
191
vmin = min(VF); vmax = max(VF);
192
color = dsearch(VF,linspace(vmin,vmax,nb_col+1));
193
xset("colormap",jetcolormap(nb_col));
195
xset("hidden3d",xget("background"))
197
plot3d(XF, YF, list(ZF,color), flag=[-1 6 4])
198
xtitle("tri-linear interpolation of "+func)
204
<LINK>interpln</LINK>