Exemplo n.º 1
0
    F[0] = y[1]  #dx/dt
    F[1] = -y[0] - E * (y[0]**2 - 1) * y[1]  #dy/dt
    return F


#initial conditions
x = 0.  #initial value of x
x2 = numpy.linspace(0, (10 * (numpy.pi)), 10000)
y = numpy.array([0.5, 0.])
xStop = (10 * (numpy.pi))  #final value of x
h1 = 0.02
h2 = 0.05
h3 = 0.1
E = 10.

X, Y1 = RK.integrate(F, x, y, xStop, h1)  #setting up plot for h1
Fig1 = plt.figure()
plt.plot(X, Y1[:, 0], 'r-', label='f(x,t) for h=0.02')
plt.title(
    'Displacement vs. Time using Runge-Kutta method of 4th order for h=0.02')
plt.xlabel('t')
plt.ylabel('x')
plt.legend()

X, Y2 = RK.integrate(F, x, y, xStop, h2)  #setting up plot for h2
Fig3 = plt.figure()
plt.plot(X, Y2[:, 0], label='f(x,t) for h=0.05')
plt.title(
    'Displacement vs. Time using Runge-Kutta method of 4th order for h=0.05')
plt.xlabel('t')
plt.ylabel('x')
Exemplo n.º 2
0
import matplotlib.pyplot as plt
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
Exemplo n.º 3
0
    F[1] = -y[0] - E * (y[0]**2 - 1) * y[1]
    return F


# y = your variables (eg [x,z]
# F = your differentiated values [x', z']

t = 0.0
y = np.array([0.5, 0.])
tStop = 10 * (np.pi)
h = 0.02
h1 = 0.05
h2 = 0.1

#plotting for h=0.02
T, X1 = rk.integrate(F, t, y, tStop, h)
Freq = 1
Figure1 = plt.figure()
plt.plot(T, X1[:, 0])
plt.title('Assessment 3 Q2 plot for h=0.02')
plt.xlabel('Time')
plt.ylabel('X')
plt.grid()

#h=0.05
T, X2 = rk.integrate(F, t, y, tStop, h1)
Fig2 = plt.figure()
plt.plot(T, X2[:, 0], 'r')
plt.title('Assessment 3 Q2 Plot for h = 0.05')
plt.xlabel('Time')
plt.ylabel('X')
Exemplo n.º 4
0
    x=z[0]
    y=z[1]
    diff1=np.zeros(2)
    diff1[0]=y
    diff1[1]=-x-e1*((x**2)-1)*y
    return diff1
###Function used by odeint
def diff2(z,t):
    x=z[0]
    y=z[1]
    diff2=np.zeros(2)
    diff2[0]=y
    diff2[1]=-x-e1*((x**2)-1)*y
    return diff2

R,K=rk.integrate(diff1,tstart,ini1,tstop,h) #Solves using Runge Kutta method, arguments: function, start of interval,initial conditions,end of interval,step size)
ODE=sp.odeint(diff2,ini1,tspace) #Solves using odeint, arguments: function, initial condiions, time interval.

plt.figure(1)
plt.plot(R, K[:,0])
plt.xlabel("Time")
plt.ylabel("Displacement")
plt.title("Motion of a van der Pol oscillator, as found using Runge-Kutta")

plt.figure(2)
plt.plot(K[:,0], K[:,1])
plt.axis('equal')
plt.xlabel("Displacement")
plt.ylabel("Velocity")
plt.title('Phase space of a van der Pol oscillator, as found using Runge-Kutta')
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 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]
    #F[1] = 2.*y[1]/7. + y[0]/7. - x/7.
    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
u = bisectioninc(r, u1, u2)
X, Y = integrate(F, xStart, initCond(u), xStop, h)
printSoln(X, Y, freq)
plt.plot(X, Y)
plt.show()
Exemplo n.º 7
0
## example7_7
import numpy
import run_kut4
import printSoln


def F(x, y):
    F = numpy.zeros((2), dtype=float)
    F[0] = y[1]
    F[1] = -4.75 * y[0] - 10.0 * y[1]
    return F


x = 0.0
xStop = 10.0
y = numpy.array([-9.0, 0.0])
h = 0.1
freq = 0

X, Y = run_kut4.integrate(F, x, y, xStop, h)
printSoln.printSoln(X, Y, freq)
raw_input("\nPress return to exit")
Exemplo n.º 8
0

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


def r(u):  # Boundary condition residual--see Eq. (8.3)
    X, Y = run_kut4.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 = numpy.zeros((2), dtype=float)
    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 = brent.brent(r, u1, u2)  # Compute the correct initial condition
X, Y = run_kut4.integrate(F, xStart, initCond(u), xStop, h)
printSoln.printSoln(X, Y, freq)
raw_input("\nPress return to exit")