0

I'm trying to use interpn function and probing even a simple 2D example I cannot reproduce the interpolation value FUN(xi,yi) compared to the matrix grid value i.e. FUN(1,11) != 4.


A = [13,-1,12;5,4,3;1,6,2];
x = [0,1,2];
y = [10,11,12];

xi = linspace (min (x), max (x), 30);
yi = linspace (min (y), max (y), 60);
FUN = interpn (x, y, A, xi, yi, "spline");

disp("value(1,11) = "), FUN(1, 11)
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Damir Devetak
  • 726
  • 4
  • 10
  • 2
    1) this code errors in my MATLAB 2018b. Is this purely octave, I assume? 2) `FUN(1,11)` is not the value of the function at `x=1;y=11`, its the value of the function at index `i=1;j=11`. For example, in your case `y` goes from 10 to 12 in 60 steps, `j=1 --> y=10` while `j=60 --> y=12` You can figure out what `y` does correspond to at `j=11` then (hint, just do `yi(11)`). – Ander Biguri Oct 01 '19 at 12:45
  • Thanks Ander, good feedback. Not to diverge too much but it goes towards my wider goal. I was planning with Octave to interpolate ND data and then run some minimization algorithm like fminunc onto this interpolated function; But now its confusion that the FUN(x,y) cannot be treated as simple continuous function; Would this venture (optimization) be possible like I described it even in MatLab? Thanks – Damir Devetak Oct 01 '19 at 13:07
  • None of this is a problem for anything in MATLAB/Octave. Otherwise you could only work with functions defined on integer coordinates, which makes no sense and would make it useless. Just be aware when you are using data that indices are not coordinates. I can't help more unless a more specific problem is stated. – Ander Biguri Oct 01 '19 at 13:58
  • @damir You can define `FUN` as a function handle: `FUN = @(xi,yi) interpn (x, y, A, xi, yi, "spline");` – rahnema1 Oct 01 '19 at 14:13
  • Great works! Thanks @rahmena1; Just a follow up question if you have an idea. Doing now a simple x0 = [1,10]; [x,fval] = fminunc(FUN, x0); Doesn't seem to go through. Do you see an issue with this perhaps? – Damir Devetak Oct 01 '19 at 14:41
  • In that case you need to use: `FUN = @(xi) interpn (x, y, A, xi(1), xi(2), "spline"); ` – rahnema1 Oct 01 '19 at 15:21
  • It doesn't work now. FUN = @(xi) interpn (x, y, A, xi(1), yi(2), "spline"); gives the wrong values for point FUN(0, 11); – Damir Devetak Oct 01 '19 at 16:59
  • I used `xi(1), xi(2)` but you used `xi(1), yi(2)`. – rahnema1 Oct 01 '19 at 17:07
  • You may want to know more about defining [functions](https://octave.org/doc/v5.1.0/Defining-Functions.html) and [function handles](https://octave.org/doc/v5.1.0/Anonymous-Functions.html#Anonymous-Functions). – rahnema1 Oct 01 '19 at 17:15
  • This option doesn't work... xi = linspace (min (x), max (x), 20); FUN = @(xi) interpn (x, y, A, xi(1), xi(2), "spline"); disp("value(1,10) = "), FUN(1, 10) error: xi(2): out of bound 1 – Damir Devetak Oct 01 '19 at 19:19
  • When you define FUN as @(xi)... it only requires one parameter named xi. xi is a vector having two elements xi=[1 10]. So you need to call FUN as FUN([1 10]). You may need to read some tutorials or documentations about programming in Octave/MATLAB. – rahnema1 Oct 02 '19 at 00:20

0 Answers0