Exemplo n.º 1
0
def extra():
    l_bc, r_bc = 0., 1.
    lmbda = 12
    N = 50
    D, x = cheb_vectorized(N)
    M = np.dot(D, D)
    guess = (.5 * (x + 1))  #**lmbda
    N2 = 500

    def pseudospectral_ode(y):
        out = np.zeros(y.shape)
        ypp = M.dot(y)
        out = 4 * ypp - lmbda * np.sinh(lmbda * y)
        out[0], out[-1] = y[0] - r_bc, y[-1] - l_bc
        return out

    u = root(pseudospectral_ode, guess, method='lm', tol=1e-9)
    print u.success
    num_sol = BarycentricInterpolator(x, u.x)

    xx = np.linspace(-1, 1, N2)
    uu = num_sol.__call__(xx)
    plt.plot(x, guess, '*b')
    plt.plot(xx, uu, '-r')  # Numerical solution via
    # the pseudospectral method
    plt.axis([-1., 1., 0 - .1, 1.1])
    plt.show()
    plt.clf()
Exemplo n.º 2
0
def extra():
	l_bc, r_bc = 0., 1.
	lmbda = 12
	N = 50
	D, x = cheb_vectorized(N)
	M = np.dot(D, D)
	guess = (.5*(x+1))#**lmbda
	N2 = 500

	def pseudospectral_ode(y):
		out = np.zeros(y.shape)
		ypp = M.dot(y)
		out = 4*ypp - lmbda*np.sinh(lmbda*y)
		out[0], out[-1] = y[0] - r_bc, y[-1] - l_bc
		return out

	u = root(pseudospectral_ode,guess,method='lm',tol=1e-9)
	print u.success
	num_sol = BarycentricInterpolator(x,u.x)

	xx = np.linspace(-1, 1, N2)
	uu = num_sol.__call__(xx)
	plt.plot(x,guess,'*b')
	plt.plot(xx, uu, '-r')						# Numerical solution via
												# the pseudospectral method
	plt.axis([-1.,1.,0-.1,1.1])
	plt.show()
	plt.clf()
Exemplo n.º 3
0
def trajectory():
	N = 200  
	D, x = cheb(N)  
	eps = 0.  
	
	def L_yp(x,yp_):
		a_ = a(x)
		return a_**3.*yp_*(1 + a_**2.*yp_**2.)**(-1./2) - a_**2.*r(x)
	
	def g(z):
		out = D.dot(L_yp( x,D.dot(z) ))
		out[0], out[-1] = z[0] - z1, z[-1] - 0
		return out
	
	
	# Use the straight line trajectory as an initial guess.
	# Another option would be to use the shortest-time trajectory found
	# in heuristic_tractory() as an initial guess.
	eps, time = time_functional()
	sol = root(g,y(x,eps))
	# sol =  root(g,z1/2.*(x-(-1.))) 
	# print sol.success
	z = sol.x
	poly = BarycentricInterpolator(x,D.dot(z))
	num_func = lambda inpt: poly.__call__(inpt)
	
	
	def L(x):
		a_, yp_ = a(x), num_func(x)
		return a_*(1 + a_**2.*yp_**2.)**(1./2) - a_**2.*r(x)*yp_
	
	print "The shortest time is approximately "
	print integrate.quad(L,-1,1,epsabs=1e-10,epsrel=1e-10)[0]
	
	# x0 = np.linspace(-1,1,200)
	# y0 = y(x0,eps)
	# plt.plot(x0,y0,'-g',linewidth=2.,label='Initial guess')
	# plt.plot(x,z,'-b',linewidth=2.,label="Numerical solution")	
	# ax = np.linspace(0-.05,z1+.05,200)
	# plt.plot(-np.ones(ax.shape),ax,'-k',linewidth=2.5)
	# plt.plot(np.ones(ax.shape),ax,'-k',linewidth=2.5)
	# plt.plot(-1,0,'*b',markersize=10.)
	# plt.plot(1,z1,'*b',markersize=10.)
	# plt.xlabel('$x$',fontsize=18)
	# plt.ylabel('$y$',fontsize=18)
	# plt.legend(loc='best')
	# plt.axis([-1.1,1.1,0. -.05,z1 +.05])
	# # plt.savefig("minimum_time_rivercrossing.pdf")
	# plt.show()
	# plt.clf()
	return x, z, N
Exemplo n.º 4
0
plt.xlabel("$x$")
plt.savefig('pseudospectral_blog1_fig0.jpg')
plt.show()

N = 5
x = np.cos((np.pi / (N - 1)) * np.linspace(0, N - 1, N))

plt.plot(X, f(X), '-k')
plt.plot(x, f(x), '*k')
plt.savefig('pseudospectral_blog1_fig1.jpg')
plt.show()

N = 5
x1 = np.cos((np.pi / N) * np.linspace(0, N, N + 1))
interp = BarycentricInterpolator(x1, f(x1))
u1 = interp.__call__(X)

N = 10
x2 = np.cos((np.pi / N) * np.linspace(0, N, N + 1))
interp = BarycentricInterpolator(x2, f(x2))
u2 = interp.__call__(X)

N = 15
x3 = np.cos((np.pi / N) * np.linspace(0, N, N + 1))
interp = BarycentricInterpolator(x3, f(x3))
u3 = interp.__call__(X)

plt.plot(X, f(X), '-k', label="$f$")
plt.plot(x1, f(x1), '*g')
plt.plot(X, u1, '-g', label="$p_5$")
Exemplo n.º 5
0
def nonlinear_minimal_area_surface_of_revolution():
    l_bc, r_bc = 1., 7.
    N = 80
    D, x = cheb_vectorized(N)
    M = np.dot(D, D)
    guess = 1. + (x - -1.) * ((r_bc - l_bc) / 2.)
    N2 = 50

    def pseudospectral_ode(y):
        out = np.zeros(y.shape)
        yp, ypp = D.dot(y), M.dot(y)
        out = y * ypp - 1. - yp**2.
        out[0], out[-1] = y[0] - r_bc, y[-1] - l_bc
        return out

    u = root(pseudospectral_ode, guess, method='lm', tol=1e-9)
    num_sol = BarycentricInterpolator(x, u.x)

    # Up to this point we have found the numerical solution
    # using the pseudospectral method. In the code that follows
    # we check that solution with the analytic solution,
    # and graph the results

    def f(x):
        return np.array([
            x[1] * np.cosh((-1. + x[0]) / x[1]) - l_bc, x[1] * np.cosh(
                (1. + x[0]) / x[1]) - r_bc
        ])

    parameters = root(f, np.array([1., 1.]), method='lm', tol=1e-9)
    A, B = parameters.x[0], parameters.x[1]

    def analytic_solution(x):
        out = B * np.cosh((x + A) / B)
        return out

    xx = np.linspace(-1, 1, N2)
    uu = num_sol.__call__(xx)
    # print "Max error is ", np.max(np.abs(uu - analytic_solution(xx)))
    plt.plot(x, guess, '-b')
    plt.plot(xx, uu, '-r')  # Numerical solution via
    # the pseudospectral method
    plt.plot(xx, analytic_solution(xx), '*k')  # Analytic solution
    plt.axis([-1., 1., l_bc - 1., r_bc + 1.])
    # plt.show()
    plt.clf()

    theta = np.linspace(0, 2 * np.pi, N2)
    X, Theta = np.meshgrid(xx, theta, indexing='ij')
    print "\nxx = \n", xx
    print "\nuu = \n", uu
    F = uu[:, np.newaxis] + np.zeros(uu.shape)
    print "\nX = \n", X
    print "\nTheta = \n", Theta
    print "\nF = \n", F
    Y = F * np.cos(Theta)
    Z = F * np.sin(Theta)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    # X, Y, Z = axes3d.get_test_data(0.05)
    ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
    print ax.azim, ax.elev
    ax.azim = -65
    ax.elev = 0
    # ax.view_init(elev=-60, azim=30)
    # plt.savefig('minimal_surface.pdf')
    plt.show()
Exemplo n.º 6
0
npts = 7
noiseAmp = 0.03
xdata = np.linspace(0.2, 10., npts) + 0.3*np.random.randn(npts)
xdata = np.sort(xdata)
ydata = f(xdata) * (1.0 + noiseAmp * np.random.randn(npts))
np.savetxt('logCurveData9.txt', zip(xdata, ydata), fmt='%10.2f')

# Create x array spanning data set plus 5%
xmin, xmax = frangeAdd(xdata, 0.05)
x = np.linspace(xmin, xmax, 200)

# Plot data and "fit"
plt.plot(xdata, ydata, 'or', label='data')
plt.plot(x, f(x), 'k-', label='fitting function')

# Create y array from cubic spline
f_cubic = interp1d(xdata, ydata, kind='cubic', bounds_error=False)
plt.plot(x, f_cubic(x), 'k-', label='cubic spline')

# Create y array from univariate spline
f_univar = UnivariateSpline(xdata, ydata, w=None, bbox=[xmin, xmax], k=3)
plt.plot(x, f_univar(x), label='univariate spline')

# Create y array from barycentric interpolation
f_bary = BarycentricInterpolator(xdata, ydata)
plt.plot(x, f_bary.__call__(x), label='barycentric interp')

plt.legend(loc='best')

plt.show()
Exemplo n.º 7
0
def nonlinear_minimal_area_surface_of_revolution():
	l_bc, r_bc = 1., 7.
	N = 80
	D, x = cheb_vectorized(N)
	M = np.dot(D, D)
	guess = 1. + (x--1.)*((r_bc - l_bc)/2.)
	N2 = 50

	def pseudospectral_ode(y):
		out = np.zeros(y.shape)
		yp, ypp = D.dot(y), M.dot(y)
		out = y*ypp - 1. - yp**2.
		out[0], out[-1] = y[0] - r_bc, y[-1] - l_bc
		return out

	u = root(pseudospectral_ode,guess,method='lm',tol=1e-9)
	num_sol = BarycentricInterpolator(x,u.x)

	# Up to this point we have found the numerical solution
	# using the pseudospectral method. In the code that follows
	# we check that solution with the analytic solution,
	# and graph the results

	def f(x):
		return np.array([ x[1]*np.cosh((-1.+x[0])/x[1])-l_bc,
						 x[1]*np.cosh((1.+x[0])/x[1])-r_bc])


	parameters = root(f,np.array([1.,1.]),method='lm',tol=1e-9)
	A, B = parameters.x[0], parameters.x[1]
	def analytic_solution(x):
		out = B*np.cosh((x + A)/B)
		return out


	xx = np.linspace(-1, 1, N2)
	uu = num_sol.__call__(xx)
	# print "Max error is ", np.max(np.abs(uu - analytic_solution(xx)))
	plt.plot(x,guess,'-b')
	plt.plot(xx, uu, '-r')						# Numerical solution via
												# the pseudospectral method
	plt.plot(xx, analytic_solution(xx), '*k')   # Analytic solution
	plt.axis([-1.,1.,l_bc-1.,r_bc +1.])
	# plt.show()
	plt.clf()

	theta = np.linspace(0,2*np.pi,N2)
	X,Theta = np.meshgrid(xx,theta,indexing='ij')
	print "\nxx = \n", xx
	print "\nuu = \n", uu
	F = uu[:,np.newaxis] +np.zeros(uu.shape)
	print "\nX = \n", X
	print "\nTheta = \n", Theta
	print "\nF = \n", F
	Y = F*np.cos(Theta)
	Z = F*np.sin(Theta)

	fig = plt.figure()
	ax = fig.add_subplot(111, projection='3d')
	# X, Y, Z = axes3d.get_test_data(0.05)
	ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
	print ax.azim, ax.elev
	ax.azim=-65; ax.elev = 0
	# ax.view_init(elev=-60, azim=30)
	# plt.savefig('minimal_surface.pdf')
	plt.show()