Exemplo n.º 1
0
import numpy
import pylab
from numpy import zeros, ones, linspace, kron, linalg, exp, sqrt, diag, \
                  arctan, pi

from arbinterp import interp_1d
from compinterp import lagrange_1d, linear_1d, spline_1d

CASE = 'runge'
def func(x):
  f = 1.0 / (1.0 + x**2)
  return f

pylab.matplotlib.interactive(False)
n = 10
x = linspace(-5, 5, n)
y = func(x)
y = interp_1d(y, x, fx)

Exemplo n.º 2
0
# my interpolation
linf, l2 = [], []

xall = []
for line in file('quasi-random/niederreiter_1d.txt').readlines():
  xall.append(float(line))
xall = numpy.array(xall) * 10 - 5
dxall = exp(numpy.random.normal(size=xall.size) * 0.5) * 0.02
exall = dxall * numpy.random.normal(size=xall.size)

for n in n_list:
  x = xall[:n]
  dx = dxall[:n]
  fx = func(x) + exall[:n]
  fy, sig = interp_1d(y, x, fx, dx, compute_dfz=True)
  pylab.figure(figsize=(5,3))
  pylab.plot(y, fye, '--k')
  pylab.plot(y, fy, 'k')
  # pylab.plot(y, fy + sig, ':b')
  # pylab.plot(y, fy - sig, ':b')
  # pylab.plot(y, fy_l, '-.g')
  # pylab.bar(x, x*0, bottom=fx, yerr=dx, width=0, ecolor='k')
  pylab.plot(numpy.array([x,x]), numpy.array([fx+dx, fx-dx]), color='k', \
             linewidth=3)
  # pylab.plot(x, fx+dx, 'v', color='k')
  # pylab.plot(x, fx-dx, '^', color='k')
  margin = 0.2
  if CASE == 'step': margin = 0.5
  pylab.ylim([fye.min()-margin*(fye.max()-fye.min()), \
              fye.max()+margin*(fye.max()-fye.min())])
Exemplo n.º 3
0
y = linspace(-5, 5, 250)
fye, fype = func(y)

pylab.figure(figsize=(5,3))
pylab.plot(y, fye, '-k')
pylab.ylim([fye.min()-0.2*(fye.max()-fye.min()), \
            fye.max()+0.2*(fye.max()-fye.min())])
pylab.savefig('output/%s/%s_exact.png' % (CASE, CASE))

# my interpolation
linf, l2 = [], []
linf_g, l2_g = [], []
for n in n_list:
  x = linspace(-5, 5, n)
  fx, fpx = func(x)
  fy_g, sig_g = interp_1d(y, x, fx, None, x, fpx, compute_dfz=True)
  fy, sig = interp_1d(y, x, fx, compute_dfz=True)
  pylab.figure(figsize=(5,3))
  pylab.plot(y, fye, '--k')
  pylab.plot(y, fy_g, 'k')
  # pylab.plot(y, fy + sig, ':b')
  # pylab.plot(y, fy - sig, ':b')
  # pylab.plot(y, fy_l, '-.g')
  pylab.plot(x, fx, 'ok')
  margin = 0.2
  pylab.ylim([fye.min()-margin*(fye.max()-fye.min()), \
              fye.max()+margin*(fye.max()-fye.min())])
  pylab.xlabel('$x$', verticalalignment='center', fontsize=16);
  pylab.ylabel('$f(x)$', horizontalalignment='center', fontsize=16)
  pylab.savefig('output/%s/uniform_%s_grad_1d_%03d.png' % (CASE, CASE, n))
  linf.append(abs(fy-fye).max())
Exemplo n.º 4
0
from compinterp import lagrange_1d

pylab.matplotlib.interactive(False)
gamma = 16.0
y = linspace(0, 1, 250)
fye = exp(-((y-0.5)*16)**2)

n_list = range(4, 50, 4)
linf, l2 = [], []

xall = (cos(random.random(size=n_list[-1]) * pi) + 1) / 2.0

for n in n_list:
  x = xall[:n]
  fx = exp(-((x-0.5)*16)**2)
  fy, sig = interp_1d(y, x, fx, gamma, compute_sigma=True)
  fy_l = lagrange_1d(y, x, fx)
  if abs(fy_l).max() > 1.0E3: fy_l[:] = 0.0
  sig[sig>1000] = 1000; sig[sig<-1000] = -1000
  pylab.figure()
  pylab.plot(y, fy, 'b')
  pylab.plot(y, fy + sig, ':b')
  pylab.plot(y, fy - sig, ':b')
  pylab.plot(y, fy_l, '-.g')
  pylab.plot(y, fye, '--r')
  pylab.plot(x, fx, '+')
  pylab.ylim([-0.2, 1.2])
  linf.append(abs(fy-fye).max())
  l2.append(sqrt(((fy-fye)**2).sum()))
  print n, linf[-1], l2[-1]