Example #1
0
def naive(f, s, Omega, N=10):
    psi = []
    for i in range(N + 1):
        psi.append(sym.sin((2 * i + 1) * x))
        u, c = least_squares_orth(f, psi, Omega, symbolic=False)
        comparison_plot(f,
                        u,
                        Omega,
                        'tmp_sin%02dx' % i,
                        legend_loc='upper left',
                        show=True)
Example #2
0
def efficient(f, s, Omega, N=10):
    u = 0
    for i in range(N + 1):
        psi = [sym.sin((2 * i + 1) * x)]
        next_term, c = least_squares_orth(f, psi, Omega, False)
        u = u + next_term
        comparison_plot(f,
                        u,
                        Omega,
                        'tmp_sin%02dx' % i,
                        legend_loc='upper left',
                        show=False,
                        plot_title='s=%g, i=%d' % (s, i))
def efficient(f, B, s, Omega, N=10, basis='a'):
    u = B
    for i in range(N+1):
        if basis == 'a':
            psi = [sym.sin((i+1)*x)]
        elif basis == 'b':
            psi = [sym.sin((2*i+1)*x)]
        elif basis == 'c':
            psi = [sym.sin(2*(i+1)*x)]
        next_term, c = least_squares_orth(f-B, psi, Omega, False)
        u = u + next_term
        # Make only plot for i even
        if i % 2 == 0:
            comparison_plot(f, u, Omega, 'tmp_sin%02dx' % i,
                            legend_loc='upper left', show=False,
                            plot_title='s=%g, i=%d' % (s, i))
Example #4
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])
            cmd += " tanh_Lagrange_%s_s%s" % (distribution, s)
            os.system(cmd)
Example #5
0
        for k in range(2, 6):
            if symbolic:
                Omega = [0, k*sym.pi] if domain_no == 1 else \
                        [-k*sym.pi/2, k*sym.pi/2]
            else:
                # cannot use sym.pi with numerical sympy computing
                Omega = [0, k*pi] if domain_no == 1 else \
                        [-k*pi/2, k*pi/2]

            u, c = least_squares(f, psi, Omega, symbolic=symbolic)

            comparison_plot(f,
                            u,
                            Omega,
                            ymin=-2,
                            ymax=2,
                            filename='tmp_N%d_V%dOmega%dk%d' %
                            (N, V, k, domain_no),
                            plot_title='sin(x) on [0,%d*pi/2] by %s' %
                            (k, ','.join([str(p) for p in psi])))
            # Need to kill the plot to proceed!
        for ext in 'png', 'pdf':
            cmd = 'doconce combine_images -2 ' + \
                  ' '.join(['tmp_N%d_V%dOmega%dk%d.' %
                            (N, V, k, domain_no) + ext
                            for k in range(2, 6)]) + \
                  ' sin_powers_N%d_V%d_Omega%d.' % (N, V, domain_no) + ext
            print(cmd)
            os.system(cmd)

# Show the standard Taylor series approximation
Example #6
0
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])
            cmd += ' tanh_Lagrange_%s_s%s' % (distribution, s)
            os.system(cmd)
Example #7
0
import sys, os
sys.path.insert(0, os.path.join(os.pardir, 'src-approx'))
from approx1D import least_squares, comparison_plot
import matplotlib.pyplot as plt
import sympy as sym
from math import factorial
import numpy as np

x = sym.Symbol('x')
f = sym.exp(-x)

Omega = [0, 8]

for N in 2,4,6:
    psi = [x**i for i in range(N+1)]
    u, c = least_squares(f,psi,Omega)
    print N, u
    plt.figure()
    comparison_plot(f, u, Omega, filename='tmp_exp_%d' % N,
                    plot_title='N=%d' % N)
Example #8
0
    for domain_no in range(1, 3):
        for k in range(2, 6):
            if symbolic:
                Omega = [0, k*sym.pi] if domain_no == 1 else \
                        [-k*sym.pi/2, k*sym.pi/2]
            else:
                # cannot use sym.pi with numerical sympy computing
                Omega = [0, k*pi] if domain_no == 1 else \
                        [-k*pi/2, k*pi/2]

            u, c = least_squares(f, psi, Omega, symbolic=symbolic)

            comparison_plot(
                f, u, Omega,
                ymin=-2, ymax=2,
                filename='tmp_N%d_V%dOmega%dk%d' %
                (N, V, k, domain_no),
                plot_title='sin(x) on [0,%d*pi/2] by %s' %
                (k, ','.join([str(p) for p in psi])))
            # Need to kill the plot to proceed!
        for ext in 'png', 'pdf':
            cmd = 'doconce combine_images -2 ' + \
                  ' '.join(['tmp_N%d_V%dOmega%dk%d.' %
                            (N, V, k, domain_no) + ext
                            for k in range(2, 6)]) + \
                  ' sin_powers_N%d_V%d_Omega%d.' % (N, V, domain_no) + ext
            print cmd
            os.system(cmd)

# Show the standard Taylor series approximation
from math import factorial, pi
Example #9
0
        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' %
                            (s, N, distribution),
                            points=points,
                            point_values=point_values,
                            points_legend='%s points' % (2 * N))
        # 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])
            cmd += ' tanh_Lagrange_regr_%s_s%s' % (distribution, s)
            os.system(cmd)
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' %
			    (s, N, distribution),
                            points=points, point_values=point_values,
                            points_legend='%s points' % (2*N))
        # 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])
            cmd += ' tanh_Lagrange_regr_%s_s%s' % (distribution, s)
            os.system(cmd)