# def func(x): # f = exp(-x**2) # f[x<0] *= -1 # return f n = 9 # number of data points # ========================= Tune this ========================= y = linspace(-5, 5, 250) fye = func(y) x = linspace(-5, 5, n) fx = func(x) # --------- use of interp1d class ---------- interp = interp_1d(x, fx, l=1, verbose=0) fy, sig = interp.interp(y, compute_df=True) # ------------------------------------------ pylab.figure(figsize=(10,6)) pylab.plot(y, fye, '--r') pylab.plot(y, fy, 'k') pylab.plot(y, fy + 3*sig, ':b') pylab.plot(y, fy - 3*sig, ':b') pylab.plot(x, fx, 'ok') pylab.ylim([fye.min()-0.2*(fye.max()-fye.min()), \ fye.max()+0.2*(fye.max()-fye.min())]) pylab.xlabel('$x$', verticalalignment='center', fontsize=16); pylab.ylabel('$f(x)$', horizontalalignment='right', fontsize=16) linf = abs(fy-fye).max()
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 = [], [] xall = [] for line in file('quasi-random/niederreiter_1d.txt').readlines(): xall.append(float(line)) xall = numpy.array(xall) * 10 - 5 for n in n_list: x = xall[:n] fx = func(x) fy, sig = interp_1d(y, x, fx, 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.plot(x, fx, 'ok') 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())]) pylab.xlabel('$x$', verticalalignment='center', fontsize=16); pylab.ylabel('$f(x)$', horizontalalignment='right', fontsize=16) pylab.savefig('output/%s/random_%s_1d_%03d.png' % (CASE, CASE, n)) linf.append(abs(fy-fye).max())