Exemplo n.º 1
0
def run_example(with_plots=True):
    r"""
    Demonstration of the use of the use of the explicit euler method by solving the
    linear test equation :math:`\dot y = - y`
    
    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance
       
    """

    #Defines the rhs
    def f(t, y):
        ydot = -y[0]
        return N.array([ydot])

    #Define an Assimulo problem
    exp_mod = Explicit_Problem(f,
                               4.0,
                               name='ExplicitEuler Example: $\dot y = - y$')

    #Explicit Euler
    exp_sim = ExplicitEuler(exp_mod)  #Create a explicit Euler solver
    exp_sim.options["continuous_output"] = True

    #Simulate
    t1, y1 = exp_sim.simulate(3)  #Simulate 3 seconds
    t2, y2 = exp_sim.simulate(5, 100)  #Simulate 2 second more

    #Plot
    if with_plots:
        import pylab as P
        P.plot(t1, y1, color="b")
        P.plot(t2, y2, color="r")
        P.title(exp_mod.name)
        P.ylabel('y')
        P.xlabel('Time')
        P.show()

    #Basic test
    nose.tools.assert_almost_equal(float(y2[-1]), 0.02628193)

    return exp_mod, exp_sim
Exemplo n.º 2
0
def run_example(with_plots=True):
    r"""
    Example of the use of Euler's method for a differential equation
    with a discontinuity (state event) and the need for an event iteration.
    
    on return:
    
       - :dfn:`exp_mod`    problem instance
    
       - :dfn:`exp_sim`    solver instance
    """
    exp_mod = Extended_Problem()  #Create the problem

    exp_sim = ExplicitEuler(exp_mod)  #Create the solver

    exp_sim.verbosity = 0
    exp_sim.report_continuously = True

    #Simulate
    t, y = exp_sim.simulate(
        10.0, 1000)  #Simulate 10 seconds with 1000 communications points

    #Plot
    if with_plots:
        import pylab as P
        P.plot(t, y)
        P.title("Solution of a differential equation with discontinuities")
        P.ylabel('States')
        P.xlabel('Time')
        P.show()

    #Basic test
    nose.tools.assert_almost_equal(y[-1][0], 8.0)
    nose.tools.assert_almost_equal(y[-1][1], 3.0)
    nose.tools.assert_almost_equal(y[-1][2], 2.0)

    return exp_mod, exp_sim