def prob3_again(): """ Using scikits.bvp_solver to solve the bvp """ epsilon = .1 lbc, rbc = 0., 1. def function1(x, y): return np.array([ y[1], (4. / epsilon) * (pi - x**2.) * y[0] + 1. / epsilon * cos(x) ]) def boundary_conditions(ya, yb): return (np.array([ya[0] - lbc]), np.array([yb[0] - rbc])) problem = bvp_solver.ProblemDefinition( num_ODE=2, num_parameters=0, num_left_boundary_conditions=1, boundary_points=(0., pi / 2.), function=function1, boundary_conditions=boundary_conditions) solution = bvp_solver.solve(problem, solution_guess=(1., 0.)) A = np.linspace(0., pi / 2., 200) T = solution(A) plt.plot(A, T[0, :], '-k', linewidth=2.0) plt.show() plt.clf() return
def prob5_again(): """ Using scikits.bvp_solver to solve the bvp """ epsilon = .05 lbc, rbc = 1. / (1. + epsilon), 1. / (1. + epsilon) def function1(x, y): return np.array([ y[1], -4. * x / (epsilon + x**2.) * y[1] - 2. / (epsilon + x**2.) * y[0] ]) def boundary_conditions(ya, yb): return (np.array([ya[0] - lbc]), np.array([yb[0] - rbc])) problem = bvp_solver.ProblemDefinition( num_ODE=2, num_parameters=0, num_left_boundary_conditions=1, boundary_points=(-1, 1), function=function1, boundary_conditions=boundary_conditions) solution = bvp_solver.solve(problem, solution_guess=(1. / (1. + epsilon), 0.)) A = np.linspace(-1., 1., 200) T = solution(A) plt.plot(A, T[0, :], '-k', linewidth=2.0) plt.show() plt.clf() return
def Exercise4(): # measles a, b = 0., 1. # Interval of the BVP n, N = 3, 80 # Dimension of the system/ Number of subintervals # Tolerance/ Maximum number of Newton steps TOL, Max_IT = 10.**(-12), 40 init_mesh = np.linspace(a, b, N + 1) # Initial Mesh lmbda, mu, eta = .0279, .02, .01 def beta1(x): return 1575. * (1. + np.cos(2. * np.pi * x)) def Guess(x): S = .1 + .05 * np.cos(2. * np.pi * x) return np.array([S, 05 * (1. - S), 05 * (1. - S), .05, .05, .05]) def ODE(x, y): return np.array([ mu - beta1(x) * y[0] * y[2], beta1(x) * y[0] * y[2] - y[1] / lmbda, y[1] / lmbda - y[2] / eta, 0, 0, 0 ]) def g(Ya, Yb): BCa = Ya[0:3] - Ya[3:] BCb = Yb[0:3] - Yb[3:] return BCa, BCb problem = bvp_solver.ProblemDefinition(num_ODE=6, num_parameters=0, num_left_boundary_conditions=3, boundary_points=(a, b), function=ODE, boundary_conditions=g) solution = bvp_solver.solve(problem, solution_guess=Guess, trace=0, max_subintervals=1000, tolerance=1e-9) Num_Sol = solution(np.linspace(a, b, N + 1)) # Guess_array = np.zeros((6,N+1)) # for index, x in zip(range(N+1),np.linspace(a,b,N+1)): # Guess_array[:,index] = Guess(x) # plt.plot(np.linspace(a,b,N+1), Guess_array[0,:] ,'-g') plt.plot(np.linspace(a, b, N + 1), Num_Sol[0, :], '-k', label='Susceptible') plt.plot(np.linspace(a, b, N + 1), Num_Sol[1, :], '-g', label='Exposed') plt.plot(np.linspace(a, b, N + 1), Num_Sol[2, :], '-r', label='Infectious') plt.legend(loc=5) # middle right placement plt.axis([0., 1., -.01, .1]) plt.xlabel('T (days)') plt.ylabel('Proportion of Population') # plt.savefig("measles.pdf") plt.clf()
def bvp2_check(): """ Using scikits.bvp_solver to solve the bvp y'' + y' + sin y = 0, y(0) = y(2*pi) = 0 y0 = y, y1 = y' y0' = y1, y1' = y'' = -sin(y0) - y1 """ from math import exp, pi, sin lbc, rbc = .1, .1 def function1(x, y): return np.array([y[1], -sin(y[0]) - y[1]]) def boundary_conditions(ya, yb): return ( np.array( [ya[0] - lbc] ), #evaluate the difference between the temperature of the hot stream on the #left and the required boundary condition np.array([yb[0] - rbc]) ) #evaluate the difference between the temperature of the cold stream on the #right and the required boundary condition problem = bvp_solver.ProblemDefinition( num_ODE=2, num_parameters=0, num_left_boundary_conditions=1, boundary_points=(0, 2. * pi), function=function1, boundary_conditions=boundary_conditions) guess = np.linspace(0., 2. * pi, 10) guess = np.array([.1 - np.sin(2 * guess), np.sin(2 * guess)]) # plt.plot(guess,np.sin(guess)) # plt.show() solution = bvp_solver.solve(problem, solution_guess=guess) # A = np.linspace(0., 2. * pi, 200) T = solution(A) plt.plot(A, T[0, :], '-k', linewidth=2.0) plt.show() plt.clf() N = 150 x = (2. * np.pi / N) * np.arange(1, N + 1).reshape(N, 1) print x.shape print solution(x)[0, :].shape plt.plot(x, solution(x)[0, :]) plt.show() # np.save('sol',solution(x)[0,:]) return
def reentry(x_guess, guess, N=240, plot=False): R = 209 beta = 4.26 rho0 = 2.704e-3 g = 3.2172e-4 s = 26600 T_init = x_guess[-1] def C_d(u): return 1.174 - 0.9 * cos(u) def C_l(u): return 0.6 * sin(u) ############################################################# def ode(x, y): u = arctan((6 * y[4]) / (9 * y[0] * y[3])) rho = rho0 * exp(-beta * R * y[2]) out = y[6] * array([ -s * rho * y[0]**2 * C_d(u) - g * sin(y[1]) / (1 + y[2])**2, # G_0 (s * rho * y[0] * C_l(u) + y[0] * cos(y[1]) / (R * (1 + y[2])) - g * cos(y[1]) / (y[0] * (1 + y[2])**2)), # G_1 y[0] * sin(y[1]) / R, # G_2 -(30 * y[0]**2. * sqrt(rho) + y[3] * (-2 * s * rho * y[0] * C_d(u)) + y[4] * (s * rho * C_l(u) + cos(y[1]) / (R * (1 + y[2])) + g * cos(y[1]) / (y[0]**2 * (1 + y[2])**2)) + y[5] * (sin(y[1]) / R)), # G_3 -(y[3] * (-g * cos(y[1]) / (1 + y[2])**2) + y[4] * (-y[0] * sin(y[1]) / (R * (1 + y[2])) + g * sin(y[1]) / (y[0] * (1 + y[2])**2)) + y[5] * (y[0] * cos(y[1]) / R)), # G_4 -(5 * y[0]**3. * sqrt(rho) * (-beta * R) + y[3] * (s * beta * R * rho * y[0]**2 * C_d(u) + 2 * g * sin(y[1]) / (1 + y[2])**3) + y[4] * (-s * beta * R * rho * y[0] * C_l(u) - y[0] * cos(y[1]) / (R * (1 + y[2])**2) + 2 * g * cos(y[1]) / (y[0] * (1 + y[2])**3))), # G_5 0 # T' = 0 # G_6 ]) return out ############################################################# def bcs(ya, yb): out1 = array([ya[0] - .36, ya[1] + 8.1 * pi / 180, ya[2] - 4 / R]) out2 = array([yb[0] - .27, yb[1], yb[2] - 2.5 / R, H(yb)]) return out1, out2 ############################################################# def H(y): u = arctan((6 * y[4]) / (9 * y[0] * y[3])) rho = rho0 * exp(-beta * R * y[2]) out = (10 * y[0]**3 * sqrt(rho) + y[3] * (-s * rho * y[0]**2 * C_d(u) - g * sin(y[1]) / (1 + y[2])**2) + y[4] * (s * rho * y[0] * C_l(u) + y[0] * cos(y[1]) / (R * (1 + y[2])) - g * cos(y[1]) / (y[0] * (1 + y[2])**2)) + y[5] * y[0] * sin(y[1]) / R) return out ############################################################# yint = np.concatenate((guess, T_init * np.ones((1, len(x_guess)))), axis=0) yint[3, :] = -1 p1, p2, p3 = yint[3, 0], yint[4, 0], yint[5, 0] u = p1 * erf(p2 * (p3 - x_guess / T_init)) yint[4, :] = 1.5 * yint[0, :] * yint[3, :] * np.tan(u) for j in range(len(yint[5, :])): y = yint[:6, j] def new_func(x): if y[1] < 0 and y[1] > -.05: y[1] = -.05 if y[1] > 0 and y[1] < .05: y[1] = .05 y[5] = x return H(y) sol = root(new_func, -8) if j > 0: if sol.success == True: yint[5, j] = sol.x else: yint[5, j] = yint[5, j - 1] else: if sol.success == True: yint[5, 0] = sol.x plt.plot(x_guess, yint[5, :]) plt.show() plt.clf() problem = bvp_solver.ProblemDefinition(num_ODE=7, num_parameters=0, num_left_boundary_conditions=3, boundary_points=(0., 1), function=ode, boundary_conditions=bcs) solution = bvp_solver.solve(problem, solution_guess=yint, initial_mesh=linspace(0, 1, len(x_guess)), max_subintervals=1000, trace=1) # For more info on the available options for bvp_solver, look at # the docstrings for bvp_solver.ProblemDefinition and bvp_solver.solve numerical_soln = solution(linspace(0, 1, N + 1)) u = arctan((6 * numerical_soln[4, :]) / (9 * numerical_soln[0, :] * numerical_soln[3, :])) domain = linspace(0, numerical_soln[6, 0], N + 1) plt.plot(domain, np.real(numerical_soln[5, :]), '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel(r'$\lambda_2$', fontsize=18) # plt.savefig('solution_xi.pdf') plt.show() plt.clf() if plot == True: # Plot guess for v plt.plot(domain, np.real(guess[0, :]), '-r', linewidth=2.0) plt.plot(domain, np.real(numerical_soln[0, :]), '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel('$v$', fontsize=18) # plt.savefig('solution_v.pdf') plt.show() plt.clf() # Plot guess for gamma plt.plot(domain, np.real(guess[1, :]), '-r', linewidth=2.0) plt.plot(domain, np.real(numerical_soln[1, :]), '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel(r'$\gamma$', fontsize=18) # plt.savefig('solution_gamma.pdf') plt.show() plt.clf() # Plot guess for xi plt.plot(domain, np.real(guess[2, :]), '-r', linewidth=2.0) plt.plot(domain, np.real(numerical_soln[2, :]), '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel(r'$\xi$', fontsize=18) # plt.savefig('solution_xi.pdf') plt.show() plt.clf() # Plot guess for control u p1, p2, p3 = guess[3, 0], guess[4, 0], guess[5, 0] plt.plot(domain, p1 * erf(p2 * (p3 - x_guess / T_init)), '-r', linewidth=2.0) plt.plot(domain, u, '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel(r'$u$', fontsize=18) # plt.savefig('solution_u.pdf') plt.show() plt.clf() return (domain, numerical_soln[0, :], numerical_soln[1, :], numerical_soln[2, :], numerical_soln[3, :], numerical_soln[4, :], numerical_soln[5, :], u)
def reentry_auxiliary_problem(N=240, plot=True): R = 209 beta = 4.26 rho0 = 2.704e-3 g = 3.2172e-4 s = 26600 T_init = 230 def C_d(u): return 1.174 - 0.9 * cos(u) def C_l(u): return 0.6 * sin(u) # Construct initial guess for the auxiliary BVP # p1, p2, p3 = 1.09835, 6.48578, .347717 # Exact solutions p1, p2, p3 = 1.3, 4.5, .5 ############################################################# def guess_auxiliary(x): out = array([ .5 * (.36 + .27) - .5 * (.36 - .27) * tanh(.025 * (x - .45 * T_init)), pi / 180 * (.5 * (-8.1 + 0) - .5 * (-8.1 - 0) * tanh(.025 * (x - .25 * T_init))), (1 / R) * (.5 * (4 + 2.5) - .5 * (4 - 2.5) * tanh(.03 * (x - .3 * T_init)) - 1.4 * cosh(.025 * (x - .25 * T_init))**(-2)), p1 * ones(x.shape), p2 * ones(x.shape), p3 * ones(x.shape) ]) return out ############################################################# def ode_auxiliary(x, y): u = y[3] * erf(y[4] * (y[5] - x / T_init)) rho = rho0 * exp(-beta * R * y[2]) out = array([ -s * rho * y[0]**2 * C_d(u) - g * sin(y[1]) / (1 + y[2])**2, (s * rho * y[0] * C_l(u) + y[0] * cos(y[1]) / (R * (1 + y[2])) - g * cos(y[1]) / (y[0] * (1 + y[2])**2)), y[0] * sin(y[1]) / R, 0, 0, 0 ]) return out ############################################################# def bcs_auxiliary(ya, yb): out1 = array([ya[0] - .36, ya[1] + 8.1 * pi / 180, ya[2] - 4 / R]) out2 = array([yb[0] - .27, yb[1], yb[2] - 2.5 / R]) return out1, out2 ############################################################# problem_auxiliary = bvp_solver.ProblemDefinition( num_ODE=6, num_parameters=0, num_left_boundary_conditions=3, boundary_points=(0., T_init), function=ode_auxiliary, boundary_conditions=bcs_auxiliary) solution_auxiliary = bvp_solver.solve(problem_auxiliary, solution_guess=guess_auxiliary) # N = 240 # Number of subintervals xint_guess = linspace(0, T_init, N + 1) yint_guess = solution_auxiliary(xint_guess) if plot == True: # Plot guess for v plt.plot(xint_guess, np.real(yint_guess[0, :]), '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel('$v$', fontsize=18) # plt.savefig('guess_v.pdf') plt.show() plt.clf() # Plot guess for gamma plt.plot(xint_guess, np.real(yint_guess[1, :]), '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel(r'$\gamma$', fontsize=18) # plt.savefig('guess_gamma.pdf'); plt.show() plt.clf() # Plot guess for xi plt.plot(xint_guess, np.real(yint_guess[2, :]), '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel(r'$\xi$', fontsize=18) # plt.savefig('guess_xi.pdf') plt.show() plt.clf() # Plot guess for control u p1, p2, p3 = yint_guess[3, 0], yint_guess[4, 0], yint_guess[5, 0] plt.plot(xint_guess, p1 * erf(p2 * (p3 - xint_guess / T_init)), '-k', linewidth=2.0) plt.xlabel('$t$', fontsize=18) plt.ylabel(r'$u$', fontsize=18) # plt.savefig('guess_u.pdf') plt.show() plt.clf() return xint_guess, yint_guess
def rhs_function(x , y): return np.array([y[1] , 1-y[0]]) def boundary_conditions(ya,yb): return (np.array([ya[0] - 1]), np.array([yb[0]])) # Implementing the ProblemDefinition technique problem = bvp_solver.ProblemDefinition(num_ODE = 2, num_parameters = 0, num_left_boundary_conditions = 1, boundary_points = (0, np.pi), function = rhs_function, boundary_conditions = boundary_conditions) # Solving the Bvp solution = bvp_solver.solve(problem, solution_guess = (2.0, 2.0)) x = np.linspace(0, np.pi/2, 45) y = solution(x) print "Solution to the Boundary Value Problem", y pylab.plot(x, y[0,:],'r-', ms = 2) pylab.plot(x, y[1,:],'g.-', ms = 2)
dY[1] = P*X*Y[0] return dY def boundary_conditions(Ya, Yb, P): BCa = np.zeros((2)) BCb = np.zeros((1)) BCa[0] = Ya[0] # u(-1)= 0 BCa[1] = Ya[1]-1.0 #u'(-1) = 1 BCb[0] = Yb[0] # u(1) = 0 return BCa, BCb problem_definition = bvp.ProblemDefinition(num_ODE = 2, num_parameters = 1, num_left_boundary_conditions = 2, boundary_points = (-1.0, 1.0), function = function, boundary_conditions = boundary_conditions) sol = bvp.solve(bvp_problem = problem_definition, solution_guess = [1.0 ,1.0 ], parameter_guess = [168.0 ], trace=1) print sol.parameters fig = plt.figure() x = sol.mesh y = sol.solution plt.plot(x, y[0,:], 'k-', lw=2)
def bcs_auxiliary(ya, yb): out1 = array([ya[0] - .36, ya[1] + 8.1 * pi / 180, ya[2] - 4 / R]) out2 = array([yb[0] - .27, yb[1], yb[2] - 2.5 / R]) return out1, out2 #--------------------------------------------------------------# ################################################################ ################################################################ #--------------------------------------------------------------# problem_auxiliary = bvp_solver.ProblemDefinition( num_ODE=6, num_parameters=0, num_left_boundary_conditions=3, boundary_points=(0, T0), function=ode_auxiliary, boundary_conditions=bcs_auxiliary) solution_auxiliary = bvp_solver.solve(problem_auxiliary, solution_guess=guess_auxiliary, trace=0, max_subintervals=20000) N = 240 x_guess = linspace(0, T0, N + 1) guess = solution_auxiliary(x_guess) #--------------------------------------------------------------# ################################################################