ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 24 Mar 2018 13:11:53 +0100Is there a way to specify the dependent variable in eulers_method?https://ask.sagemath.org/question/41727/is-there-a-way-to-specify-the-dependent-variable-in-eulers_method/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.Fri, 23 Mar 2018 08:19:15 +0100https://ask.sagemath.org/question/41727/is-there-a-way-to-specify-the-dependent-variable-in-eulers_method/Comment by dan_fulea for <p>Consider</p>
<pre><code>sage: u,v = PolynomialRing(QQ,2, "uv").gens()
sage: eulers_method(2*u + v, 1, 5, 0.2, 4)
</code></pre>
<p>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?</p>
<p>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.</p>
https://ask.sagemath.org/question/41727/is-there-a-way-to-specify-the-dependent-variable-in-eulers_method/?comment=41742#post-id-41742Asking 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`.Fri, 23 Mar 2018 20:15:45 +0100https://ask.sagemath.org/question/41727/is-there-a-way-to-specify-the-dependent-variable-in-eulers_method/?comment=41742#post-id-41742Comment by cretinsgaccount for <p>Consider</p>
<pre><code>sage: u,v = PolynomialRing(QQ,2, "uv").gens()
sage: eulers_method(2*u + v, 1, 5, 0.2, 4)
</code></pre>
<p>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?</p>
<p>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.</p>
https://ask.sagemath.org/question/41727/is-there-a-way-to-specify-the-dependent-variable-in-eulers_method/?comment=41744#post-id-41744How is the order of the arguments of f determined from the input expression?Sat, 24 Mar 2018 08:11:37 +0100https://ask.sagemath.org/question/41727/is-there-a-way-to-specify-the-dependent-variable-in-eulers_method/?comment=41744#post-id-41744Comment by dan_fulea for <p>Consider</p>
<pre><code>sage: u,v = PolynomialRing(QQ,2, "uv").gens()
sage: eulers_method(2*u + v, 1, 5, 0.2, 4)
</code></pre>
<p>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?</p>
<p>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.</p>
https://ask.sagemath.org/question/41727/is-there-a-way-to-specify-the-dependent-variable-in-eulers_method/?comment=41745#post-id-41745The 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.Sat, 24 Mar 2018 13:11:53 +0100https://ask.sagemath.org/question/41727/is-there-a-way-to-specify-the-dependent-variable-in-eulers_method/?comment=41745#post-id-41745