matlabinterpolationpolynomial-approximations

Obtaining a 2D interpolation polynomial in Matlab


I have three vectors, one of X locations, another of Y locations and the third is a f(x, y). I want to find the algebraic expression interpolation polynomial (using matlab) since I will later on use the result in an optimization problem in AMPL. As far as I know, there are not any functions that return the interpolation polynomial.

I have tried https://la.mathworks.com/help/matlab/ref/griddedinterpolant.html, but this function only gives the interpolated values at certain points.

I have also tried https://la.mathworks.com/help/matlab/ref/triscatteredinterp.html as sugested in Functional form of 2D interpolation in Matlab, but the output isn't the coefficents of the polynomial. I cannot see it, it seems to be locked inside of a weird variable.

This is a small program that I have done to test what I am doing:

close all
clear
clc

[X,Y] = ndgrid(1:10,1:10);
V = X.^2 + 3*(Y).^2;
F = griddedInterpolant(X,Y,V,'cubic');
[Xq,Yq] = ndgrid(1:0.5:10,1:0.5:10);
Vq = F(Xq,Yq);
mesh(Xq,Yq,Vq)
figure
mesh(X, Y, V)

I want an output that instead of returning the value at grid points returns whatever it has used to calculate said values. I am aware that it can be done in mathematica with https://reference.wolfram.com/language/ref/InterpolatingPolynomial.html, so I find weird that matlab can't.


Solution

  • You can use fit if you have the curve fitting toolbox.

    If it's not the case you can use a simple regression, if I take your example:

    % The example data
    [X,Y] = ndgrid(1:10,1:10);
    V = X.^2 + 3*(Y).^2;
    
    % The size of X
    s = size(X(:),1);
    
    % Let's suppose that you want to fit a polynome of degree 2.
    % Create all the possible combination for a polynome of degree 2
    %        cst     x     y     x^2       y^2       x*y
    A = [ones(s,1), X(:), Y(:), X(:).^2, Y(:).^2, X(:).*Y(:)]
    
    % Then using mldivide
    p = A\V(:)
    
    % We obtain:
    
    p = 
        0  % cst
        0  % x
        0  % y
        1  % x^2
        3  % y^2
        0  % x*y