MATLAB: Gauss elimination or inverse

gauss eliminationinverseMATLAB

Hi all,
I have a plynomial equation similar to the one iven below
Equation : ax^(n-1)+bx^(n-2)+………….+c=y
The equation must be satisfied for a set of x (say x1,x2,x3…..etc) and correspondig y (say y1,y2,y3……etc)
I need a suggestion to find the coefficient for the equation.(a,b,…..,c)
Should i use Gauss elimination method or simply do a inverse for the matrix [x] and find the solution.
Highly appreciate the responses.

Best Answer

People have said repeatedly that you want to use polyfit. This is the correct answer, IF you need to fit a polynomial.
At the same time, you should not use Gaussian elimination, or use a matrix inverse. Those tools will have you writing the code, and sadly, making many novice mistakes in the computations that are already dealt with in polyfit.
But even with polyfit, there are still massively serious issues when you are trying to fit a high order polynomial. These issues can make it almost impossible to do a high order polynomial fit in double precision arithmetic. The problem is the resulting linear system of equations will become nearly singular in double precision arithmetic. (It would be far worse had you tried to use Gaussian elimination or use a matrix inverse. Seriously so.)
The big problem is what domain the variable x lives in. What is the min and max for x? You don't show your data. How many datapoints do you have? How much noise is present in the data? Best, is if you provide your data. Use a .mat file, attached to a comment.
In order to have even a chance in hell of solving this problem, you need to use the centering and scaling options in polyfit. That will improve the condition of the solution. But even then, unless we know enough about your data, it is difficult to help you more, to know if you can actually do this fit. There is potentially a chance in the end that you can do nothing at all to solve the problem, but I cannot know that until I see your data.
Finally, I would ask if you really do need a polynomial to fit the data? High order polynomial fits are usually a terrribly bad idea, when far better tools, like spline models, are available.
Related Question