How to implement change of variables for differential equations?

66 views
Skip to first unread message

程迪

unread,
Dec 20, 2016, 10:05:05 AM12/20/16
to sympy
Hi, everyone 

I am going to solve Blasius equation in a tranformed variable.

The equation looks like:

U'''+2U*U''=0
U' == diff(U(y),y)

Change of variable:

x = (y-L)/(y+L)
U = y+V

I cannot implement it correctly using **subs** or **xreplace** due to some limitations(Incorrect behavior with subs and second derivatives)

Currently, I have to implement it this way:

from sympy import *
U,V,x,y = symbols('U,V,x,y')
L = symbols('L')
init_printing()
get_ipython().magic(u'matplotlib inline')

# Blasius equation
beq = 2*Derivative(U(y),y,3)+Derivative(U(y),y,2)*U(y)
Eq(beq,0)

# ## substitution of variables

# from y to x

#new variable: x
#old variable: y
y2x = 2*y/(L+y)-1

# from x to y
x2y = solve(y2x-x,y)[0]

# ## Dictionary for change of variable

sd = zip([diff(U(y),y,i) for i in range(4)],[diff(y+V(y2x),y,i).subs({y:x2y}).simplify().doit() for i in range(4)])
sd


# Change of variables from high order derivative to low order ones.
# *sympy* function **replace** is used rather than **subs** which will give incorrect result.
for sdi in sd[::-1]:
    beq = beq.replace(*sdi)
beq=beq.simplify()

# ## some hacks to generate code for chebfun.chebop in matlab
DIFF=symbols('diff')
sd2 = zip([Derivative(V(x),x,i)for i in range(4)],[DIFF(V,i) for i in range(4)])

beq2  = beq.args[-1]
for sdi in sd2[::-1]:
    beq2 = beq2.replace(*sdi)
beq2

print octave_code(beq2.subs({L:1}))


My question is: is there any way to do this more tight and clean?



Aaron Meurer

unread,
Dec 22, 2016, 2:19:46 PM12/22/16
to sy...@googlegroups.com
That bug has been fixed in master. subs works for me with your
expression beq.subs(reversed(sd)) gives the same result as what you
get with replace (the reversed is important so that it replaces higher
derivatives first).

Aaron Meurer
> --
> 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 sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/7706f861-d4b7-47ea-a705-466e0f29be14%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages