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

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 close merge delete

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.

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

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.