示例#1
0
def phi_factory(name, N, highest_derivative=2):
    """
    Generate N+1 basis functions (phi) on [0,1] which vanish
    on the boundary. Differentiate the functions up to
    (and including) highest_derivative.
    """
    x = sym.Symbol('x')
    from sympy import sin, cos, pi
    if name == 'sines':
        phi = {0: [sin(pi*(i+1)*x) for i in range(N+1)]}
    elif name == 'poly':
        phi = {0: [x**(i+1)*(1-x) for i in range(N+1)]}
    elif name == 'poly2':
        phi = {0: [x**(i+2)*(1-x) for i in range(N+1)]}
    elif name == 'poly3':
        phi = {0: [(1-x)**(i+1) for i in range(N+1)]}
    elif name == 'Lagrange':
        from Lagrange import Lagrange_polynomials
        # Return all Lagrange polynomials and strip off
        # boundary polynomials in application code (if necessary)
        phi = {0: Lagrange_polynomials(x, N, [0,1], 'uniform')[0]}
    elif name == 'Lagrange_Cheb':
        from Lagrange import Lagrange_polynomials
        phi = {0: Lagrange_polynomials(x, N, [0,1], 'Chebyshev')}

    # Compute derivatives of the basis functions
    for d in range(1, highest_derivative+1):
        phi[d] = [sym.diff(phi[0][i], x, d) for i in range(len(phi[0]))]
    return phi
示例#2
0
import sys, os
sys.path.insert(0, os.path.join(os.pardir, 'src-approx'))
from approx1D import interpolation, comparison_plot
from Lagrange import Lagrange_polynomials
import sympy as sym

x = sym.Symbol('x')
Omega = [0, 1]
N_values = 3, 7, 11, 15

for s in 5, 20:
    f = -sym.tanh(s * (x - 0.5))  # sympy expression
    for distribution in 'uniform', 'Chebyshev':
        for N in N_values:
            phi, points = Lagrange_polynomials(x,
                                               N,
                                               Omega,
                                               point_distribution=distribution)

            u, c = interpolation(f, phi, points)
            filename = 'tmp_tanh_%d_%d_%s' % (N, s, distribution)
            comparison_plot(f,
                            u,
                            Omega,
                            filename,
                            plot_title='s=%g, N=%d, %s points' %
                            (s, N, distribution))
        # Combine plot files (2x2)
        for ext in 'png', 'pdf':
            cmd = 'doconce combine_images ' + ext + ' '
            cmd += ' '.join(
                ['tmp_tanh_%d_%d_%s' % (N, s, distribution) for N in N_values])
示例#3
0
sys.path.insert(0, os.path.join(os.pardir, 'src'))
from approx1D import regression, comparison_plot
from Lagrange import Lagrange_polynomials
import sympy as sym
import numpy as np

x = sym.Symbol('x')
Omega = [0, 1]
N_values = 3, 7, 11, 15

for s in 5, 20:
    f = -sym.tanh(s * (x - 0.5))  # sympy expression
    for distribution in 'uniform', 'Chebyshev':
        for N in N_values:
            # Compute the points from a 2*N Lagrange polynomial
            dummy, points = Lagrange_polynomials(
                x, 2 * N, Omega, point_distribution=distribution)
            # Compute phi from N points Lagrange polynomial
            phi, dummy = Lagrange_polynomials(x,
                                              N,
                                              Omega,
                                              point_distribution=distribution)
            points = np.array(points, dtype=float)
            point_values = -np.tanh(s * (points - 0.5))

            u, c = regression(f, phi, points)
            filename = 'tmp_tanh_%d_%d_%s' % (N, s, distribution)
            comparison_plot(f,
                            u,
                            Omega,
                            filename,
                            plot_title='s=%g, N=%d, %s points' %