Overview: Trying to make a short polynomial fitting function using the Vandermonde matrix.
Here is how I generate the necessary vectors:
n = (some number, see below);
T = n/12;
for i=1:n
xt(i) = i; % row vector of indep. variable
s = 0.05*(1 - 2*rand(1)); % add noise
dt(i) = exp(-0.02*xt(i)) * sin(xt(i)/T) + s; % synthetic "observations"
end
x = xt'; % column vector of of indep. variable
d = dt'; % column vector of corresponding observations
q = length(x);
z = x(:);
A = ones(q,class(z));
for j = q-1:-1:1
A(:,j) = z.*A(:,j+1);
end
My next line is either:
P = (A\d);
or
[B,R]=qr(A,0);
P = (R\(B'*d));
Now, for n<10, I can get perfect fits. However, for n>~15, I get the badly behaved matrix, no matter if I decompose it or not, and my final polynomial fit is just crap.
Anyone have any suggestions for fixing this? I'd like to be able to get up to at least n=100 with minimal errors....
Thanks!
|
|
0
|
|
|
|
Reply
|
Mike
|
10/18/2010 5:06:03 AM |
|
On 10/17/2010 10:06 PM, Mike wrote:
> Overview: Trying to make a short polynomial fitting function using the Vandermonde matrix.
>
<SNIP>
if cond(A) is too big, i.e. A close to singular, try pinv(A) ?.
--Nasser
|
|
0
|
|
|
|
Reply
|
Nasser
|
10/18/2010 5:12:34 AM
|
|
"Mike " <mrg2138@gmail.com> wrote in message
news:i9gkjr$ksu$1@fred.mathworks.com...
> Overview: Trying to make a short polynomial fitting function using the
> Vandermonde matrix.
>
> Here is how I generate the necessary vectors:
> n = (some number, see below);
> T = n/12;
> for i=1:n
> xt(i) = i; % row vector of indep. variable
> s = 0.05*(1 - 2*rand(1)); % add noise
> dt(i) = exp(-0.02*xt(i)) * sin(xt(i)/T) + s; % synthetic "observations"
> end
> x = xt'; % column vector of of indep. variable
> d = dt'; % column vector of corresponding observations
>
> q = length(x);
> z = x(:); A = ones(q,class(z)); for j = q-1:-1:1
> A(:,j) = z.*A(:,j+1);
> end
>
> My next line is either:
> P = (A\d);
>
> or
> [B,R]=qr(A,0);
> P = (R\(B'*d));
>
>
> Now, for n<10, I can get perfect fits. However, for n>~15, I get the
> badly behaved matrix, no matter if I decompose it or not, and my final
> polynomial fit is just crap.
> Anyone have any suggestions for fixing this? I'd like to be able to get
> up to at least n=100 with minimal errors....
In general, unless there's a VERY good reason (based on the underlying
problem) to fit a high order polynomial equation, you should stick to small
order (<= 10) equations. Trying to fit a 100th order polynomial puts you
squarely in the GIGO scenario.
http://en.wikipedia.org/wiki/GIGO
If you're looking to get a curve that EXACTLY goes through all the points,
then rather than using a high order polynomial switch to an interpolating
spline approach.
--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
|
|
0
|
|
|
|
Reply
|
Steven_Lord
|
10/18/2010 2:14:21 PM
|
|
"Mike " <mrg2138@gmail.com> wrote in message <i9gkjr$ksu$1@fred.mathworks.com>...
> Overview: Trying to make a short polynomial fitting function using the Vandermonde matrix.
>
> Here is how I generate the necessary vectors:
> n = (some number, see below);
> T = n/12;
> for i=1:n
> xt(i) = i; % row vector of indep. variable
> s = 0.05*(1 - 2*rand(1)); % add noise
> dt(i) = exp(-0.02*xt(i)) * sin(xt(i)/T) + s; % synthetic "observations"
> end
> x = xt'; % column vector of of indep. variable
> d = dt'; % column vector of corresponding observations
>
> q = length(x);
> z = x(:);
> A = ones(q,class(z));
> for j = q-1:-1:1
> A(:,j) = z.*A(:,j+1);
> end
>
> My next line is either:
> P = (A\d);
>
> or
> [B,R]=qr(A,0);
> P = (R\(B'*d));
>
>
> Now, for n<10, I can get perfect fits. However, for n>~15, I get the badly behaved matrix, no matter if I decompose it or not, and my final polynomial fit is just crap.
>
> Anyone have any suggestions for fixing this? I'd like to be able to get up to at least n=100 with minimal errors....
>
> Thanks!
Sigh.
Something works reasonably well. In this case, a low
order polynomial fit. The user tries a slightly higher
order fit, and it works slightly better. So the obvious
extrapolative conclusion is that we can get arbitrarily
better results using an arbitrarily high order polynomial
fit.
The user typically understands nothing about floating
point arithmetic, but his computer is big and fast.
The answer is that, no, high order polynomials are not
a great thing to use. They fail miserably, despite the
idea that a Taylor series is just a high order polynomial.
Floating point arithmetic is your downfall. Do NOT
just keep on throwing higher order polynomials at all
problems.
Instead, use a least squares spline, or any of a variety
of other models. The model you choose will often be
delegated by your particular needs. My suggestion,
which has been designed to do very well for many
problems, is SLM.
http://www.mathworks.com/matlabcentral/fileexchange/24443
John
|
|
0
|
|
|
|
Reply
|
John
|
10/18/2010 2:38:03 PM
|
|
|
3 Replies
569 Views
(page loaded in 0.056 seconds)
Similiar Articles: Matrix is close to singular or badly scaled. - comp.soft-sys ...Overview: Trying to make a short polynomial fitting function using the Vandermonde matrix. Here is how I generate the necessary vectors: n = (some... Causes of "Matrix is singular, close to singular or badly scaled ...Hi everyone, what can cause this warning: "Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN." in th... trouble solving system of linear equations for symbolic ...When solving A*x = b' by x = linsolve(A, b'), I get Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 8.968388e-20. Solving an integro-differential equation - comp.soft-sys.matlab ...Undefined function or method 'allvalues' for input ... Solving an integro-differential equation - comp.soft-sys.matlab ... ... Matrix is close to singular or badly scaled ... Inverse of Matrix crashes Matlab - comp.soft-sys.matlabMatrix is close to singular or badly scaled. - comp.soft-sys ... Inverse of Matrix crashes Matlab - comp.soft-sys.matlab... In the example, the matrix A is not a full ... matrix inverse using qr decomposition - comp.soft-sys.matlab ...Matrix is close to singular or badly scaled. - comp.soft-sys ... Matrix is close to singular or badly scaled. - comp.soft-sys ... Matrix is close to singular or badly ... How to construct a transitive matrix from a general matrix? - comp ...Matrix is close to singular or badly scaled. - comp.soft-sys ... Overview: Trying to make a short polynomial fitting function using the Vandermonde matrix. ... generate N perfect numbers. - comp.lang.prologMatrix is close to singular or badly scaled. - comp.soft-sys ... generate N perfect numbers. - comp.lang.prolog Matrix is close to singular or badly scaled. - comp.soft ... Matlab inverse matrix - comp.soft-sys.matlab... In the example, the matrix A is not a full matrix, but matlab's inverse routine will still return a matrix. >> inv(A) Warning: Matrix is close to singular or badly ... Re: Matrix is close to singular or badly scaled. - Newsreader ...On Jun 13, 7:28 pm, kavitha <kavitharedd...@yahoo.co.in> wrote: > Please suggest. If, in the search for a solution to a problem, a badly scaled matrix results, it is ... Matrix is close to singular or badly scaled. HELP! - MATLAB ...range(AugA(:)) You'll see that the difference is on the order of 10^21. Double precision floating point values only have an accurate range of 16 decimal digits. 7/23/2012 8:30:37 PM
|