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

Python Differential Equation Solver

This Python code solves a boundary value problem for a system of first-order differential equations using a shooting method. It defines the differential equations, initial conditions, boundary condition residual function, and uses a Runge-Kutta 4 and Ridder's method to iteratively compute the initial condition that satisfies the boundary condition. Finally it prints the solution.

Uploaded by

diego
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)
64 views1 page

Python Differential Equation Solver

This Python code solves a boundary value problem for a system of first-order differential equations using a shooting method. It defines the differential equations, initial conditions, boundary condition residual function, and uses a Runge-Kutta 4 and Ridder's method to iteratively compute the initial condition that satisfies the boundary condition. Finally it prints the solution.

Uploaded by

diego
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

1 #!

/usr/bin/python
2 ## example8_1
3 import numpy as np
4 from run_kut4 import *
5 from ridder import *
6 from printSoln import *
7
8 def initCond(u): # Init. values of [y,y']; use 'u' if unknown
9 return [Link]([0.0, u])
10
11 def r(u): # Boundary condition residual--see Eq. (8.3)
12 X,Y = integrate(F,xStart,initCond(u),xStop,h)
13 y = Y[len(Y) - 1]
14 r = y[0] - 1.0
15 return r
16
17 def F(x,y): # First-order differential equations
18 F = [Link](2)
19 F[0] = y[1]
20 F[1] = -3.0*y[0]*y[1]
21 return F
22
23 xStart = 0.0 # Start of integration
24 xStop = 2.0 # End of integration
25 u1 = 1.0 # 1st trial value of unknown init. cond.
26 u2 = 2.0 # 2nd trial value of unknown init. cond.
27 h = 0.1 # Step size
28 freq = 2 # Printout frequency
29 u = ridder(r,u1,u2) # Compute the correct initial condition
30 X,Y = integrate(F,xStart,initCond(u),xStop,h)
31 printSoln(X,Y,freq)
32 input("\nPress return to exit")
33

You might also like