0% found this document useful (0 votes)
15 views1 page

Program 1

The document contains Python code that defines a system of ordinary differential equations (ODEs) and uses the shooting method to find initial conditions for two variables, lambda1 and lambda2, at t=0. It integrates the ODEs from t=0 to t=1 and checks the terminal conditions at t=1 to ensure they equal 20. The code attempts to find these initial values using a root-finding algorithm and reports success or failure of the process.

Uploaded by

SAGNIK PAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

Program 1

The document contains Python code that defines a system of ordinary differential equations (ODEs) and uses the shooting method to find initial conditions for two variables, lambda1 and lambda2, at t=0. It integrates the ODEs from t=0 to t=1 and checks the terminal conditions at t=1 to ensure they equal 20. The code attempts to find these initial values using a root-finding algorithm and reports success or failure of the process.

Uploaded by

SAGNIK PAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import numpy as np

from scipy.integrate import solve_ivp


from scipy.optimize import root

# System of ODEs
def odes(t, y):
z1, z2, l1, l2 = y
dz1dt = -2 * t * z1 - 0.5 * l1
dz2dt = 2 * t * z1 - z2
dl1dt = -2 * z1 + (2 * t + 1) * l1 - 2 * t * l2
dl2dt = -2 * z2 + l2
return [dz1dt, dz2dt, dl1dt, dl2dt]

# Residuals for terminal conditions at t = 1


def shooting_residual(guess):
l1_0, l2_0 = guess
y0 = [5, 5, l1_0, l2_0] # z1(0), z2(0), lambda1(0), lambda2(0)
sol = solve_ivp(odes, [0, 1], y0, t_eval=[1])
l1_1, l2_1 = sol.y[2, -1], sol.y[3, -1]
return [l1_1 - 20, l2_1 - 20]

# Initial guess for lambda1(0), lambda2(0)


initial_guess = [0.0, 0.0]

# Root finding
sol_root = root(shooting_residual, initial_guess)

# Final results
if sol_root.success:
alpha_1, alpha_2 = sol_root.x
print("Success: Found initial values for lambda_1 and lambda_2")
print(f"alpha_1 (lambda_1(0)) = {alpha_1:.6f}")
print(f"alpha_2 (lambda_2(0)) = {alpha_2:.6f}")
else:
print("Root-finding failed:")
print(sol_root.message)

You might also like