Пример #1
0
def func0():
	nu, L = .1, 1.
	A, B, T = -L, L, 2.
	time_steps = 200
	delta_t = T/time_steps
	u_m,u_p = 5.,1.
	s, a = (u_m + u_p)/2., (u_m - u_p)/2.
	
	N = 200
	D,x = cheb(N)
	M = D.dot(D)
	M[0,:], M[-1,:] = 0., 0.
	M[0,0], M[-1,-1] = 1., 1.
	D2 = D[1:-1,:]
	
	bcs = np.zeros(N+1)
	bcs[0], bcs[-1] = 1., 5.
	def cap_F(u): 
		out = M.dot(u) - bcs
		out[1:-1] += (L/nu)*(s-u[1:-1])*D2.dot(u)
		return out
	
	guess = s - a*np.tanh(.6*(a/(2.*nu))*x)
	sol = root(cap_F, guess )
	
	if sol.success: 
		soln = s - a*np.tanh((a/(2.*nu))*(1./2)*(B+A + (B-A)*x))
		plt.plot((1./2)*(B+A + (B-A)*x),soln,'-k',linewidth=2.0,label='True solution')
		plt.plot((1./2)*(B+A + (B-A)*x),sol.x,'-r',linewidth=2.0,label='Numerical solution')
		
		plt.plot(x,guess,'*r',linewidth=2.0,label='Guess')
		plt.axis([-L,L,.5,5.5])
		plt.legend(loc='best')
		plt.show()
	return 
Пример #2
0
def func0():
	nu, L = .1, 1.
	A, B, T = -L, L, 2.
	time_steps = 200
	delta_t = T/time_steps
	u_m,u_p = 5.,1.
	s, a = (u_m + u_p)/2., (u_m - u_p)/2.

	N = 200
	D,x = cheb(N)
	M = D.dot(D)
	M[0,:], M[-1,:] = 0., 0.
	M[0,0], M[-1,-1] = 1., 1.
	D2 = D[1:-1,:]

	bcs = np.zeros(N+1)
	bcs[0], bcs[-1] = 1., 5.
	def cap_F(u):
		out = M.dot(u) - bcs
		out[1:-1] += (L/nu)*(s-u[1:-1])*D2.dot(u)
		return out

	guess = s - a*np.tanh(.6*(a/(2.*nu))*x)
	sol = root(cap_F, guess )

	if sol.success:
		soln = s - a*np.tanh((a/(2.*nu))*(1./2)*(B+A + (B-A)*x))
		plt.plot((1./2)*(B+A + (B-A)*x),soln,'-k',linewidth=2.0,label='True solution')
		plt.plot((1./2)*(B+A + (B-A)*x),sol.x,'-r',linewidth=2.0,label='Numerical solution')

		plt.plot(x,guess,'*r',linewidth=2.0,label='Guess')
		plt.axis([-L,L,.5,5.5])
		plt.legend(loc='best')
		plt.show()
	return
def exercise2():
	# Solves the Poisson boundary value problem u''(x) = f(x), u(-1) = u(1) = 0
	def f(x): return np.exp(2.*x) #np.exp(4.*x)
	
	def anal_sol(x): 
		# return (np.exp(4.*x) - np.sinh(4.)*x - np.cosh(4.))/16.
		return (np.exp(2.*x) - np.sinh(2.)*x - np.cosh(2.))/4.
	
	N = 3
	D,x = cheb(N)
	
	A = np.dot(D,D)[1:N:1,1:N:1]
	u = np.zeros(x.shape); u[1:N] = solve(A,f(x[1:N:1]))
	
	# print "\nDx = \n", x[1:N:1]
	# print "\nA = \n", A
	
	xx = np.linspace(-1,1,50)
	uu = (np.poly1d(np.polyfit(x,u,N)))(xx)
	
	plt.plot(x,u,'*r'); plt.plot(xx,uu,'-r')
	plt.plot(xx,anal_sol(xx),'-k')
	plt.axis([-1.,1.,-2.5,.5]); plt.show()
	print "Max error is ", np.max(np.abs(uu - anal_sol(xx)))
	return 
Пример #4
0
def Burgers():
	nu, L = .1, 2.
	u_m,u_p = 5.,1.
	s, a = (u_m + u_p)/2., (u_m - u_p)/2.

	def guess(x):
		return s - a*np.tanh(a/(2.*nu)*x)

	N = 50
	D,x = cheb(N)
	M = D.dot(D)
	M[0,:], M[-1,:] = 0., 0.
	M[0,0], M[-1,-1] = 1., 1.
	D2 = D[1:-1,:]

	u_bcs = np.zeros(N+1)
	y_bcs = np.zeros(N+1)
	u_bcs[0], u_bcs[-1] = u_p, s
	y_bcs[0], y_bcs[-1] = u_m, s
	def cap_F(u):
		out = np.zeros(2*(N+1))
		out1, out2 = out[:N+1], out[N+1:]
		u1, u2 = u[:N+1], u[N+1:]

		out1 += M.dot(u1) - u_bcs
		out1[1:-1] += (.5*L/nu)*(s - u1[1:-1])*D2.dot(u1)
		out2 += M.dot(u2) - y_bcs
		out2[1:-1] -= (.5*L/nu)*(s - u2[1:-1])*D2.dot(u2)
		return out

	array = np.empty(2*(N+1))
	array[:N+1] = guess(.4*L/2.*(x+1.))
	array[N+1:] = guess(-(.3)*L/2.*(x+1.))

	sol = root(cap_F, array )
	if sol.success:
		plt.plot(x,sol.x[:N+1],'-*k',linewidth=2.)
		plt.plot(x,sol.x[N+1:],'-*k',linewidth=2.)
		# plt.plot(x,array[:N+1],'-*r')
		# plt.plot(x,array[N+1:],'-*r')
		plt.axis([-1,1,u_p-.5, u_m+.5])
		plt.show()
		x_orig = L/2.*(x+1.)
		plt.plot(x_orig,sol.x[:N+1],'-ok',markersize=3.,linewidth=2.,label='Numerical solution')
		plt.plot(-x_orig,sol.x[N+1:],'-ok',markersize=3.,linewidth=2.)
		plt.plot(x_orig,array[:N+1],'-or',markersize=3.,linewidth=2.,label='Guess')
		plt.plot(-x_orig,array[N+1:],'-or',markersize=3.,linewidth=2.)
		plt.axis([-L,L,u_p-.5, u_m+.5])
		plt.legend(loc='best')
		plt.show()
	return
Пример #5
0
def Burgers():
	nu, L = .1, 2.
	u_m,u_p = 5.,1.
	s, a = (u_m + u_p)/2., (u_m - u_p)/2.
	
	def guess(x): 
		return s - a*np.tanh(a/(2.*nu)*x)
	
	N = 50
	D,x = cheb(N)
	M = D.dot(D)
	M[0,:], M[-1,:] = 0., 0.
	M[0,0], M[-1,-1] = 1., 1.
	D2 = D[1:-1,:]
	
	u_bcs = np.zeros(N+1)
	y_bcs = np.zeros(N+1)
	u_bcs[0], u_bcs[-1] = u_p, s
	y_bcs[0], y_bcs[-1] = u_m, s
	def cap_F(u): 
		out = np.zeros(2*(N+1))
		out1, out2 = out[:N+1], out[N+1:]
		u1, u2 = u[:N+1], u[N+1:]
		
		out1 += M.dot(u1) - u_bcs
		out1[1:-1] += (.5*L/nu)*(s - u1[1:-1])*D2.dot(u1)
		out2 += M.dot(u2) - y_bcs
		out2[1:-1] -= (.5*L/nu)*(s - u2[1:-1])*D2.dot(u2)
		return out
	
	array = np.empty(2*(N+1))
	array[:N+1] = guess(.4*L/2.*(x+1.))
	array[N+1:] = guess(-(.3)*L/2.*(x+1.))
	
	sol = root(cap_F, array )
	if sol.success:
		plt.plot(x,sol.x[:N+1],'-*k',linewidth=2.)
		plt.plot(x,sol.x[N+1:],'-*k',linewidth=2.)
		# plt.plot(x,array[:N+1],'-*r')
		# plt.plot(x,array[N+1:],'-*r')
		plt.axis([-1,1,u_p-.5, u_m+.5])
		plt.show()
		x_orig = L/2.*(x+1.)
		plt.plot(x_orig,sol.x[:N+1],'-ok',markersize=3.,linewidth=2.,label='Numerical solution')
		plt.plot(-x_orig,sol.x[N+1:],'-ok',markersize=3.,linewidth=2.)
		plt.plot(x_orig,array[:N+1],'-or',markersize=3.,linewidth=2.,label='Guess')
		plt.plot(-x_orig,array[N+1:],'-or',markersize=3.,linewidth=2.)
		plt.axis([-L,L,u_p-.5, u_m+.5])
		plt.legend(loc='best')
		plt.show()
	return 
Пример #6
0
def nonzeroDirichlet():
    # Solves the Poisson boundary value problem
    # u''(x) + u'(x) = f(x),
    # u(-1) = a
    # u(1) = b
    a, b = 2, -1

    def f(x):
        return -(b - a) / 2. * np.ones(x.shape) + np.exp(3. * x)

    def G(x):
        return (b - a) / 2. * np.ones(x.shape) * x + (b + a) / 2. * np.ones(
            x.shape)

    def anal_sol(x):
        # out = ( np.exp(-x)/( 12.*(-1 +np.exp(1))*(1 + np.exp(1))*np.exp(3))  *
        # (-12.*a*np.exp(x+3.)+12.*a*np.exp(4) + 12.*b*np.exp(x+5)-12*b*np.exp(4) -
        #   np.exp(4*x+3) + np.exp(4*x+5) - np.exp(x+8 )+ np.exp(x)- np.exp(1) + np.exp(7)
        #   )
        # 	  )
        N, B = np.array([[1., np.exp(1)],
                         [1, np.exp(-1)]]), np.array([[a - np.exp(-3) / 12.],
                                                      [b - np.exp(3) / 12.]])
        C = solve(N, B)
        return C[0] + C[1] * np.exp(-x) + np.exp(3. * x) / 12.

    N = 7
    D, x = cheb(N)

    A = np.dot(D, D)[1:N:1, 1:N:1]
    u = np.zeros(x.shape)
    u[1:N] = solve(A + D[1:N:1, 1:N:1], f(x[1:N:1]))

    xx = np.linspace(-1, 1, 50)
    uu = (np.poly1d(np.polyfit(x, u, N)))(xx)
    print "Max error is ", np.max(np.abs(uu + G(xx) - anal_sol(xx)))

    # plt.plot(x,u+G(x), '*k')
    # plt.plot(xx,uu+G(xx), '-r')
    plt.plot(xx, anal_sol(xx))
    plt.xlabel('$x$')
    plt.ylabel('$u$')
    # plt.axis([-1.,1.,-2.5,.5])
    plt.savefig('nonzeroDirichlet.pdf')
    plt.clf()
def deriv_matrix_exercise1():
	
	def f(x): return np.exp(x)*np.cos(6.*x)
	
	def fp(x): return np.exp(x)*(np.cos(6.*x) - 6.*np.sin(6.*x))
	
	N = 10
	D,x = cheb(N)
	f_exact, fp_exact = f(x), fp(x)
	xx = np.linspace(-1,1,200)
	plt.plot(xx,fp(xx),'-k') # Exact Derivative
	
	uu = (np.poly1d(np.polyfit(x, D.dot(f(x) ), N)))(xx)
	
	plt.plot(x,D.dot(f(x)),'*r')	# Approximation to derivative at grid points
	plt.plot(xx,uu,'-r')			# Approximation to derivative at other values
	plt.show()
	print "max error = \n", np.max(np.abs( D.dot(f(x)) - fp(x)   ))
	return 
Пример #8
0
def nonzeroDirichlet():
	# Solves the Poisson boundary value problem 
	# u''(x) + u'(x) = f(x), 
	# u(-1) = a
	# u(1) = b
	a, b = 2, -1
	
	def f(x): 
            return -(b-a)/2.*np.ones(x.shape)+ np.exp(3.*x)
	
	def G(x): 
            return (b-a)/2.*np.ones(x.shape) *x + (b+a)/2.*np.ones(x.shape)
	
	def anal_sol(x): 
		# out = ( np.exp(-x)/( 12.*(-1 +np.exp(1))*(1 + np.exp(1))*np.exp(3))  * 
		# (-12.*a*np.exp(x+3.)+12.*a*np.exp(4) + 12.*b*np.exp(x+5)-12*b*np.exp(4) - 
		#   np.exp(4*x+3) + np.exp(4*x+5) - np.exp(x+8 )+ np.exp(x)- np.exp(1) + np.exp(7) 
		#   ) 
		# 	  )
		N, B = np.array([[1.,np.exp(1)],[1,np.exp(-1)]]), np.array([[a-np.exp(-3)/12.], [b-np.exp(3)/12.]])
		C = solve(N,B)
		return C[0] + C[1]*np.exp(-x) + np.exp(3.*x)/12. 
	
	
	N = 7
	D, x = cheb(N)
	
	A = np.dot(D, D)[1:N:1,1:N:1]
	u = np.zeros(x.shape)
	u[1:N] = solve(A + D[1:N:1,1:N:1], f(x[1:N:1]))
	
	xx = np.linspace(-1, 1, 50)
	uu = (np.poly1d(np.polyfit(x,u,N)))(xx)
	print "Max error is ", np.max(np.abs(uu + G(xx) - anal_sol(xx)))
	
	# plt.plot(x,u+G(x), '*k')
	# plt.plot(xx,uu+G(xx), '-r')
	plt.plot(xx, anal_sol(xx))
	plt.xlabel('$x$')
	plt.ylabel('$u$')
	# plt.axis([-1.,1.,-2.5,.5])
	plt.savefig('nonzeroDirichlet.pdf')
        plt.clf()
def exercise3():
	# Solves the Poisson boundary value problem 
	# u''(x) + u'(x) = f(x), 
	# u(-1) = a
	# u(1) = b
	a, b = 2, -1
	def f(x): return -(b-a)/2.*np.ones(x.shape)+ np.exp(3.*x)
	
	def G(x): return (b-a)/2.*np.ones(x.shape) *x + (b+a)/2.*np.ones(x.shape)
	
	def anal_sol(x): 
		out = ( np.exp(-x)/( 12.*(-1 +np.exp(1))*(1 + np.exp(1))*np.exp(3)   )  * 
		( -12.*a*np.exp(x+3.)+12.*a*np.exp(4) + 12.*b*np.exp(x+5)-12*b*np.exp(4) - 
		  np.exp(4*x+3) + np.exp(4*x+5) - np.exp(x+8 )+ np.exp(x)- np.exp(1) + np.exp(7) 
		  ) 
			  )
		
		return out
	
	
	N = 8
	D,x = cheb(N)
	
	A = np.dot(D,D)[1:N:1,1:N:1]
	u = np.zeros(x.shape); u[1:N] = solve(A + D[1:N:1,1:N:1],f(x[1:N:1]))
	# print "\nDx = \n", x[1:N:1]
	# print "\nA = \n", A
	
	xx = np.linspace(-1,1,50)
	uu = (np.poly1d(np.polyfit(x,u,N)))(xx)
	
	plt.plot(x,u+G(x),'*k')
	plt.plot(xx,uu+G(xx),'-r')
	plt.plot(xx,anal_sol(xx),'-k')
	# plt.axis([-1.,1.,-2.5,.5]); 
	plt.show()
	print "Max error is ", np.max(np.abs(uu+G(xx) - anal_sol(xx)))
	return
Пример #10
0
def Solitons():
	# nu, L = .1, 2.
	# u_m,u_p = 5.,1.
	# s, a = (u_m + u_p)/2., (u_m - u_p)/2.
	L = 120.
	s = .5

	def guess(x):
		return (3.*s/2)*np.cosh( (math.sqrt(s/2.)/2.)*(x+3.5) )**(-2.)


	# x = np.linspace(-1,1,400)
	# plt.plot(x,guess(x),'-k')
	# plt.show()
	# import sys; sys.exit()

	# N = 50
	# D,x = cheb(N)
	# M = D.dot(D)
	# M[0,:], M[-1,:] = 0., 0.
	# M[0,0], M[-1,-1] = 1., 1.
	# D2 = D[1:-1,:]

	N = 50
	D1, x = cheb(N)
	D2 = D1.dot(D1)
	D3 = D2.dot(D1)
	D2[0,:], D2[-1,:] = 0., 0.
	D2[0,0], D2[-1,-1] = 1., 1.
	D1mod = D1[1:-1,:]

	u_bcs = np.zeros(N+1)
	y_bcs = np.zeros(N+1)
	u_bcs[0], u_bcs[-1] = 0., (3./3)*s
	y_bcs[0], y_bcs[-1] = 0., (3./3)*s
	def cap_F(u):
		out = np.zeros(2*(N+1))
		out1, out2 = out[:N+1], out[N+1:]
		u1, u2 = u[:N+1], u[N+1:]

		out1 += D2.dot(u1) - u_bcs
		out1[1:-1] -= (s - u1[1:-1]/2.)*u1[1:-1]
		out2 += D2.dot(u2) - y_bcs
		out2[1:-1] -= (s - u2[1:-1]/2.)*u2[1:-1]
		return out

	array = np.empty(2*(N+1))
	array[:N+1] = guess(.4*L/2.*(x+1.))
	array[N+1:] = guess(-(.3)*L/2.*(x+1.))
	print np.min(array[N+1:]), np.min(array[:N+1])
	sol = root(cap_F, array )
	if sol.success:
		# print sol
		plt.plot(x,sol.x[:N+1],'-*k',linewidth=2.)
		plt.plot(x,sol.x[N+1:],'-*k',linewidth=2.)
		# plt.plot(x,array[:N+1],'-*r')
		# plt.plot(x,array[N+1:],'-*r')
		# plt.axis([-1,1,0.-.01, 2.*s])
		plt.show()
		x_orig = L/2.*(x+1.)
		plt.plot(x_orig,sol.x[:N+1],'-ok',markersize=3.,linewidth=2.,label='Numerical solution')
		plt.plot(-x_orig,sol.x[N+1:],'-ok',markersize=3.,linewidth=2.)
		plt.plot(x_orig,array[:N+1],'-or',markersize=3.,linewidth=2.,label='Guess')
		plt.plot(-x_orig,array[N+1:],'-or',markersize=3.,linewidth=2.)
		# plt.axis([-L,L,0.-.01, 2.*s])
		plt.legend(loc='best')
		plt.show()
	return
Пример #11
0
def Solitons():
    # nu, L = .1, 2.
    # u_m,u_p = 5.,1.
    # s, a = (u_m + u_p)/2., (u_m - u_p)/2.
    L = 120.
    s = .5

    def guess(x):
        return (3. * s / 2) * np.cosh(
            (math.sqrt(s / 2.) / 2.) * (x + 3.5))**(-2.)

    # x = np.linspace(-1,1,400)
    # plt.plot(x,guess(x),'-k')
    # plt.show()
    # import sys; sys.exit()

    # N = 50
    # D,x = cheb(N)
    # M = D.dot(D)
    # M[0,:], M[-1,:] = 0., 0.
    # M[0,0], M[-1,-1] = 1., 1.
    # D2 = D[1:-1,:]

    N = 50
    D1, x = cheb(N)
    D2 = D1.dot(D1)
    D3 = D2.dot(D1)
    D2[0, :], D2[-1, :] = 0., 0.
    D2[0, 0], D2[-1, -1] = 1., 1.
    D1mod = D1[1:-1, :]

    u_bcs = np.zeros(N + 1)
    y_bcs = np.zeros(N + 1)
    u_bcs[0], u_bcs[-1] = 0., (3. / 3) * s
    y_bcs[0], y_bcs[-1] = 0., (3. / 3) * s

    def cap_F(u):
        out = np.zeros(2 * (N + 1))
        out1, out2 = out[:N + 1], out[N + 1:]
        u1, u2 = u[:N + 1], u[N + 1:]

        out1 += D2.dot(u1) - u_bcs
        out1[1:-1] -= (s - u1[1:-1] / 2.) * u1[1:-1]
        out2 += D2.dot(u2) - y_bcs
        out2[1:-1] -= (s - u2[1:-1] / 2.) * u2[1:-1]
        return out

    array = np.empty(2 * (N + 1))
    array[:N + 1] = guess(.4 * L / 2. * (x + 1.))
    array[N + 1:] = guess(-(.3) * L / 2. * (x + 1.))
    print np.min(array[N + 1:]), np.min(array[:N + 1])
    sol = root(cap_F, array)
    if sol.success:
        # print sol
        plt.plot(x, sol.x[:N + 1], '-*k', linewidth=2.)
        plt.plot(x, sol.x[N + 1:], '-*k', linewidth=2.)
        # plt.plot(x,array[:N+1],'-*r')
        # plt.plot(x,array[N+1:],'-*r')
        # plt.axis([-1,1,0.-.01, 2.*s])
        plt.show()
        x_orig = L / 2. * (x + 1.)
        plt.plot(x_orig,
                 sol.x[:N + 1],
                 '-ok',
                 markersize=3.,
                 linewidth=2.,
                 label='Numerical solution')
        plt.plot(-x_orig, sol.x[N + 1:], '-ok', markersize=3., linewidth=2.)
        plt.plot(x_orig,
                 array[:N + 1],
                 '-or',
                 markersize=3.,
                 linewidth=2.,
                 label='Guess')
        plt.plot(-x_orig, array[N + 1:], '-or', markersize=3., linewidth=2.)
        # plt.axis([-L,L,0.-.01, 2.*s])
        plt.legend(loc='best')
        plt.show()
    return