예제 #1
0
def polyFit(xData, yData, m):
    """ 
    Returns coefficients of the polynomial
    p(x) = c[0] + c[1]x + c[2]x^2 +...+ c[m]x^m
    that fits the specified data in the least
    squares sense.

    USAGE:
        c = polyFit(xData,yData,m)
    INPUT:
        xData,yData : numpy arrays
            data to be fitted
        m : int
            degree of p(x)
    OUTPUT:
        c : numpy array
            coefficients of p(x)
    """
    import numpy as np
    from ps12 import gaussElimin

    def SUM1(j, k, x):
        s = 0
        for i in range(len(x)):
            s = x[i] ** (j + k) + s
        return s

    def SUM2(k, x, y):
        s = 0
        for i in range(len(y)):
            s = x[i] ** k * y[i] + s
        return s

    a = np.zeros([m + 1, m + 1])
    for j in range(m + 1):
        for k in range(m + 1):
            a[k, j] = SUM1(j, k, xData)
    b = np.zeros([m + 1, 1])
    for k in range(m + 1):
        b[k, 0] = SUM2(k, xData, yData)
    x = gaussElimin(a, b)
    return x
예제 #2
0
def main():
    a = np.array([[2, 1, -1], [-3, -1, 2], [-2, 1, 2]])
    b = np.array([[8], [-11], [-3]])
    x = ps12.gaussElimin(a, b)
    x = ps12.LUdecomp(a)
    y = ps12.LUsolve(x, b)