I have discrete regular grid of a,b
points and their corresponding c
values and I interpolate it further to get a smooth curve. Now from interpolation data, I further want to create a polynomial equation for curve fitting. How to fit 3D plot in polynomial?
I try to do this in MATLAB. I used Surface fitting toolbox in MATLAB (r2010a) to curve fit 3-dimensional data. But, how does one find a formula that fits a set of data to the best advantage in MATLAB/MAPLE or any other software. Any advice? Also most useful would be some real code examples to look at, PDF files, on the web etc.
This is just a small portion of my data.
a = [ 0.001 .. 0.011];
b = [1, .. 10];
c = [ -.304860225, .. .379710865];
Thanks in advance.
To fit a curve onto a set of points, we can use ordinary least-squares regression. There is a solution page by MathWorks describing the process.
As an example, let's start with some random data:
As @BasSwinckels showed, by constructing the desired design matrix, you can use
mldivide
orpinv
to solve the overdetermined system expressed asAx=b
:Next we visualize the result:
As was mentioned, we can get higher-order polynomial fitting by adding more terms to the independent variables matrix (the
A
inAx=b
).Say we want to fit a quadratic model with constant, linear, interaction, and squared terms (1, x, y, xy, x^2, y^2). We can do this manually:
There is also a helper function
x2fx
in the Statistics Toolbox that helps in building the design matrix for a couple of model orders:Finally there is an excellent function
polyfitn
on the File Exchange by John D'Errico that allows you to specify all kinds of polynomial orders and terms involved: