Message Boards Message Boards

0
|
5842 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Can interpolating functions be used in NMinimize constraints?

In[1]:= int = Interpolation[Table[{i, (i - 5)^2}, {i, 10}]]

Out[1]= InterpolatingFunction[{{1, 10}}, {
 5, 3, 0, {10}, {4}, 0, 0, 0, 0, Automatic, {}, {}, 
  False}, {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, {{16}, {9}, {4}, {1}, \
{0}, {1}, {4}, {9}, {16}, {25}}, {Automatic}]

In[5]:= NMinimize[{o, o == int[x]}, {x, 1, 10}]

During evaluation of In[5]:= InterpolatingFunction::dmval: Input value {0.652468} lies outside the range of data in the interpolating function. Extrapolation will be used.

During evaluation of In[5]:= NMinimize::bcons: The following constraints are not valid: {o==InterpolatingFunction[{{1,10}},{5,3,0,{10},{4},0,0,0,0,Automatic,{},{},False},{{1,2,3,4,5,6,7,8,9,10}},{{16},{9},{4},{1},{0},{1},{4},{9},{16},{25}},{Automatic}][x]}. Constraints should be equalities, inequalities, or domain specifications involving the variables.

Out[5]= NMinimize[{o, 
  o == InterpolatingFunction[{{1, 10}}, {
     5, 3, 0, {10}, {4}, 0, 0, 0, 0, Automatic, {}, {}, False}, {{1, 
     2, 3, 4, 5, 6, 7, 8, 9, 10}}, {{16}, {9}, {4}, {1}, {0}, {1}, {
     4}, {9}, {16}, {25}}, {Automatic}][x]}, {x, 1, 10}]
POSTED BY: Frank Kampas
5 Replies

Giving numbers after the variable in NMinimize gives a suggested search range, not bounds on the variables.

I think I've found a work-around

In[13]:= int = Interpolation[Table[{i, (i - 5)^2}, {i, 10}]];

In[14]:= NMinimize[{o, o == int[x]}, {o, x}] // Quiet

Out[14]= {\[Infinity], {o -> Indeterminate, x -> Indeterminate}}

In[15]:= NMinimize[{o, int[x] <= o <= int[x]}, {o, x}] // Quiet

Out[15]= {-7.45059*10^-9, {o -> -7.45059*10^-9, x -> 5.}}
POSTED BY: Frank Kampas

There are a couple of issues. The major one, I think, is the o and not the interpolating function:

NMinimize[{o , o == (x - 5)^2}, {x, 1, 10}]

(*  NMinimize::bcons: The following constraints are not valid: {o==(-5+x)^2}. 
    Constraints should be equalities, inequalities, or domain specifications involving 
    the variables.  *)

(*  Out[.]= NMinimize[{o, o == (-5 + x)^2}, {x, 1, 10}]  *)

A potential one is that a constraint of the form <some function of x> == int[x] results in a discrete set of points, which I don't think NMinimize can handle:

NMinimize[{x , x == int[x]}, {x, 1, 10}]

(*  InterpolatingFunction::dmval: Input value {0.652468} lies outside the range of data 
     in the interpolating function. Extrapolation will be used.  *)

(* NMinimize::nsol: There are no points that satisfy the constraints {}. *)

(*  Out[.]= {?, {x -> Indeterminate}}  *)

A trivial one: I don't know what the 1 and 10 do in the second argument {x, 1, 10}, but they do not constrain x to be in the domain of int[x]:

NMinimize[{x , 1 < int[x]}, {x, 1, 10}]

(*  InterpolatingFunction::dmval: Input value {0.652468} lies outside the range of data 
    in the interpolating function. Extrapolation will be used.
    ... <more messages> ... *)

(*  Out[.]= {-26.7218, {x -> -26.7218}}   <-- solution not in the domain  *)

Mariusz has already given examples that work. Here is another:

NMinimize[{(x - 4)^2 , x < int[x]}, {x} ? Cuboid @@ Transpose@ int["Domain"]]

(*  InterpolatingFunction::dmval: Input value {0.665389} lies outside the range of data 
    in the interpolating function. Extrapolation will be used.  *)

(*  Out[.]= {0.626136, {x -> 3.20871}}  *)

Constraints are enforced with a penalty function, I believe, which means that NMinimize is not guaranteed to sample strictly within the constraints; however, the solution should satisfy the constraints.

POSTED BY: Michael Rogers

That works for this example problem, but the problem I'm actually working with needs the Interpolation function in the constraints.

POSTED BY: Frank Kampas

Ok maybe so:

For 1D:

     int = Interpolation[Table[{i, (i - 5)^2}, {i, 10}]];

    {NMinimize[{x^4 - 3 x^2 - x, (x - 5)^2 < 8}, x] // Quiet, 
     NMinimize[{x^4 - 3 x^2 - x, int[x] < 8}, x] // Quiet, 
     NMinimize[{x^4 - 3 x^2 - x, int[x] < 8 && 1 < x < 10}, x] // Quiet}

     (* {{5.91934, {x -> 2.17157}}, {5.91934, {x -> 2.17157}}, {5.91934, {x ->2.17157}}}*)

For 2D:

    f = Interpolation[
      Flatten[Table[{{x, y}, x^2 + y^2 + x}, {x, -20, 20}, {y, -20, 20}], 
       1]](*Generating fake data*)
    NMinimize[{x^2 - (y - 1)^2, f[x, y] <= 4}, {x, y}]

   (* {-9.22366, {x -> -0.299019, y -> -2.05173}} *)

  NMinimize[{x^2 - (y - 1)^2, x^2 + y^2 + x <= 4}, {x, y}](* Check *)

    (* {-9.22366, {x -> -0.299019, y -> -2.05173}} *) (*OK*)
POSTED BY: Mariusz Iwaniuk

Try:

int = Interpolation[Table[{i, (i - 5)^2}, {i, 10}]];
NMinimize[{int[x], 1 < x < 10}, x]

(* {6.5524*10^-20, {x -> 5.}} *)

FindMinimum[{int[x], 1 < x < 10}, {x, 2}]

(* {0., {x -> 5.}} *)

Using FunctionInterpolation:

int2 = FunctionInterpolation[Cos[x], {x, 0, 6}];
{NMinimize[{int2[x], 0 < x < 6}, x], Plot[int2[x], {x, 0, 6}]}
POSTED BY: Mariusz Iwaniuk
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract