views:

156

answers:

1

I have had that problem before and I couldn't solve it how do I fit a 2d surface z=f(x,y) with a polynomial in numpy (full crossterms).

Thanks in advance,

Wolfgang
A: 

This is inherently numerically ill-conditioned but you could do something like this:

import numpy as np

x = np.random.randn(500)
y = np.random.randn(500)
z = np.random.randn(500) # Dependent variable

v = np.array([ones(500), x, y, x**2, x * y, y**2])

coefficients, residues, rank, singval = np.lstsq(v.T, z)

The more terms you add, the worse things get, numerically. Are you sure you want a polynomial interpolant?

There are other bases for polynomials for which the matrix of values is not so badly conditioned but I can't remember what they are called; any college-level numerical analysis textbook would have this material, though.

dwf