On Wed, 25 Nov 2020 at 16:37, David Bailey <[email protected]> wrote:
>
> On 19/11/2020 15:37, Oscar Benjamin wrote:
>
>
> I obviously wasn't paying enough attention when I wrote that :)
>
> I know the feeling, because I also managed to garble the differential 
> equation, which should read:
>
> n**2*f(x) - x*Derivative(f(x), x) + (1 - x**2)*Derivative(f(x), (x, 2))
>
> I am pretty sure that should resolve to a simple differential equation after 
> the substitution x=cos(t)

Okay now it works:

In [22]: eq = n**2*f(x) - x*Derivative(f(x), x) + (1 -
x**2)*Derivative(f(x), (x, 2))

In [23]: xf = Function('x') # make x a function of t

In [24]: xs = cos(t)

In [25]: diffx = lambda e, n: diffx(diffx(e, n-1), 1) if n>1 else
Derivative(e, t) / Derivative(xf(t), t)

In [26]: changex = lambda e: e.replace(Derivative, lambda e, vs:
diffx(e, vs[1]))

In [27]: eqf = eq.subs(x, xf(t))

In [28]: assert changex(eqf).doit() == eqf

In [29]: eqsubs = changex(eqf).subs(f(xf(t)), g(t)).subs(xf(t),
xs).doit()

In [30]: str(trigsimp(eqsubs))
Out[30]: 'n**2*g(t) + Derivative(g(t), (t, 2))'

> I tried to follow your prescription but I think the confusion may have set in 
> here,
>
> In [233]: xf = Function('x') # make x a function of t
>
> because I guess I don't need xf because the ODE is already represented in 
> terms of an undefined function f(x).

Yes but we need x to become x(t) because otherwise x.diff(t) will just
be zero. Maybe that is unnecessary though if we skip through to an
equation in a new function g anyway... Maybe I just did this so the
sanity check (changex(eqf).doit() == eqf) works but that step isn't
strictly needed.

> I also had difficulty following what you were doing once I  reached the 
> nested lambda expressions!
>
> It would be great if you could find the time to demonstrate your method on my 
> example - a working example is always worth its weight in gold.
>
> When I originally put in this query, I had assumed that there would be a 
> one-line answer buried in SymPy.

We definitely do need to supply a function for doing this. It's really
not trivial to get this right. If we had something like dsubs or
dchange then we could make use of it internally as well (e.g. in the
ode module).

-- 
Oscar

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxRQmQ7eCK9H0V2HCH0t9L1h9uoyp%2BX50xYPcpp7%3Dejkdw%40mail.gmail.com.

Reply via email to