def Exercise1(): x_subintervals, t_subintervals = 6, 10 x_interval, t_final = [0, 1], .4 init_conditions = 2. * np.maximum( 1. / 5 - np.abs(np.linspace(0, 1, x_subintervals + 1) - 1. / 2), 0.) x, u = heatexplicit(init_conditions, x_subintervals, t_subintervals, x_interval, T=t_final, flag3d="on", nu=.05) # # Generates the figures in the heat flow lab # view = [-.1, 1.1,-.1, .5] # plt.plot(x,u[:,0],'-k',linewidth=2.0) # Initial Conditions # plt.plot(x,u[:,0],'ok',linewidth=2.0) # plt.axis(view) # plt.savefig('heatexercise1a.pdf') # plt.clf() # # plt.plot(x,u[:,-1],'-k',linewidth=2.0) # State at t = .2 # plt.plot(x,u[:,-1],'ok',linewidth=2.0) # plt.axis(view) # plt.savefig('heatexercise1b.pdf') # plt.clf() Data = x, u[:, ::1] Data = Data[0], Data[1].T[0:-1, :] time_steps = Data[1].shape[0] interval_length = 50 view = [-.1, 1.1, -.1, .5] math_animation(Data, time_steps, view, interval_length)
def Exercise1(): x_subintervals, t_subintervals = 6,10 x_interval,t_final = [0,1], .4 init_conditions = 2.*np.maximum(1./5 - np.abs(np.linspace(0,1,x_subintervals+1)-1./2),0.) x,u = heatexplicit(init_conditions,x_subintervals,t_subintervals,x_interval,T=t_final,flag3d="on",nu=.05) # # Generates the figures in the heat flow lab # view = [-.1, 1.1,-.1, .5] # plt.plot(x,u[:,0],'-k',linewidth=2.0) # Initial Conditions # plt.plot(x,u[:,0],'ok',linewidth=2.0) # plt.axis(view) # plt.savefig('heatexercise1a.pdf') # plt.clf() # # plt.plot(x,u[:,-1],'-k',linewidth=2.0) # State at t = .2 # plt.plot(x,u[:,-1],'ok',linewidth=2.0) # plt.axis(view) # plt.savefig('heatexercise1b.pdf') # plt.clf() Data = x,u[:,::1] Data = Data[0], Data[1].T[0:-1,:] time_steps = Data[1].shape[0] interval_length=50 view = [-.1, 1.1,-.1, .5] math_animation(Data,time_steps,view,interval_length)
def Exercise2(): x_subintervals, t_subintervals = 140., 70. x_interval, t_final = [-12, 12], 1. init_conditions = np.maximum( 1. - np.linspace(-12, 12, x_subintervals + 1)**2, 0.) x, u = heatexplicit(init_conditions, x_subintervals, t_subintervals, x_interval, T=t_final, flag3d="off", nu=1.) # # Generates a figure in the heat flow lab # view = [-12, 12,-.1, 1.1] # plt.plot(x,u[:,0],'-k',linewidth=2.0,label="Initial State") # plt.plot(x,u[:,-1],'--k',linewidth=2.0,label="State at time $t=1$.") # State at t = .2 # plt.xlabel("x",fontsize=16) # plt.legend(loc=1) # plt.axis(view) # plt.savefig('heatexercise2.pdf') # plt.show() # plt.clf() # Animation of results view = [-12, 12, -.1, 1.1] Data = x, u[:, ::2] Data = Data[0], Data[1].T[0:-1, :] time_steps = Data[1].shape[0] interval_length = 30 math_animation(Data, time_steps, view, interval_length)
def Exercise2(): x_subintervals, t_subintervals = 140.,70. x_interval,t_final = [-12,12], 1. init_conditions = np.maximum(1.-np.linspace(-12,12,x_subintervals+1)**2,0.) x,u = heatexplicit(init_conditions,x_subintervals,t_subintervals,x_interval,T=t_final,flag3d="off",nu=1.) # # Generates a figure in the heat flow lab # view = [-12, 12,-.1, 1.1] # plt.plot(x,u[:,0],'-k',linewidth=2.0,label="Initial State") # plt.plot(x,u[:,-1],'--k',linewidth=2.0,label="State at time $t=1$.") # State at t = .2 # plt.xlabel("x",fontsize=16) # plt.legend(loc=1) # plt.axis(view) # plt.savefig('heatexercise2.pdf') # plt.show() # plt.clf() # Animation of results view = [-12, 12,-.1, 1.1] Data = x,u[:,::2] Data = Data[0], Data[1].T[0:-1,:] time_steps = Data[1].shape[0] interval_length=30 math_animation(Data,time_steps,view,interval_length) return
def animate_heat1d(): Domain = 10. Data = heatexplicit(J=320,nu=1.,L=Domain,T=1.,flag3d="animation") Data = Data[0], Data[1].T[0:-1,:] time_steps, view = Data[1].shape[0], [-Domain, Domain,-.1, 1.5] math_animation(Data,time_steps,view) return
def plot3d(): T, L, J, nu = 1., 10., 320, 1. flag3d = "3d_plot" X,U = heatexplicit(J,nu,L,T,flag3d) N = U.shape[1]-1 # #Produce 3D plot of solution xv,tv = np.meshgrid(np.linspace(-L,L,J/4+1), np.linspace(0,T,N/8+1)) Z = U[::4,::8].T fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(tv, xv, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) ax.set_xlabel('Y'); ax.set_ylabel('X'); ax.set_zlabel('Z') plt.show() return
def plot_error(): # Compare solutions for various discretizations X, U, L = [], [], 10. List, LineStyles = [40,80,160,320,640,1280,2560], ['--k','-.k',':k','-k','-k','-k'] for points in List: x,u = heatexplicit(J=points,nu=1.,L=10.,T=1.,flag3d="off") X.append(x); U.append(u[:,-1]) del x, u print points # # Plot of solutions for varying discretizations # for j in range(0,len(LineStyles)): plt.plot(X[j],U[j],LineStyles[j],linewidth=1) # plt.axis([-10,10,-.1,.4]) # plt.show() # plt.clf() h = [2.*L/item for item in List[:-1]] def graph(X,U,List,h): # # Error Chart 1: Plot approximate error for each solution # for j in range(0,len(List)-1): plt.plot(X[j],abs(U[-1][::2**(6-j)]-U[j]),'-k',linewidth=1.2) for j in range(0,len(List)-1): plt.plot(X[j],abs(U[-1][::List[-1]/List[j]]-U[j]),'-k',linewidth=1.2) plt.savefig("ApproximateError.pdf") plt.clf() # # Error Chart 2: Create a log-log plot of the max error of each solution # MaxError = [max(abs(U[-1][::2**(6-j)]-U[j] )) for j in range(0,len(List)-1)] MaxError = [max(abs(U[-1][::List[-1]/List[j]]-U[j] )) for j in range(0,len(List)-1)] plt.loglog(h,MaxError,'-k',h,MaxError,'ko') plt.ylabel("Max Error") plt.xlabel('$\Delta x$') h, MaxError = np.log(h)/np.log(10),np.log(MaxError)/np.log(10) print '\n'*2, "Approximate order of accuracy = ", (MaxError[-1]-MaxError[0])/(h[-1]-h[0]) plt.savefig("MaximumError.pdf") # plt.show() return graph(X,U,List,h) return