File: poly2fit.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (32 lines) | stat: -rw-r--r-- 844 bytes parent folder | download | duplicates (6)
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).';