Exemple #1
0
sys.path.append(parentdir)

from ode45 import ode45
from odeoptions import Odeoptions


def sin(t, y):
    dydt = np.cos(t)
    return dydt


tspan = [0, 10]
y0 = [0]

myOptions = Odeoptions()  #Create an option with default value.
myOptions.odeset('RelTol', 1e-5)
myOptions.odeset('AbsTol', 1e-8)
myOptions.odeset('Refine', 10)
myOptions.odeset('NormControl', True)
myOptions.odeset('MaxStep', 1)
myOptions.odeset('InitialStep', 0.1)

sol = ode45(sin, tspan, y0, myOptions)

#Plot ode45 approx
fig = plt.figure()
plt.title('Ode45 approx')
plt.xlabel('t')
plt.ylabel('y')
plt.plot(sol.t, sol.y[0])
plt.show()
Exemple #2
0
def events(t, y):
    value = [y[0]]
    isterminal = [1]
    direction = [-1]
    return [value, isterminal, direction]


def dydt(t, y):
    return [y[1], -9.8]


tstart = 0
tfinal = 30
y0 = [0.0, 20.0]
options = Odeoptions()
options.odeset('Refine', 10)
options.odeset('Events', events)

tout = np.array([tstart])
yout = np.array([[y0[0]], [y0[1]]])

teout = np.array([0])
yeout = np.array([[0], [0]])
ieout = np.array([0])

for i in range(10):

    tspan = np.array([tstart, tfinal])
    res = ode45(dydt, tspan, y0, options)
    nt = len(res.t)
Exemple #3
0
#Add parent folder to the path. Code taken from https://codeolives.com/2020/01/10/python-reference-module-in-parent-directory/
import os, sys
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)

from odeoptions import Odeoptions
from ode45 import ode45


def odefcn(x, y):
    epsilon = 1e-2
    return ((1 - x) * y - y**2) / epsilon


epsilon = 1e-6
y0 = [1]
xspan = [0, 2]
options = Odeoptions()
options.odeset('NonNegative', [0])

res = ode45(odefcn, xspan, y0, options)

fig = plt.figure()

plt.title('The knee problem')
plt.xlabel('x')
plt.ylabel('y')
plt.plot(res.t, res.y[0], label='Non-negativity')
plt.show()
#### INPUT 3
INPUT = INPUT + 1
tspan = [
    8.96715752, 9.36035427, 9.75355102, 10.14674776, 10.53994451, 10.93314125,
    11.326338, 11.71953475, 12.11273149, 12.50592824, 12.89912498, 13.29232173,
    13.68551848, 14.07871522, 14.47191197, 14.86510871, 15.25830546,
    15.6515022, 16.04469895
]
y0_1 = [-1.94429498]
y0_2 = [4.96831322, -7.15530037]
y0_3 = [-2.33703264, -2.00682155, -7.14666703]
A = 4.75708095
B = 3.95418388
C = -4.72783249
opts = Odeoptions()
opts.odeset('RelTol', 1.3818760883438436e-04)
opts.odeset('AbsTol', 2.0635002062377786e-05)
opts.odeset('InitialStep', 0.23252681078089357)
precision_mean[INPUT, :] = compute_tests(tspan, y0_1, y0_2, y0_3, opts, A, B,
                                         C)

#### INPUT 4
INPUT = INPUT + 1
tspan = [
    8.39586318, 8.91985661, 9.01857804, 9.08393703, 9.13868348, 10.11329884,
    10.40673529, 10.51920706, 10.99127963, 11.14986855, 11.21056473,
    11.35003671, 11.8351665, 12.08577212, 12.74535624, 12.86117488
]
y0_1 = [-3.76904574]
y0_2 = [5.24019054, 4.65499285]
y0_3 = [-6.51846199, 3.11856858, 1.27300755]
Exemple #5
0
    M[1, 1] = m1 + m2
    M[1, 5] = -m2 * L * np.sin(y[4])
    M[2, 2] = 1
    M[3, 3] = m1 + m2
    M[3, 5] = m2 * L * np.cos(y[4])
    M[4, 4] = 1
    M[5, 1] = -L * np.sin(y[4])
    M[5, 3] = L * np.cos(y[4])
    M[5, 5] = L**2
    return M


tspan = np.linspace(0, 4, 25)
y0 = [0, 4, 2, 20, -np.pi / 2, 2]
options = Odeoptions()
options.odeset('Mass', mass)

res = ode45(dydt, tspan, y0, options)

#Plot ode45 approx
fig = plt.figure()
plt.title('A thrown baton problem with mass matrix M(t,y)')
plt.xlabel('x')
plt.ylabel('y')

for j in range(len(res.t)):
    theta = res.y[4, j]
    X = res.y[0, j]
    Y = res.y[2, j]
    xvals = np.array([X, X + L * np.cos(theta)])
    yvals = np.array([Y, Y + L * np.sin(theta)])
Exemple #6
0
        ((y[0] - mustar) / r23),
        -2 * y[2] + y[1] - mustar * (y[1] / r13) - mu * (y[1] / r23)
    ])


def events(t, y):
    y0 = np.array([1.2, 0, 0, -1.04935750983031990726])
    dDSQdt = 2 * np.dot((y[0:2] - y0[0:2]), y[2:4])
    value = np.array([dDSQdt, dDSQdt])
    isterminal = np.array([1, 0])  # stop at local minimum
    direction = np.array([1, -1])  # [local minimum, local maximum]
    return [value, isterminal, direction]


y0 = [1.2, 0, 0, -1.04935750983031990726]
tspan = [0, 7]
options = Odeoptions()
options.odeset('Events', events)
options.odeset('RelTol', 1e-5)
options.odeset('AbsTol', 1e-4)

res = ode45(dydt, tspan, y0, options)

#PLOT
fig = plt.figure()
plt.title('Restricted three body problem')
plt.xlabel('x(t)')
plt.ylabel('y(t)')
plt.plot(res.y[0], res.y[1])
plt.plot(res.ye[0], res.ye[1], 'ro')
plt.show()