Ejemplo n.º 1
0
do_assert("point", euler.point,
          (0, {'1': 0, '2': 0, '3': 0}))
do_assert("calc derivative", euler.calc_derivative_at_point(),
          {'1': 1, '2': 0, '3': 0})

euler.step_forward(0.1)
do_assert("step forward", euler.point,
          (0.1, {'1': 0.1, '2': 0, '3': 0}))

do_assert("new derivative", euler.calc_derivative_at_point(),
          {'1': 1.1, '2': 0.1, '3': 0})
euler.step_forward(-0.5)
do_assert("step forward 2", euler.point,
          (-0.4, {'1': -0.45, '2': -0.05, '3': 0}))

euler.go_to_input(5, step_size=2)

# notes to help you debug:

# point: (-0.4, (-0.45, -0.05, 0))
# derivative: (0.55, -0.5, -0.1)
# deltas: (2, (1.1, -1, -0.2))

# point: (1.6, (0.65, -1.05, -0.2))
# derivative: (1.65, -0.4, -2.1)
# deltas: (2, (3.3, -0.8, 4.2))

# point: (3.6, (3.95, -1.85, 4))
# derivative: (4.95, 2.1, -3.7)
# deltas: (1.4, (9.8, 4.2, -7.4))
Ejemplo n.º 2
0
assert euler.calc_derivative() == [
    2.1
], "EulerEstimator's calc_derivative() was not right, it should be [2.1], but was {}".format(
    euler.calc_derivative())
print("    EulerEstimator's calc_derivative() Passed!!!\n")

print("    Testing EulerEstimator's step_forward()")
euler.step(-0.5)
assert (round(euler.point[0], 5), [round(x, 5) for x in euler.point[1]]) == (
    0.6, [3.15]
), "EulerEstimator's step_forward() was not right, it should be (0.6, 3.15), but was {}".format(
    tuple(euler.point))
print("    EulerEstimator's step_forward() Passed!!!\n")

print("    Testing EulerEstimator's go_to_input()")
euler.go_to_input(3, 0.5)
assert (round(euler.point[0], 5), [round(x, 5) for x in euler.point[1]]) == (
    3, [9.29]
), "EulerEstimator's go_to_input() was not right, it should be (3, [9.29]), but was {}".format(
    tuple(euler.point))
print("    EulerEstimator's go_to_input() Passed!!!\n")

print('--------------------------------------------')

euler_2 = EulerEstimator(
    {
        'a': (lambda t, x: x[0] + 1),
        'b': (lambda t, x: x[0] + x[1]),
        'c': (lambda t, x: 2 * x[1])
    }, (0, {
        'a': 0,
 
print("\n Testing point after first step")
euler.step_forward(0.1)
assert euler.point == (1.1, {'a':4.2}), 'Incorrect point after first step'
print("     passed")
 
print("\n Testing derivative after first step")
assert euler.calc_derivative_at_point() == {'a':2.1},'Incorrect derivative after first step'
print("     passed")
 
print("\n Testing point after 2nd step")
euler.step_forward(-0.5)
assert (round(euler.point[0],3),{item:round(euler.point[1][item],3) for item in euler.point[1]})== (0.6, {'a':3.15}), 'Incorrect point after 2nd step'
print("     passed")
 
print("\n Testing go_to_input")
euler.go_to_input(3, step_size = 0.5)
assert (round(euler.point[0],3),{item:round(euler.point[1][item],3) for item in euler.point[1]}) == (3, {'a':9.229}), 'Incorrect final result'
print("     passed")
 


euler = EulerEstimator(
                derivatives = {
    'susceptible': (lambda x: -0.0003*x['susceptible']*x['infected']),
    'infected': (lambda x: 0.0003*x['susceptible']*x['infected'] - 0.02*x['infected']),
    'recovered': (lambda x: 0.02*x['infected'])
},
point = (0, {'susceptible': 1000, 'recovered': 0, 'infected': 1}))

euler.plot([0,100], step_size = 0.001, filename = 'test_plot.png')
Ejemplo n.º 4
0
import sys
sys.path.append('src')
from euler_estimator import EulerEstimator

euler = EulerEstimator(derivative=(lambda x: x + 1), point=(1, 4))

assert euler.point == (1, 4), 'Wrong Point'
assert euler.calc_derivative_at_point() == 2, 'Wrong Derivative'

euler.step_forward(0.1)
assert euler.point == (1.1, 4.2), 'Wrong Point'

assert euler.calc_derivative_at_point() == 2.1, 'Wrong Derivative'
euler.step_forward(-0.5)
assert euler.point == (0.6, 3.15), 'wrong point'
euler.go_to_input(3, 0.5)

assert euler.point == (3, 9.29), 'Weong Point'

print('ALL TESTS PASSED')