0

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.

slow_learner
  • 337
  • 1
  • 2
  • 15
  • Not sure to understand. You need a function that returns the coefficients for a polynomial p(X,Y) of degree n that is a best fit (in a least-squares sense) for the data in Z ? But why are you trying to fit your data if you already know the original `f(x,y)` function ? – obchardon Sep 30 '19 at 16:17
  • It is a test, I have a file with measured data which f(x, y) is unknown to me. I use this to make sure that my code works before working with the actual data where I have no means to check my results. – slow_learner Sep 30 '19 at 16:19
  • The following page should help https://de.mathworks.com/help/curvefit/evaluate-a-surface-fit.html (from Curve Fitting toolbox) – marsei Sep 30 '19 at 16:23

1 Answers1

3

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
obchardon
  • 10,614
  • 1
  • 17
  • 33
  • Thank you very much for your answer, but please, allow me a question. The actual data won't be as clean as this, is there a way to calculate R^2 to see how good the fit is? – slow_learner Sep 30 '19 at 17:05
  • 1
    @slow_learner R^2 doesn't really mean anything expect for linear regression. You can evaluate how good a fit is through other means (e.g. calculating residuals). – David Sep 30 '19 at 23:34