19 views (last 30 days)

Hello everyone.

I have a file which size is 681*441, it contains values measured at 300321 points. I want to find the interpolation polynomial. I have looked for functions which return what I am seeking; but I have found none. griddedinterpolant does not return the polynomial in question, but only values in a more precise grid. On the other hand, the function polyfit does provide with the nodes of a polynomial of any desired order, but it seems to work only in one dimension.

Can someone please tell me if there is a way to do as in the function polyfit but returning a two dimensional polynomial?

Thanks.

Jaime.

David K.
on 25 Sep 2019

First off, I looked a little bit on wikis to find https://en.wikipedia.org/wiki/Multivariate_interpolation which might have what you want but I am not sure. However, the way I wanted to do it is with fmincon. I attached the objective function I created. Basically it creates a function of the form

a1x^2+a2y^2+a3xy+a4x+a5x+a6

then it loads in the X,Y, and wind data that you have and determines how closely it fits by a basic difference and summing.

This is utilized by fmincon as such:

a = fmincon(@objfun, rand(6,1), eye(6),ones(6,1).*20)

I decided to make the initial conditions random just because. Then, because this is a constrained minimum search, I just made it so that no value could go over 20, it would also likely work with large constraints as well but I doubt they will come into affect much. I tried using other optimization algorithms that did not require constraints but they did not work as well. For the example you gave the output should be

a = [0 1 0 1 0 0]

It generally gets pretty close, sometimes the constant value messes up though.

David K.
on 26 Sep 2019

I am loading in testData.mat so if you change your save to be

save('testData.mat', 'wind','X','Y')

it should work fine.

Or, we can remove saving in general to make it a little nicer. Change the function as seen

function output = objfun(coeffs,wind,X,Y)

% create the function ax^2+by^2+cxy+dx+ey+f

currPoly = @(x,y) coeffs(1).*x.^2 + coeffs(2).*y.^2 + coeffs(3).*x.*y + coeffs(4).*x + coeffs(5).*y + coeffs(6);

% data = load('testData.mat');

currGuess = currPoly(X,Y); % eval the coefficients

error = abs(wind-currGuess);

output = sum(error,[1,2]); % sum the error

Then change the way you are calling it as such:

a = fmincon(@(x) objfun(x,wind,X,Y), rand(6,1), eye(6),ones(6,1).*20)

This will actually also be faster since calling a load function multiple times is pretty slow.

David K.
on 30 Sep 2019

Sign in to comment.

John D'Errico
on 26 Sep 2019

I don't think you realize that an interpolation polynomial that passes exctly through 300321 points will be impossible to evaluate in double precision arithmetic. In fact, it may require a precision that is on the order of many thousand of decimal digits to get any thing out if it. Possibly hundreds of thousands of digits will be required.

And then, expect complete crapola anyway. Why? Because the polynomial might actually predict the points, but give you meaningless garbage between the points!

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/482094-two-dimensional-interpolation-polynomial#comment_749843

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/482094-two-dimensional-interpolation-polynomial#comment_749843

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/482094-two-dimensional-interpolation-polynomial#comment_749862

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/482094-two-dimensional-interpolation-polynomial#comment_749862

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/482094-two-dimensional-interpolation-polynomial#comment_749874

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/482094-two-dimensional-interpolation-polynomial#comment_749874

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/482094-two-dimensional-interpolation-polynomial#comment_749880

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/482094-two-dimensional-interpolation-polynomial#comment_749880

Sign in to comment.