Question

I'd like to interpolate a surface using c#. The situation is the following:

A set of x,y,z coordinates is given. Now I d like to interpolate between those points using a finer grid. Actually I d like to know the z coordinate at a certain point, e.g. x=2.2, y=1.6 z =??.

I was able to solve the interpolation using MatLab, but was not successful while using c#.. Furthermore, I was able to plot surfaces with ilnumerics, and tried to find some information on their homepage.

EDIT:

I think I need to clarify some things - sorry for the confusing way of asking my question

here you can see how I draw the surface out of some points:

using System;
using System.Drawing;
using System.Windows.Forms;
using ILNumerics;
using ILNumerics.Drawing;
using ILNumerics.Drawing.Plotting; 

namespace Surface
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void ilPanel1_Load(object sender, EventArgs e)
      {
         using (ILScope.Enter())
         {
            ILArray<float>  R = ILMath.linspace<float>(0, 5, 5);
            ILArray<float> R1 = ILMath.linspace<float>(0, 25, 5);
            ILArray<float>  y = 1;
            ILArray<float>  x = ILMath.meshgrid(R, R, y);
            ILArray<float> z = ILMath.meshgrid(R * R, R, y);
            ILArray<float> Z = ILMath.zeros<float>(x.S[0], x.S[1], 3);

            Z[":;:;1"] = x;
            Z[":;:;2"] = y;
            Z[":;:;0"] = z;

             ilPanel1.Scene.Add(new ILPlotCube(twoDMode: false) {
                new ILSurface(Z, colormap: Colormaps.Cool) {
                    Colors = 1.4f * x * x * x + 0.13f * y * y,
                    Childs = { new ILColorbar() }
                }
            });
         }
      }
   }
}

The x and y coordinates are linearly distributed from 0 to 5 and the z coordinate has an quadratic shape. I d like to now the value of the z coordinate at a certain x,y coordinate, e.g. x=2.2, y=1.6 z =?? - which is definitely not a know point on my surface. So I thought it would be a good idea to interpolate the surface with an "finer" grid, that I m able to read out the value of the z coordinate...

Was it helpful?

Solution

The z coordinate of your function is computed in this line:

 ILArray<float> z = ILMath.meshgrid(R * R, R, y);

Since meshgrid is actually used to create the X and Y coordinates for 2 dimensional function evaluation, only the R * R result goes into z. After that line, x,y and z look as follows:

x
<Single> [5,5]
[0]: 0,00000 1,25000 2,50000 3,75000 5,00000 
[1]: 0,00000 1,25000 2,50000 3,75000 5,00000 
[2]: 0,00000 1,25000 2,50000 3,75000 5,00000 
[3]: 0,00000 1,25000 2,50000 3,75000 5,00000 
[4]: 0,00000 1,25000 2,50000 3,75000 5,00000 

y
<Single> [5,5]
[0]: 0,00000 0,00000 0,00000 0,00000 0,00000 
[1]: 1,25000 1,25000 1,25000 1,25000 1,25000 
[2]: 2,50000 2,50000 2,50000 2,50000 2,50000 
[3]: 3,75000 3,75000 3,75000 3,75000 3,75000 
[4]: 5,00000 5,00000 5,00000 5,00000 5,00000 

z
<Single> [5,5]
[0]: 0,00000 1,56250 6,25000 14,06250 25,00000 
[1]: 0,00000 1,56250 6,25000 14,06250 25,00000 
[2]: 0,00000 1,56250 6,25000 14,06250 25,00000 
[3]: 0,00000 1,56250 6,25000 14,06250 25,00000 
[4]: 0,00000 1,56250 6,25000 14,06250 25,00000 

Obviously, z does only depend on x, which gets clear by the resulting surface:

Surface

So, the value of z would be: x * x. Or for your specific example:

x=2.2, y=1.6 z =4.84

Edit: In case the underlying function is not known, you could either

  • try to learn that function (using ridge_regression() or pinv()), or
  • interpolate from the data of the neighboring grid points.

There is currently no corresponding function (like 'interp2') in ILNumerics. However, in your case - where only one single point needs to be interpolated (?), one can find the neighboring grid points and use one of the common interpolation methods.

Edit: With the release of the interpolation toolbox things became significantly easier. You can now interpolate in any dimension in high speed and with a single line.

OTHER TIPS

There are different interpolation techniques to choose from. I would suggest to start with Bilinear interpolation:

http://en.wikipedia.org/wiki/Bilinear_interpolation

or divide every quad into two triangles and use barycentric interpolation:

http://en.wikipedia.org/wiki/Barycentric_coordinate_system_(mathematics)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top