Purpose
General-purpose linear interpolation function
Synopsis
(1) function interp(VR:real, XY: array(...) of real) :real
(2) function interp(VR:real, X: array(...) of real, Y: array(...) of real) :real
(3) function interp(VC:linctr, XY: array(...) of real) :gexp
(4) function interp(VC:linctr, XV: array(...) of xvitem) :gexp
(5) function interp(VG:gexp, XY: array(...) of real) :gexp
(6) function interp(VG:gexp, XV: array(...) of xvitem) :gexp
Arguments
VR
|
real value to be interpolated
|
VC
|
linear expression to be interpolated
|
VG
|
general expression to be interpolated
|
XY
|
real array of pairs of values with which to interpolate
|
X
|
real array of X-values with which to interpolate
|
Y
|
real array of Y-values with which to interpolate
|
XV
|
array of pairs of xvitems with which to interpolate
|
Return value
Forms (1) and (2) return the interpolated value. The remaining forms return the
formula for interpolating the required value.
Example
Linear interpolation is typically used to estimate the value of a function
at a point from known values at two adjacent points. As an example, suppose we want to
estimate x2, but are given values only at x=1(1), 2(4),
3(9), 4(16) and 5(25).
Then, to estimate the square of (say) 2.5 we would take the value half way
between x=2 and x=3 and obtain the result 6.5. The interp
function performs the same operation. Its first argument is the value (constant or variable)
that we are trying to interpolate. The other argument(s) contain the given values, either in
a 2-dimensional real array of corresponding X and Y values (as in XY2 below), two
1-dimensional real arrays of X and corresponding Y values (as in X and Y below), or in a
1-dimensional array of values which contain successive pairs of values X and its
corresponding Y (as in the remaining arrays below).
parameters
n = 5
end-parameters
declarations
XY2: array(1..n,1..2) of real
XY: array(1..n*2) of real
X, Y: array(1..n) of real
XV: array(1..n*2) of xvitem
V: mpvar
R: real
end-declarations
X := [1, 2, 3, 4, 5]
Y := [1, 4, 9, 16, 25]
XY:= [1,1, 2,4, 3,9, 4, 16, 5,25]
forall (i in 1..n) do
XY2(i,1) := X(i)
XY2(i,2) := Y(i)
end-do
forall (i in 1..n*2) do
XV(i) := XVitem(XY(i))
end-do
R := 2.5
writeln("Estimate (XY) for ",R," is ",interp(R,XY))
writeln("Estimate (XY2) for ",R," is ",interp(R,XY2))
writeln("Estimate (X,Y) for ",R," is ",interp(R,X,Y))
setparam("xslp_MaxXVArrayDimension",2)
interp(V,XY2) <= 10
setparam("xslp_MaxXVArrayDimension",1)
interp(V+6,XY) <= 16
interp(V^2,XV) <= 20
The first three uses of
interp calculate an immediate value (all
the arguments are constant). The last three uses all produce constraints (at least one
of the arguments is not constant). The real arrays are effectively being used as
XVs.
Normally, only 1-dimensional arrays can be used. If a multi-dimensional array is to be used,
then the parameter
xslp_maxxvarraydimension must be set before the array
is first referenced as an XV The parameter should be returned to its standard value (1)
afterwards so that any inadvertent uses of multi-dimensional arrays are identified.
Further information
More complex uses of
interp may require the use of the
Func
wrapper and custom-built XVs
Related topics
If you have any comments or suggestions about these pages,
please send mail to docs@dashoptimization.com.