2

I am attempting to fit data of a bi-variate polynomial using normal polynomials.I have started with the example on bivariate data fitting: Fitting polynomials to data

Issue is these are not orthogonal so coeffs change as degree changes. My plan was to use legendre polynomials (build X and Y polynomials and then take cross product similarly to how I build A list below) but I am stuck on how to then fit the data.

def least_sqr(n,degree,x,y,DEL):
    #n==length of list x#
    A = []
    for i in range(n):
        A.append([])
        for xd in range(degree+1):
            for yd in range(degree+1-xd):
                A[i].append((x[i]**xd)*(y[i]**yd)) #f_j(x_i)

    q,_,_,_ = linalg.lstsq(A,DEL)
    return q

def MAIN():
    x,y,delx,dely,n = data_parse(file1, file2, file3, file4);
    degree=3
    q=least_sqr(n,degree,x,y,delx)
    r=least_sqr(n,degree,x,y,dely)  
Community
  • 1
  • 1
Magnar
  • 21
  • 2
  • 1
    I do not understand your question. you have 4 variables. a 1D fit would be fitting for example `delx` to `x`. A bivariate fit would be fitting one scalar `delx` to two variables `x,y`. Is the bivariate fitting what you're trying to do? If that is the case, you say you want to use Legendre polynomials as base for your fits. How do you define Legendre polynomials in 2D? – gg349 May 09 '14 at 09:12
  • Yes I am trying to do a bivariate fit of delx to x,y. I am creating two bivariate fits from the 4 variables I have delx fit and a dely fit. So I need to generate the coeffs for a 3rd order bivariate problem X=a0+a1x+a2y+a3x^2+a4xy+a5y^2+...+a9y^3. For this problem x and y are coordinates and delx and dely are deviations from those coordinates. So using the "Fitting polynomials to data" method my Z term would be delx and then I would complete fitting again with Z=dely so I get coeffients to map the X and Y deviation for every point. – Magnar May 09 '14 at 18:07
  • Why do you mention Legendre polynomials then? Those are normal polynomials. Please edit the question to clarify the problem, and post your code for the 3rd order fit. Read [this](http://stackoverflow.com/help/mcve) – gg349 May 10 '14 at 09:45
  • I have edited the question per your request to clarify issue. I have included the code I am currently using. The reason I mention Legendre polynomials is I knew they were orthogonal and I thought the numpy/scipy built in Legendre functions could resolve my problem. – Magnar May 12 '14 at 17:03
  • The title is still far off. I think you are trying to fit a 2D polynomial of order `degree` to some data. Is it so? If that is the case, the fit is a sum of many bases `x^a*y^b`, each multiplied by a coefficient. Is it so? State it above. Each of these bases are NON-orthogonal. I did a similar fit in 1D domains using [this](http://adsabs.harvard.edu/abs/1976JCoPh..21..208K) very well written article. I hope it helps. In addition, I do not understand the code above and the code does not run, and this puts off most people here at SO. See [this](http://www.sscce.org/). – gg349 May 12 '14 at 17:21
  • As a final note, are you sure you want a polynomial fit? If there isn't a good reason to go for a polynomial fit, opt for something else. – gg349 May 12 '14 at 17:23

0 Answers0