In [1]:
import numpy as np
import scipy as sp
import scipy.sparse as sparse
import scipy.sparse.linalg as sla
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
n = 64
h = 1.0 / n
A = sparse.diags([-1, 2, -1], [-1, 0, 1], shape=(n,n), format='csr')
b = np.zeros((n,))
In [4]:
def interpolate(vc):
    """interpolate v of size 2**(m-1)-1 to 2**(m)-1"""
    nc = len(vc)
    nf = 2**(int(np.log2(nc+1))+1)+1
    
    vf = np.zeros((nf,))
    
    vf[1::2] = 0.5 * vc[:-1] + 0.5 * vc[1:]
    vf[0::2] = vc
    return vf
In [6]:
m = 4
nf = 2**m + 1
nc = 2**(m-1) + 1

xc = np.linspace(0,1,nc+2)[1:-1]
#vc = np.sin(np.pi * xc)
vc = np.random.rand(nc)
plt.plot(xc, vc, 'bo-', clip_on=False)

xf = interpolate(xc)
vf = interpolate(vc)
plt.plot(xf, vf, 'go-', clip_on=False)
Out[6]:
[<matplotlib.lines.Line2D at 0x10af16a58>]
In [ ]:
 
In [ ]: