Is there a way to specify the dependent variable in eulers_method?

asked 2018-03-23 02:19:15 -0500

anonymous user

Anonymous

updated 2018-03-23 02:20:37 -0500

Consider

sage: u,v = PolynomialRing(QQ,2, "uv").gens()
sage: eulers_method(2*u + v, 1, 5, 0.2, 4)

On the face of it it's ambiguous whether this is solving du/dv = 2*u + v with u(v=1) = 5 or if it's doing dv/du = 2*u + v with v(u=1)=5. In practice it solves the latter, but how is that determined?

It appears to hinge on the order of u and v on the LHS of the first line but I can't find any mention of this in the documentation.

edit retag flag offensive close merge delete

Comments

Asking for the code via ??eulers_method and going till the end, we get it and there is an obvious asymmetry in the variables x0 and y0,

def eulers_method(f,x0,y0,h,x1,algorithm="table"):
    r"""
    ::: lines
    This function is for pedagogical purposes only.
    ::: many other lines
    """
    if algorithm=="table":
        print("%10s %20s %25s"%("x","y","h*f(x,y)"))
    n=int((1.0)*(x1-x0)/h)
    x00=x0; y00=y0
    soln = [[x00,y00]]
    for i in range(n+1):
        if algorithm=="table":
            print("%10r %20r %20r"%(x00,y00,h*f(x00,y00)))
        y00 = y00+h*f(x00,y00)
        x00=x00+h
        soln.append([x00,y00])
    if algorithm!="table":
        return soln

so h is married with x0.

dan_fulea gravatar imagedan_fulea ( 2018-03-23 14:15:45 -0500 )edit

How is the order of the arguments of f determined from the input expression?

cretinsgaccount gravatar imagecretinsgaccount ( 2018-03-24 02:11:37 -0500 )edit

The call in the example is:

eulers_method(2*u + v, 1, 5, 0.2, 4)

The arguments above are placed inside the definition as follows: f becomes 2*u+v and x0 becomes 1, and y0 is 5, and h is the step .2 and we go till x1 which is 4. The question is now, how we compute

(2*u+v)(1,5)

i.e. why do we get

sage: u,v = PolynomialRing(QQ,2, "uv").gens()
sage: (2*u+v)(1,5)
7

and not 11. Instead of answering this question, i would prefer to use a proper function in the call. so that there is no such question, depending on hidden structure. For instance:

sage: f = lambda u,v: 2*u+v
sage: eulers_method(f, 1, 5, 0.2, 4)
         x                    y                  h*f(x,y)
         1                    5     1.40000000000000

a.s.o.

dan_fulea gravatar imagedan_fulea ( 2018-03-24 07:11:53 -0500 )edit