# POLYFIT2W

Compute a weighted 2D polynomial fit.

## Syntax

`[p, S] = polyfit2w(x,y,z,w,n,m)`

## Description

```Compute the polynomial coefficients of a
function of 2 variables x and y of degrees n and m, respectively,
that fit	the data in z in a least-squares sense, using weights w.
x,y,z and w can be vectors or matrices of the same size.
S is a structure containing three elements: the Cholesky factor of the
Vandermonde matrix, the degrees of freedom and the norm of the residuals.```

The weighted regression problem is formulated in matrix format as:

```  A'*W*z = A'*W*A*p
where the matrix A contains the x,y data```
```if n = 3 and m = 1,
A = [y.*x.^3  y.*x.^2  y.*x  y x.^3  x.^2  x  ones(length(x),1)]
Note that the various xy products are column vectors of length(x).```
```The polynomial coeffiecients are ordered as
[p31 p21 p11 p01 p30 p20 p10 p00]' for the computation.```
```The coefficents of the output p
matrix are arranged as shown:```
```    p31 p30
p21 p20
p11 p10
p01 p00```

The indices on the elements of p correspond to the order of x and y associated with that element.

For a solution to exist, the number of ordered triples [x,y,z] must equal or exceed (n+1)*(m+1). Note that m or n may be zero.

To evaluate the resulting polynominal function, use polyval2d.