コード例 #1
0
    def test_cheby_shape(self):
        n, a, b = 7, 0, 2
        basis = BasisChebyshev(n, a, b)
        assert_equal(basis.nodes.size, n)
        assert_equal(basis.Phi().shape, (n, n))
        assert_equal(basis._diff(0, 2).shape, (n - 2, n))
        assert_equal(basis._diff(0, -3).shape, (n + 3, n))
        assert_equal(basis().shape, (n, ))

        nx = 24
        x = np.linspace(a, b, nx)
        assert_equal(basis.Phi(x).shape, (nx, n))
        assert_equal(basis.Phi(x, 1).shape, (nx, n))
        assert_equal(basis.Phi(x, -1).shape, (nx, n))
        assert_equal(basis(x).shape, (nx, ))
コード例 #2
0
m = 1001
x = np.linspace(a, b, m)

# ### % Plot monomial basis functions

# In[6]:

Phi = np.array([x**j for j in np.arange(n)])
basisplot(x, Phi, 'Monomial Basis Functions on [0,1]')

# ### % Plot Chebychev basis functions and nodes

# In[7]:

B = BasisChebyshev(n, a, b)
basisplot(x, B.Phi(x).T, 'Chebychev Polynomial Basis Functions on [0,1]')
nodeplot(B.nodes, 'Chebychev Nodes on [0,1]')

# ### % Plot linear spline basis functions and nodes

# In[8]:

L = BasisSpline(n, a, b, k=1)
basisplot(x, L.Phi(x).T.toarray(), 'Linear Spline Basis Functions on [0,1]')
nodeplot(L.nodes, 'Linear Spline Standard Nodes on [0,1]')

# ### % Plot cubic spline basis functions and nodes

# In[9]:

C = BasisSpline(n, a, b, k=3)
コード例 #3
0
ファイル: demapp02.py プロジェクト: lnsongxf/CompEcon-python
# Set the points of approximation interval:
a = -1  # left points
b = 1  # right points

# Choose an approximation scheme. In this case, let us use an 11 by 11 Chebychev approximation scheme:
n = 11  # order of approximation
basis = BasisChebyshev(
    [n, n], a,
    b)  # write n twice to indicate the two dimensions. a and b are expanded

# Compute the basis coefficients c.  There are various way to do this:
# One may compute the standard approximation nodes x and corresponding interpolation matrix Phi and function values y
# and use:
x = basis.nodes
Phi = basis.Phi(x)  # input x may be omitted if evaluating at the basis nodes
y = f(x)
c = np.linalg.solve(Phi, y)
print('Interpolation coeff =\n ', c)

# Alternatively, one may compute the standard approximation nodes x and corresponding function values y and use these
# values to create an BasisChebyshev object with keyword argument y:
x = basis.nodes
y = f(x)
fa = BasisChebyshev([n, n], a, b, y=y)
print('Interpolation coeff =\n ', fa.c)  # attribute c returns the coefficients

# ... or one may simply pass the function directly to BasisChebyshev using keyword 'f', which by default
# will evaluate it at the basis nodes
F = BasisChebyshev([n, n], a, b, f=f)
print('Interpolation coeff =\n ', F.c)
コード例 #4
0
ファイル: demapp04.py プロジェクト: lnsongxf/CompEcon-python
for i in range(nn):
    # Uniform-node monomial-basis approximant
    xnodes = np.linspace(a, b, n[i])
    c = np.polyfit(xnodes, runge(xnodes), n[i])
    yfit = np.polyval(c, x)
    phi = xnodes.reshape(-1, 1)**np.arange(n[i])

    errunif[i] = yfit - y
    nrmunif[i] = np.log10(norm(yfit - y, np.inf))
    conunif[i] = np.log10(cond(phi, 2))

    # Chebychev-node Chebychev-basis approximant
    yapprox = BasisChebyshev(n[i], a, b, f=runge)
    yfit = yapprox(
        x)  # [0] no longer needed?  # index zero is to eliminate one dimension
    phi = yapprox.Phi()
    errcheb[i] = yfit - y
    nrmcheb[i] = np.log10(norm(yfit - y, np.inf))
    concheb[i] = np.log10(cond(phi, 2))

# Plot Chebychev- and uniform node polynomial approximation errors
ax2 = fig1.add_subplot(
    212,
    title="Runge's Function $11^{th}$-Degree\nPolynomial Approximation Error.",
    xlabel='x',
    ylabel='Error')
ax2.axhline(color='gray', linestyle='--')
ax2.plot(x, errcheb[4], label='Chebychev Nodes')
ax2.plot(x, errunif[4], label='Uniform Nodes')
ax2.legend(loc='upper center', frameon=False)