Ejemplo n.º 1
0

def initCond(u):  # Init. values of [y,y']; use 'u' if unknown
    return np.array([0.0, u])


def r(u):  # Boundary condition residual--see Eq. (8.3)
    X, Y = integrate(F, xStart, initCond(u), xStop, h)
    y = Y[len(Y) - 1]
    r = y[0] - 1.0
    return r


def F(x, y):  # First-order differential equations
    F = np.zeros(2)
    F[0] = y[1]
    F[1] = -3.0 * y[0] * y[1]
    return F


xStart = 0.0  # Start of integration
xStop = 2.0  # End of integration
u1 = 1.0  # 1st trial value of unknown init. cond.
u2 = 2.0  # 2nd trial value of unknown init. cond.
h = 0.1  # Step size
freq = 2  # Printout frequency
u = ridder(r, u1, u2)  # Compute the correct initial condition
X, Y = integrate(F, xStart, initCond(u), xStop, h)
printSoln(X, Y, freq)
input("\nPress return to exit")
Ejemplo n.º 2
0
def initCond(u):  # Initial values of [y,y',y",y"'];
                  # use 'u' if unknown
    return array([0.0, 0.0, u[0], u[1]])

def r(u):  # Boundary condition residuals-- see Eq. (8.7)
    r = zeros(len(u),type=Float64)
    X,Y = integrate(F,x,initCond(u),xStop,h)
    y = Y[len(Y) - 1]
    r[0] = y[2]             
    r[1] = y[3] - 1.0       
    return r
              
def F(x,y):  # First-order differential equations                   
    F = zeros((4),type=Float64)
    F[0] = y[1]
    F[1] = y[2]
    F[2] = y[3]
    if x == 0.0: F[3] = -12.0*y[1]*y[0]**2
    else:        F[3] = -4.0*(y[0]**3)/x
    return F

x = 0.0                    # Start of integration        
xStop = 1.0                # End of integration
u = array([-1.0, 1.0])     # Initial guess for u
h = 0.1                    # Initial step size
freq = 1                   # Printout frequency
u = newtonRaphson2(r,u,1.0e-5)
X,Y = integrate(F,x,initCond(u),xStop,h)
printSoln(X,Y,freq)
raw_input("\nPress return to exit")
Ejemplo n.º 3
0
from printSoln import *
#F is array containing primes of variables x' and z', y is array of variables x,z
#dx/dt=z
def F(t,y):
	F=numpy.zeros(2)
	F[0]=y[1]
	F[1]=(-w**2)*(y[0]**3)#this is saying z'=-w^2x^3, setting up differential equation.
	return F
y=numpy.array([1,0])#initial conditions 
w=1
t=0.#initial value of t
tStop=20.#final value of t
h=0.001#h=size of steps
freq=1
T,Y=RK.integrate(F,t,y,tStop,h)#need to integrate this is in order to use it
printSoln(T,Y,freq)

fig2=plt.figure()

plt.plot(Y[:,0],Y[:,1], label='Velocity against position for an anharmonic oscillator')#since second column in Y was dx/dt and first column was x.
plt.axis('equal')
plt.xlabel('x')
plt.ylabel('dx/dt')
plt.title ('Velocity of oscillator against its position')

def G(t2,y2):
	G=numpy.zeros(2)
	G[0]=y2[1]
	G[1]=(-w**2)*(y2[0])#this is saying z'=-w^2x, setting up differential equation.
	return G
y2=numpy.array([1,0])#initial conditions 
Ejemplo n.º 4
0
## example7_11
from bulStoer import *
from numarray import array,zeros,Float64
from printSoln import *

def F(x,y):
    F = zeros((2),type=Float64) 
    F[0] = y[1]
    F[1] =(-y[1] - y[0]/0.45 + 9.0)/2.0
    return F

H = 0.5
xStop = 10.0
x = 0.0
y = array([0.0, 0.0])
X,Y = bulStoer(F,x,y,xStop,H)
printSoln(X,Y,1)
raw_input("\nPress return to exit")


Ejemplo n.º 5
0
    F = numpy.zeros(2)
    F[0] = y[1]
    F[1] = (-w**2) * (
        y[0]**3)  #this is saying z'=-w^2x^3, setting up differential equation.
    return F


y = numpy.array([1, 0])  #initial conditions
w = 1
t = 0.  #initial value of t
tStop = 20.  #final value of t
h = 0.001  #h=size of steps
freq = 1
T, Y = RK.integrate(F, t, y, tStop,
                    h)  #need to integrate this is in order to use it
printSoln(T, Y, freq)

plt.plot(T, Y[:, 0], 'r-', label='f(t,x)')
plt.xlabel('t')
plt.ylabel('x')
plt.title('Motion of oscillator from t=0 to 20')
plt.legend()
plt.show()

fig1 = plt.figure()  #this is for when you want more than one graph.
y2 = numpy.array([10, 0])
T, Y2 = RK.integrate(F, t, y2, tStop, h)
plt.plot(T, Y2[:, 0], 'b-', label='f(t,x)')
plt.xlabel('t')
plt.ylabel('x')
plt.title(
Ejemplo n.º 6
0
def r(u):
    xs, ys = integrate(F, xStart, initCond(u), xStop, h)
    y = ys[len(ys) - 1]
    r = y[1] - 1.0
    return r


from run_kut4 import *
from ridder import *
from printSoln import *

xStart = 0
xStop = 2.0
h = 0.1
freq = 2

initCond = lambda u: np.array([0.0, u])

u1 = 1
u2 = 10
u = ridder(r, u1, u2)

xs, ys = integrate(F, xStart, initCond(u), xStop, h)
printSoln(xs, ys, freq)

plt.plot(xs, ys[:, 0], 'o-', xs, ys[:, 1], '-')
plt.xlabel('x')
plt.legend(['y', 'dy/dx'], loc=1)
plt.grid()
plt.show()