PHYS 305: Computational Physics II
Winter 2022


Shooting method 1 -- basic boundary value problem

Previous   Next

Equation and BCs:

Y'' = deriv2(x, Y, Y')
Y(a) = ya,  Y(b) = yb

Strategy:

Guess Y'(a), call it z
Bisect on z to satisfy the BC at b

Code:

def deriv2(x, y):
    return -y[0]

def f(x, y):					# RHS of the system
    return np.array([y[1], deriv2(x, y)])

def integrate(func, a, ya, ypa, b, dx):		# integrate a to b and return results
    x = a
    y = np.array([ya, ypa])			# y = [Y, Y']
    while x < b-0.5*dx:
        x, y = rk4_step(func, x, y, dx)
    return x, y
	      
def g(z):					# integrate and return error
    x, y = integrate(f, a, ya, z, b, dx)
    return y[0] - yb				# want y[0] = yb