File: poly2fit.m

package info (click to toggle)
psychtoolbox-3 3.0.9%2Bsvn2579.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 63,408 kB
  • sloc: ansic: 73,310; cpp: 11,139; objc: 3,129; sh: 1,669; python: 382; php: 272; makefile: 172; java: 113
file content (32 lines) | stat: -rw-r--r-- 843 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function p = poly2fit(x,y,z,n)
% POLY2FIT	POLY2FIT(x,y,z,n) finds the coefficients of a two-dimensional
%		polynomial formed from the data in the matrices x and y
%		that fits the data in the matrix z in a least-squares sense.
%               The coefficients are returned in the vector p, in descending
%               powers of x and y. For example, the second order polynomial
%
%                  x ^ 2 + xy +2x -3 y - 1
% 
%               would be returned as p = [ 1 1 0 2 -3 -1 ]
%

%		Written my Jim Rees, 12 Aug. 1991

if any((size(x) ~= size(y)) | size(x) ~= size(z))
	error('X,Y, and Z matrices must be the same size')
end

% First straighten out x, y, and z. 
x = x(:); y = y(:); z = z(:);

n = n+1;
k = 1;
%A = zeros(x);
for i = n:-1:1,
  for j=1:i,
	A(:,k) =  ((x.^(i-j)).*(y.^(j-1))) ;
        k = k + 1;
  end
end

p = (A\z).';