Fit a polynomial function

Does someone know how it is possible to fit a polynomial function whent the x value is a vector? In other words, if we want to fit a polynomial function with output data y and input parameters x where x=[x1,x2,x3,....,xn]. Because until now the only thing that I have found is only if x is a single parameter.

 Accepted Answer

the cyclist
the cyclist on 21 Jun 2015
Edited: the cyclist on 21 Jun 2015
The easiest way to do this, if you have the Statistics Toolbox, is to use the fitlm function. Here is some example code:
load carsmall
tbl = table(Weight,Acceleration,MPG,'VariableNames',{'Weight','Acceleration','MPG'});
% % The formula from the example
% formula = 'MPG ~ Weight + Acceleration';
% A formula with 2nd- and 3rd-order polynomials
formula = 'MPG ~ Weight^3 + Acceleration^2';
lm = fitlm(tbl,formula)
A few things to notice:
This code is based on the example in that documentation.
I have included -- but commented out -- the formula in which Weight and Acceleration are included to first order.
The model I fit includes Acceleration to 2rd order and Weight to 3rd order. Notice that the way I specified the model, MATLAB automatically included the lower-order terms (including the intercept).
You could also include cross terms like Acceleration*Weight, but I did not.
Remember that a "linear" model is one that is linear in the coefficients. Even though this model includes terms like Acceleration^2, it is still linear because the coefficient of that term will be linear.

6 Comments

Thank you for your answer. This seems to me more close to what I need, but you have to define the formula of the polynomial every time. In my case, I have 11 independent variables and one dependent! So, I am looking if there is a polynomial fit for which to provide the data and the required order of teh polynomial and give me the best fit
Of course the best fitting polynomial for a set of N points will be a polynomial of order N-1 where it will go through every point exactly. Of course it goes crazy in between points with wild oscillations, so you don't want that if you want to estimate values for any points that are not your training points.
Katerina Rippi
Katerina Rippi on 21 Jun 2015
Edited: Katerina Rippi on 21 Jun 2015
Ok, I agree with that and that is why I want to fit just a quadratic or a cubic polynomial. However, "the cyclist" provided me with an answer where I have to write which parameters will be in the power of 2 or 3 ('MPG ~ Weight^3 + Acceleration^2'). So, my question is if I can just by providing the variables and the order of the polynomial that I want, to have a fit. Because, as you can understand if you have 11 parameters it is a bit difficult to write the formula.
I fail to follow what you think you're model is... somewhere, somehow you have to specify it; Matlab doesn't know what you intend a priori, either.
Do you want simply
y=sum(c*xi)
or what about cross terms and/or higher powers? How's all that to be specified with just a single input?
The Curve Fitting toolbox does have a shorthand nomenclature of being able to write polyNM and it will fill in from the two numerical values the terms of those powers and cross terms but it's limited to only two independent variables and max 5th order so can't use that form with eleven candidate variables.
I thought that by giving the order of the polynomial and of course the data, Matlab would be capable of fitting this polynomial. But, anyway, I wrote an approximate formula like "cyclist" sail that I thought would best fit and it worked. So, thanks everyone. Problem solved, I think. If any other implications I may ask again
But, again, ML has to have some way to "know" what you intend and with multiple variables the possibilities are endless so there's no attempt to try to generalize beyond some lower-order special cases that are fairly frequent but you kept dancing around the question of whether you did or did not, think interaction terms would be important for starters...

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 21 Jun 2015
See my polyfit demo, attached below this image it creates

4 Comments

@IA, I believe she wants a fit of the form
Y = f(X1,X2,...),
not just
Y = f(X)
For a multi-dimensional fit, she can use John D'Errico's polyfitn(): http://www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn
Thanks but I have already seen these options. My question was if y=f(x1, x2,x3,...,xn) then how we make the regression of the polynomial? Do we have this possibility or the option is only if y=f(x)?
Attached is an example. Consider x1 to be the horizontal direction, and x2 to be the orthogonal (vertical) direction. It fits the data (models it) to a 4th order polynomial in both directions. For each (x1, x2) pair, I have a value f(x1,x2) which is the intensity of the image. Then I fit a 2D 4th order polynomial surface to those values.

Sign in to comment.

dpb
dpb on 21 Jun 2015
There are several regression and curve fitting routines if you have the Statistics and/or Curve Fitting toolboxes; if you don't you can use the "backslash" operator that will do a least squares solution to an overdetermined system. In this case you write the explicit model as the design matrix (not forgetting to include the column of Ones for the intercept term, of course) and a vector of the observation values as
X=[ones(length(y),1) x1 x2 ... xN];
and solve as
c=X\y;
where each xi is the (column) vector of the values of the associated independent variable and y is your vector of observations.
For example, if you were to try to fit a model of the form z=f(x,y) with the cross term, you could write
X=[ones(size(z)) x x.*y y];
c=X\z;
Again, the above assumes column vectors x,y,z are the two independent variables and z is the observation. c will be the coefficients of the the model
z=c(1) + c(2)*x + c(3)*x.*y + c(4)*y;

Categories

Asked:

on 21 Jun 2015

Commented:

dpb
on 21 Jun 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!