def exa3_44(needFigure=True): T = 10 N = int(1e4) a = 1 ne = 512 h = a / ne epsilon = 1e-3 x = np.linspace(0, a, ne + 1) u0 = np.exp(-(x - 0.5)**2 / epsilon) start = timer() [t, ut] = pde_fem(u0, T, a, N, ne, epsilon, fNagumo) end = timer() print("Run time", end - start, "secs") # if needFigure: plt.figure(1) ch0.PlotSetup() ax = plt.gca(projection='3d') [T, X] = np.meshgrid(t, x) ax.plot_surface(T, X, ut, cmap=cm.coolwarm, linewidth=0, antialiased=False) plt.axis('equal') ax.set_zlabel(r'$u$') ax.set_ylabel(r'$x$') ax.set_xlabel(r'$t$') plt.savefig('fig3_4a.pdf') return t, ut
def exa3_33(needFigure=True): T = 10 N = 1e4 epsilon = 1 a = 20 J = 128 h = a / J x = np.linspace(0, a, J + 1) u0 = 1 / (1 + np.exp(-(2 - x) / sqrt(2))) start = timer() [t, ut] = pde_fd(u0, T, a, N, J, epsilon, fNagumo, 'N') end = timer() print("Run time", end - start, "secs") [T, X] = np.meshgrid(t, x) # if (needFigure == True): plt.figure(1) ch0.PlotSetup() ax = plt.gca() ax = plt.Axes3D(fig) #ax.plot_wireframe(T,X,ut,rstride=16,cstride=1000,colors='k') CS = ax.contourf(X, T, ut, 10, cmap=plt.cm.bone) #ax.set_zlabel(r'$u$') ax.set_xlabel(r'$x$') ax.set_ylabel(r'$t$') plt.colorbar(CS) plt.savefig('fig3_4.pdf')
def uniform_mesh_info(ns): """ A2.4 Page 69. """ h = 1 / ns x = np.linspace(0, 1, ns + 1) y = np.copy(x) # co-ordinates of vertices xv, yv = np.meshgrid(x, y) xv = xv.ravel() yv = yv.ravel() n2 = ns * ns nvtx = (ns + 1) * (ns + 1) ne = 2 * n2 # global vertex labels of individual elements elt2vert = np.zeros((ne, 3), dtype='int') vv = np.reshape(np.arange(0, nvtx), (ns + 1, ns + 1), order='F') v1 = vv[0:ns, 0:ns] v2 = vv[1:, 0:ns] v3 = vv[0:ns, 1:] elt2vert[0:n2, :] = np.vstack((v1.ravel(), v2.ravel(), v3.ravel())).T v1 = vv[1:, 1:] elt2vert[n2:, :] = np.vstack((v1.ravel(), v3.ravel(), v2.ravel())).T plt.figure(2) # ch0.PlotSetup() plt.axis('equal') plt.triplot(xv.ravel(), yv.ravel(), elt2vert, 'k-') plt.xlabel(r'$x_1$') plt.ylabel(r'$x_2$') return xv, yv, elt2vert, nvtx, ne, h
def exa10_42(needFigure=True): T = 10 N = int(1e3) a = 1 ne = 512 h = a / ne epsilon = 1e-3 r = 1 M = 1 sigma = 0.1 x = np.linspace(0, a, ne + 1) u0 = np.exp(-(x - 0.5)**2 / epsilon) s0 = np.random.RandomState() t, u, ut = spde_fem_MhDt(u0, T, a, N, 1, ne, 1, epsilon, fNagumo, lambda u: sigma * u, r, M, s0) if needFigure: plt.figure(1) ch0.PlotSetup() ax = plt.gca(projection='3d') [T, X] = np.meshgrid(t, x) ax.plot_surface(T, X, ut, cmap=cm.coolwarm, linewidth=0, antialiased=False) ax.set_zlabel(r'$u$') ax.set_ylabel(r'$x$') ax.set_xlabel(r'$t$') plt.savefig('fig10_.pdf')
def exa10_40(needFigure=True): T=10; N=int(1e3); a=np.array([2*pi,16]); J=np.array([128,64]) alpha=0.1; epsilon=1e-3; sigma=0.1; M=2; kappa=1; x=np.linspace(0,a[0],J[0]+1); y=np.linspace(0,a[1],J[1]+1) xx,yy=np.meshgrid(x,y,indexing='ij') u0=np.sin(xx)*np.cos(pi*yy/8) u0=np.sin(yy)*np.cos(pi*xx/8) # t,u,ut=spde_twod_Gal(u0,T,a,N,kappa,J,epsilon, fAC, lambda u: sigma, alpha,M) if (needFigure==True): plt.figure(1) ch0.PlotSetup() ax = plt.gca() xx,yy=np.meshgrid(x,y) print(xx.shape,ut.shape,x.shape,y.shape) # ax = plt.gca(projection='3d') # ax.plot_surface(xx,yy,ut[:,:,-1], # cmap=cm.bone,linewidth=0, # antialiased=False) CS=ax.contourf(x,y,ut[:,:,-1].T,4,cmap=plt.cm.bone) plt.colorbar(CS) ax.set_xlabel(r'$x_1$') ax.set_ylabel(r'$x_2$') plt.savefig('fig10_11a.pdf')
def exa3_40(needFigure=True): T = 10 N = 1e3 a = [2 * pi, 16] J = [128, 256] epsilon = 1e-3 x = np.linspace(0, a[0], J[0] + 1) y = np.linspace(0, a[1], J[1] + 1) [xx, yy] = np.meshgrid(x, y, indexing='ij') # initial data printed in book #u0=np.sin(xx)*np.cos(pi*yy/8) # we think he intended this initial data, which gives Fig 3.8b u0 = np.sin(yy) * np.cos(pi * xx / 8) start = timer() [t, ut] = pde_twod_Gal(u0, T, a, N, J, epsilon, fAC) end = timer() print("Run time", end - start, "secs") if (needFigure == True): plt.figure(1) ch0.PlotSetup() ax = plt.gca(projection='3d') ax.plot_surface(xx, yy, ut[:, :, -1], cmap=cm.bone, linewidth=0, antialiased=False) plt.axis('equal') # ax.set_xlabel(r'$x$') ax.set_ylabel(r'$y$') ax.set_zlabel(r'$u$') # plt.savefig('fig3_5b.pdf')
def exa10_39(needFigure=True): T = 2 N = 50 a = 2 * pi J = 64 r = 1 epsilon = 0.001 sigma = 1 M = 2 kappa = 1 x = np.linspace(0, a, J + 1) u0 = np.sin(x) s0 = np.random.RandomState() tref, uref, ureft = spde_oned_Gal_MJDt(u0, T, a, N, kappa, J, J, epsilon, fAC, lambda u: sigma, r, M, s0) if (needFigure == True): plt.figure(1) ch0.PlotSetup() ax = plt.gca() ax = plt.gca(projection='3d') [T, X] = np.meshgrid(tref, x) CS = ax.plot_wireframe(T, X, ureft, rstride=3, cstride=3, colors='k') #CS=ax.contourf(tref,x,ureft,10,cmap=plt.cm.bone) #ax.set_zlabel(r'$u$') ax.set_xlabel(r'$t$') ax.set_ylabel(r'$x$') # plt.colorbar(CS) plt.savefig('fig10_.pdf')
def exa_fbm(H=0.1): T=10; N=100; t=np.linspace(0,T,N); X=fbm(t,H) plt.figure(1) ch0.PlotSetup() plt.plot(t,X,'k-') plt.xlabel(r'$t$') plt.ylabel(r'$B^H$') plt.savefig('fig5_fbm.pdf',bbox_inches='tight')
def exa_bm(): T=10; N=100; t=np.linspace(0,T,N) X=bmotion(t) plt.figure(1) ch0.PlotSetup() plt.plot(t,X,'k-') plt.xlabel(r'$t$') plt.ylabel(r'X') plt.savefig('fig5_bm.pdf',bbox_inches='tight')
def fig6_7(): t = np.linspace(0, 20, 200) Z = quad_sinc(t, 100, 2) plt.figure(1) ch0.PlotSetup() plt.plot(t, np.real(Z)) plt.plot(t, np.imag(Z)) plt.xlabel(r'$t$') plt.ylabel(r'$Z$') plt.savefig('fig6_7b.pdf', bbox_inches='tight')
def fig6_9(): N = int(1e3) dt = 1 / (N - 1) t, X, Y = circulant_exp(N, dt, 1) ch0.PlotSetup() plt.plot(t, X) plt.plot(t, Y) plt.xlabel(r'$t$') plt.ylabel(r'$X$') plt.savefig('fig6_9.pdf', bbox_inches='tight')
def exa10_10(): dtref=0.01; kappa=100; r=1/2; J=128; a=1; bj=get_onedD_bj(dtref,J,a,r) s0=np.random.RandomState() dW=get_onedD_dW(bj,kappa,0,1,s0) x=np.linspace(0,a,J-1) plt.figure(1) ch0.PlotSetup() plt.plot(x,np.squeeze(dW)) plt.xlabel(r'$t$') plt.ylabel(r'$dW$') plt.savefig('fig10_.pdf',bbox_inches='tight')
def fig7_10c(): grid = np.linspace(0, 10, 200) u = turn_band_wm(grid, grid, 10, 1, 1) plt.figure(1) ch0.PlotSetup() ax = plt.gca() #ax.plot_wireframe(T,X,ut,rstride=16,cstride=1000,colors='k') CS = ax.contourf(grid, grid, u, 10, cmap=plt.cm.bone) #ax.set_zlabel(r'$u$') ax.set_xlabel(r'$y$') ax.set_ylabel(r'$x$') plt.colorbar(CS) plt.savefig('fig7_10c.pdf', bbox_inches='tight')
def exa3_8(): u0 = np.array([0.5, 0.1]) T = 1 Nref = 1e6 d = 2 kappa = np.array([10, 1e2, 1e3, 1e4, 1e5]) [dt, errA, errB] = exp_euler_conv(u0, T, Nref, d, f, kappa) plt.figure(1) ch0.PlotSetup() plt.xlabel(r'$\Delta t$') plt.ylabel(r'error') plt.loglog(dt, errA, '-k') plt.savefig('fig3_2.pdf', bbox_inches='tight')
def exa10_43b(): T=1; N=int(1e3); a=1; r=1; sigma=0.5; epsilon=1e-3; M=1; Nref=1e5; ne=32; h=a/ne; x=np.linspace(0,a,ne+1) u0=np.exp(-(x-0.5)**2/epsilon) kappa=np.array([5,10,20,50,100,200,500]) dt,errT=spde_fem_convDt(u0,T,a,Nref,kappa,ne,epsilon, fNagumo, lambda u:gquadratic(u,sigma), r,M) plt.figure(1) ch0.PlotSetup() plt.loglog(dt,errT,'-k*') plt.xlabel(r'$\Delta t$') plt.ylabel(r'error') plt.savefig('fig10_.pdf',bbox_inches='tight')
def exa10_31(): T=0.1; epsilon=0.1; sigma=1; a=1; M=int(1e4); J=4*2**np.array([2,3,4]); N=0.25*J**2; v=np.zeros(N.shape) for i in range(N.size): u0=np.zeros(J[i]+1) v[i]=l2_sq_mct(T,a,int(N[i]),J[i],M,epsilon,sigma) plt.figure(1) ch0.PlotSetup() plt.plot(N,v) plt.xlabel(r'$J$') plt.ylabel(r'L2 SQ$') plt.title(r'Converges to 0.3489') plt.savefig('exa10_31.pdf',bbox_inches='tight')
def exa8_23(): lam = 1 alpha = 1 sigma = 1 u0 = np.array([0.5, 0]) T = 10 N = 1000 [t, u] = vdp(u0, T, N, alpha, lam, sigma) plt.figure(1) ch0.PlotSetup() plt.plot(t, u[0, :]) plt.plot(t, u[1, :]) plt.xlabel(r'$t$') plt.ylabel(r'$u$') plt.savefig('fig8_5.pdf', bbox_inches='tight')
def exa10_43(): T=1; N=int(1e4); a=1; r=1; sigma=0.5; epsilon=1e-3; neref=512; href=a/neref; x=np.linspace(0,a,neref+1) u0=np.exp(-(x-0.5)**2/epsilon) L=np.array([2,4,8,16,32],dtype='int32') M=1 h,errh=spde_fem_convh(u0,T,a,N,neref,L,epsilon, fNagumo, lambda u: gquadratic(u,sigma), r,M) plt.figure(1) ch0.PlotSetup() plt.loglog(h,errh,'k-*') plt.xlabel(r'$h$') plt.ylabel(r'error') plt.savefig('fig10_.pdf',bbox_inches='tight')
def exa7_41(): fhandle1 = lambda x1, x2: sep_exp(x1, x2, 1 / 5, 1 / 10) C_red = reduced_cov(201, 401, 1 / 200, 1 / 200, fhandle1) u1, u2 = circ_embed_sample_2d(C_red, 201, 401) plt.figure(1) ch0.PlotSetup() ax = plt.gca() x = np.linspace(0, 1, 201) y = np.linspace(0, 2, 401) #ax.plot_wireframe(T,X,ut,rstride=16,cstride=1000,colors='k') CS = ax.contourf(y, x, u1, 10, cmap=plt.cm.bone) #ax.set_zlabel(r'$u$') ax.set_xlabel(r'$y$') ax.set_ylabel(r'$x$') plt.colorbar(CS) plt.savefig('fig7_6.pdf', bbox_inches='tight')
def exa10_12(): J=[32,16]; dtref=0.01; kappa=100; a=[2*pi,3*pi] alpha=0.05; bj=get_twod_bj(dtref,J,a,alpha) W1,W2=get_twod_dW(bj,kappa,1) plt.figure(1) ch0.PlotSetup() gridx=np.linspace(0,a[0],J[0]) gridy=np.linspace(0,a[1],J[1]) ax = plt.gca() #ax.plot_wireframe(T,X,ut,rstride=16,cstride=1000,colors='k') CS=ax.contourf(gridy,gridx,W1[0,:,:],10,cmap=plt.cm.bone) #ax.set_zlabel(r'$u$') ax.set_xlabel(r'$x$') ax.set_ylabel(r'$y$') plt.colorbar(CS) plt.savefig('fig10_.pdf')
def fig6_6(): T = 30 N = 2**9 t = np.linspace(0, T, N) M = N / 4 vector_q = [0.5, 1, 1.5, 2] ch0.PlotSetup() for i in range(1, 5): q = vector_q[i - 1] X, Y = quad_wm(t, N, M, q) plt.subplot(2, 2, i) plt.plot(t, X) plt.plot(t, Y) plt.xlabel(r'$t$') plt.ylabel(r'$X$') plt.savefig('fig6_6.pdf', bbox_inches='tight')
def exa10_30(needFigure=True): a=20; J=1024; x=np.linspace(0,a,J+1); u0=1/(1+np.exp(-(2-x)/sqrt(2))); ell=1; N=int(1e3); T=1; epsilon=1; sigma=1; t,ut=spde_fd_n_exp(u0,T,a,N,J,epsilon,sigma,ell, fNagumo) [T,X]=np.meshgrid(t,x) # if (needFigure==True): plt.figure(1) ch0.PlotSetup() ax = plt.gca() #ax.plot_wireframe(T,X,ut,rstride=16,cstride=1000,colors='k') CS=ax.contourf(X,T,ut,10,cmap=plt.cm.bone) #ax.set_zlabel(r'$u$') ax.set_xlabel(r'$x$') ax.set_ylabel(r'$t$') plt.colorbar(CS) plt.savefig('fig10_.pdf')
def exa3_41b(): epsilon = 1 a = 2 * pi Jref = 1024 N = 1e4 x = np.linspace(0, a, Jref + 1) u0 = np.sin(4 * x) * np.sin(x) J = Jref * np.array([1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32, 1 / 64]) T = 10 errJ = pde_oned_Gal_convJ(u0, T, a, N, Jref, J, epsilon, fAC) plt.figure(1) ch0.PlotSetup() plt.loglog(J, errJ, '-k*') plt.xlabel(r'$J$') plt.ylabel(r'error') plt.savefig('fig3_6b.pdf', bbox_inches='tight')
def exa3_41(): epsilon = 1 a = 2 * pi J = 1024 x = np.linspace(0, a, J + 1) u0 = np.sin(4 * x) * np.sin(x) kappa = np.array([5, 10, 20, 50, 100, 200], dtype='int') T = 10 Nref = int(1e4) [dt, errT] = pde_oned_Gal_convDt(u0, T, a, Nref, kappa, J, epsilon, fAC) plt.figure(1) ch0.PlotSetup() plt.loglog(dt, errT, '-k*') plt.xlabel(r'$\Delta t$') plt.ylabel(r'error') plt.savefig('fig3_6a.pdf', bbox_inches='tight')
def exa8_25(): d = 2 m = 2 A = np.array([[1, 0], [0, -1]]) sigma = np.array([1, 2]) T = 1 N = 5000 u0 = np.array([1, 1]) t, u = MilsteinDiag(u0, T, N, d, m, lambda u: A.dot(u), lambda u: sigma * u, lambda u: sigma) plt.figure(1) ch0.PlotSetup() plt.plot(t, u[0, :]) plt.plot(t, u[1, :]) plt.xlabel(r'$t$') plt.ylabel(r'$u$') plt.savefig('fig8_6.pdf', bbox_inches='tight')
def fig1_6(): N = 10 J = np.zeros(N) l2_norm_1 = np.copy(J) l2_norm_2 = np.copy(J) h1_norm_1 = np.copy(J) h1_norm_2 = np.copy(J) a = 0 b = 1 # domain for j in range(10): N = int(pow(2, (j + 3))) J[j] = N l2_norm_1[j], h1_norm_1[j] = get_norm(f_u1, a, b, N) for j in range(10): N = int(pow(2, (j + 3))) l2_norm_2[j], h1_norm_2[j] = get_norm(f_u2, a, b, N) # get symbolic values [l2_norm_sym_1, h1_norm_sym_1] = sp_u1(a, b) [l2_norm_sym_2, h1_norm_sym_2] = sp_u2(a, b) # computer errors el2_1 = abs(l2_norm_1 - l2_norm_sym_1) ehs_1 = abs(h1_norm_1 - h1_norm_sym_1) el2_2 = abs(l2_norm_2 - l2_norm_sym_2) ehs_2 = abs(h1_norm_2 - h1_norm_sym_2) # plotting plt.figure(1) ch0.PlotSetup() plt.axis('equal') plt.subplot(1, 2, 1) # plt.loglog(J, el2_1, 'k-') plt.loglog(J, ehs_1, 'k-.') plt.xlabel(r'$J$') plt.ylabel(r'error') plt.title(r'(a)') # plt.subplot(1, 2, 2) plt.loglog(J, el2_2, 'k-') plt.loglog(J, ehs_2, 'k-.') plt.xlabel(r'$J$') plt.title(r'(b)') # plt.tight_layout()
def example_p58(): plt.figure(1) ch0.PlotSetup() for i in range(1, 5): plt.subplot(2, 2, i) ne = pow(2, i) uh, A, b, K, M = oned_linear_FEM(ne, np.ones(ne), 10 * np.ones(ne), np.ones(ne)) plt.plot(np.linspace(0, 1, ne + 1), uh, '-ok') plt.xlabel(r'$x$') plt.ylabel(r'$u$') xx = np.linspace(0, 1, 100) uex = exact_exa2_3(xx) plt.plot(xx, uex, '-k') plt.tight_layout() plt.savefig('fig2_6.pdf', bbox_inches='tight') error = test_FEM_error(ne, uh) print('error', error)
def exa8_28(): u0 = 1 T = 10 N = 40 d = 1 m = 1 theta = 1 r = -8 sigma = 3 t, u = EulerMaruyamaTheta(u0, T, N, d, m, lambda u: r * u, lambda u: sigma * u, theta) plt.figure(1) ch0.PlotSetup() plt.plot(t, u[0, :]) plt.xlabel(r'$t$') plt.ylabel(r'$u$') plt.savefig('fig8_8.pdf', bbox_inches='tight')
def exa3_5(flag=1): """ flag=1 Explicit Euler (default) flag=0 Implicit Euler """ u0 = [0.5, 0.1] T = 10 N = 1000 d = 2 if (flag == 1): [t, u] = exp_euler(u0, T, N, d, f) else: [t, u] = imp_euler(u0, T, N, d, f) plt.figure(1) ch0.PlotSetup() plt.plot(t, u[1, :], 'k-') plt.xlabel(r'$t$') plt.ylabel(r'$u_1$') plt.savefig('fig3_1.pdf', bbox_inches='tight')
def exa3_45(): T = 1 N = int(1e4) kappa = 1 epsilon = 1e-3 neref = 512 a = 1 L = np.array([2, 4, 8, 16, 32], dtype='int') href = a / neref x = np.linspace(0, a, neref + 1) u0 = np.exp(-(x - 0.5)**2 / epsilon) h, errh = pde_fem_convh(u0, T, a, N, neref, L, epsilon, fNagumo) plt.figure(1) ch0.PlotSetup() plt.loglog(h, errh, 'k-*') plt.xlabel(r'$h$') plt.ylabel(r'error') plt.savefig('fig3_7b.pdf', bbox_inches='tight')