def test_diff2fd24(): for n in 2**arange(4,11): #Create an evenly-spaced grid of n points x = linspace(-1,1,n) # Second-order differentiation matrix against second-order derivative D2 = diff2fd24(n,2,2./(n-1)) D4 = diff2fd24(n,4,2./(n-1)) du_true, u = testf(x,2) du2 = D2.dot(u) du4 = D4.dot(u) # Discrete L2-norm -- right-hand rule err2 = tools.normd(du2-du_true,x,'even') print("Discrete L2-error (2nd order) is %20.15e for n = %4d") % (err2,n) err4 = tools.normd(du4-du_true,x,'even') print("Discrete L2-error (4th order) is %20.15e for n = %4d") % (err4,n)
def test_diff2lag(): for n in 2**arange(4,7): #Create Chebyshev points (of the second kind) to interpolate x = cos(pi*linspace(0,n,n)/n) # Second-order differentiation matrix against second-order derivative D = diff2lag(x) du_true, u = testf(x,2) du = dot(D,u) # Discrete L2-norm -- right-hand rule err = tools.normd(du-du_true,x,'right') print("Discrete L2-error is %20.15e for n = %4d") % (err,n)
def test(): for n in 2**arange(4,7)-1: #Create Chebyshev points (of the second kind) to interpolate x = cos(pi*linspace(0,n,n)/n) y = testf(x) # Run the test ni = 2*(n+1) xi = linspace(-1+1./n**2,1-1./n**2,ni) yi = barylag(x,y,xi) ytrue = testf(xi) err = tools.normd(yi-ytrue,xi,'right') print("Discrete L2-error is %20.15e for nodes = %4d, points = %4d") % (err,n,ni)
def test(): t0 = -3 tf = 3 hvec = array([0.1, 0.01, 0.001]) # Euler Test y0 = sqrt(pi)/2*exp(-t0**2) print "Scalar Euler Test Results:\n" for h in hvec: y, tv = euler(testf,y0,t0,tf,h,True) ytrue = sqrt(pi)/2*exp(-tv**2) delta = tools.normd(y-ytrue,tv,'even') print("Discrete L2 error is %20.15e for h = %2.1e") % (delta, h) y0 = array([1, 2, 3])*sqrt(pi)/2*exp(-t0**2) delta = zeros(3) print "\nVector Euler Test Results:\n" for h in hvec: y, tv = euler(testf,y0,t0,tf,h,True) ytrue = sqrt(pi)/2*exp(-tv**2) delta[0] = tools.normd(y[:,0]-ytrue,tv,'even') delta[1] = tools.normd(y[:,1]-2*ytrue,tv,'even') delta[2] = tools.normd(y[:,2]-3*ytrue,tv,'even') print("Discrete L2 error is\n %20.15e, %20.15e, %20.15e\nfor h = %2.1e") % (delta[0],delta[1],delta[2], h) # Euler Predictor-Corrector Test y0 = sqrt(pi)/2*exp(-t0**2) print "Scalar Euler PC Test Results:\n" for h in hvec: y, tv = eulerpc(testf,y0,t0,tf,h,True) ytrue = sqrt(pi)/2*exp(-tv**2) delta = tools.normd(y-ytrue,tv,'even') print("Discrete L2 error is %20.15e for h = %2.1e") % (delta, h) y0 = array([1, 2, 3])*sqrt(pi)/2*exp(-t0**2) delta = zeros(3) print "\nVector Euler PC Test Results:\n" for h in hvec: y, tv = eulerpc(testf,y0,t0,tf,h,True) ytrue = sqrt(pi)/2*exp(-tv**2) delta[0] = tools.normd(y[:,0]-ytrue,tv,'even') delta[1] = tools.normd(y[:,1]-2*ytrue,tv,'even') delta[2] = tools.normd(y[:,2]-3*ytrue,tv,'even') print("Discrete L2 error is\n %20.15e, %20.15e, %20.15e\nfor h = %2.1e") % (delta[0],delta[1],delta[2], h) # RK4 Test y0 = sqrt(pi)/2*exp(-t0**2) print "Scalar RK4 Test Results:\n" for h in hvec: y, tv = RK4(testf,y0,t0,tf,h,True) ytrue = sqrt(pi)/2*exp(-tv**2) delta = tools.normd(y-ytrue,tv,'even') print("Discrete L2 error is %20.15e for h = %2.1e") % (delta, h) y0 = array([1, 2, 3])*sqrt(pi)/2*exp(-t0**2) delta = zeros(3) print "\nVector RK4 Test Results:\n" for h in hvec: y, tv = RK4(testf,y0,t0,tf,h,True) ytrue = sqrt(pi)/2*exp(-tv**2) delta[0] = tools.normd(y[:,0]-ytrue,tv,'even') delta[1] = tools.normd(y[:,1]-2*ytrue,tv,'even') delta[2] = tools.normd(y[:,2]-3*ytrue,tv,'even') print("Discrete L2 error is\n %20.15e, %20.15e, %20.15e\nfor h = %2.1e") % (delta[0],delta[1],delta[2], h)