$title  Interpolation in GAMS

$ontext

Beginning GAMS programmers have a really hard time distinguishing
between the role of procedural and declarative programming constructs
in GAMS.  Interpolation in GAMS from a given table of input data to a
fixed set of integer values involves procedural programming.  There is
no need to declare equations and so forth.

It seems crazy that I should be teaching procedural programming to
anyone under the age of 50.  I wrote my first Fortran program when I
was in 5th grade (in 1965), and I repeatedly demonstrate that: "Real
programmers can write Fortran in any language."

TFR 
August, 2011
Ocean City, NJ

-----------------------------------------------------------------
Dear Tom,

I found your code of GAMS about linear interpolation. However, the
interpolation is for fixed values (interpolation for each year). I was
wondering if you have some example of interpolation using dynamic
values. I was trying to program a linear interpolation but for dynamic
values. for example

For specific values of Storage (S) estimated by my model and using a
Table (Input) make a interpolation to found specific Depth(Wdepth)

Given a Table with Volume and Specific Water Depth
*(Input)

k           Vol              D
step1      0                0
step2     10              0.3
step3     200            0.5
step4     500            0.8
step5    1000            1

I made the follow code, but I don't know why it does not works. Any
help will be greatly appreciated.


*First select the step to use (if the selection is in the range will
be 1, otherwise is 0)

Step_to_Use(t,wu,k) =E= ((S(t,wu) ge Vol(wu,k)) and ((S(t,wu) lt Vol(wu,k+1)) and (ord(k) lt card(k))))

*Once step is selected, It can develop a simple interpolation

 WDepth(t,wu) =E= sum(k$(ord(k) lt card(k)), Step_to_Use(t,wu,k)* ([D(wu,k+1)-D(wu,k)]/[Vol(wu,
k+1)-Vol(wu,k)+0.00001]*[S(t,wu)-Vol(wu,k)]+D(wu,k)));

In where t is time, wu is the type of reservoir k is the number of
steps

$offtext

set     v       Volumes to interpolate  /0*1000/,
        s       Steps -- input data     /step1*step5/;

table data      Raw data for several steps

          Vol            D
step1      0             0
step2     10             0.3
step3     200            0.5
step4     500            0.8
step5    1000            1;

set     vs(v,s) Mapping from volume to lower datapoint;
vs(v,s)$(v.val>=data(s,"vol") and v.val<data(s+1,"vol")) = yes;
vs(v,s)$(v.val=data(s,"vol")) = yes;
display vs;

parameter bug  Check that we have a one-to-one mapping;
bug(v) = yes$(sum(vs(v,s),1)<>1);
abort$card(bug) "Error in mapping:",bug;

parameter       d(v)    Interpolated depth;

loop(v,
  loop(vs(v,s),
    d(v) =  (data(s+1,"d") * (v.val - data(s,"vol")) + data(s,  "d") * (data(s+1,"vol")-v.val)) /
            (data(s+1,"vol")-data(s,"vol")) + eps;
            ));

display d;