Two scripts: polyfitweighted2 fits 2D data with weights, and polyval2 evaluates the 2D polynomial
10 Downloads
Updated 12 Feb 2007
No Licensepolyfitweighted2.m and polyval2.m
---------
polyfitweighted2.m: Find a least-squares fit of 2D data z(x,y) with an n th order polynomial, weighted by w(x,y).
polyval2.m: Evaluate 2D polynomial produced by polyfitweighted2.m.
Usage polyfitweighted2
-----
P = polyfitweighted2(X,Y,Z,N,W) finds the coefficients of a polynomial P(X,Y) of degree N that fits the data Z best in a least-squares sense. P is a row vector of length (N+1)*(N+2)/2 containing the polynomial coefficients in ascending powers, 0th order first.
P = [p00 p10 p01 p20 p11 p02 p30 p21 p12 p03...]
where pNM is the polynomial coefficient of the Nth power of X times the Mth power of Y.
based on polyfit.m by The Mathworks Inc.
For more information see HTML-published M-file included.
Note: P is not in the format of standard Matlab 1D polynomials. Use polval2.m to evaluate the polynomial in this format, at given values of x,y.
Usage polyval2
---
Z = POLYVAL2(P,X,Y) returns the value of a 2D polynomial P evaluated at (X,Y). P is a vector of length (N+1)*(N+2)/2 containing the polynomial coefficients in ascending powers as above.
Inspired: 2D polynomial fitting with SVD, polyVal2D and polyFit2D
Daniel Politte (view profile)
William Thalheimer (view profile)
Your documentation is unclear, the size of Z and W should be [length(y), length(x)], but you list it in the opposite order, which does not work.
For example,
polyfitweighted2([1,2],[1,2,3],[1,3,5;3,4,6],2,[1,1,1;1,1,1])
does not work, but
polyfitweighted2([1,2],[1,2,3],[1,3;5,3;4,6],2,[1,1;1,1;1,1])
does work. Note here that size(Z) = [3,2], length(x)=2, and length(y)=3
This is the same convention that surf() and mesh() uses.
Great function besides that though.
Richard Edmonds (view profile)
my bad,
Just having problems with matrix dimensions. lol.
It can be useful to generate Z from the outputs of meshgrid(x,y) so that the dimensions are correct.
milad babaei (view profile)
would somebody plz explain me on that those script algorithm??how does code work (does the least square )???
David Holdaway (view profile)
Very useful program, only a few issues with memory usage creating the very large grid of zeros length(x)*length(y) by length(p). Could possibly be improved with a catch routine to check if the memory usage is too high and split the (x,y) grid into 4 or 9 etc and apply the program multiple times.
This is just nitpicking though it's a very good program.
K E (view profile)
Very useful. Matlab should build it in.
Ryan Kruse (view profile)
This is great! Thanks!
I made a slight modification to deal with NaN values. I changed this:
x = x(:);
y = y(:);
z = z(:);
w = w(:);
to this:
x = x(~isnan(z));
y = y(~isnan(z));
z = z(~isnan(z));
w = w(~isnan(z));
Andrey Rubshtein (view profile)
Dear Salman, first of all thank you for your submission.
Nevertheless, I have a minor remark - In many applications (x,y,z) triples are not necessarily measured on a grid. For example: I have:
(1,1,10), (1,2,5) , (2,1,7) , but i have no clue what is the value of z at (2,2).
Good work!
Thank you!