Work started — Exact Differential Equations

I have decided to start working on my project, even though it is a week early, since I am finished with school and have nothing better to do.  I also like the idea of getting ahead.  I am pushing everything to my github account.  So far, I have implemented exact differential equations, which are cake (i.e., they took me about an hour to code up).  These are equations of the type,Exact Differential Equation FormwheredP:dy=dQ:dx.  If this condition holds, then there exists a functionF(x,y), often called a ‘potential function’ because of some applications of theses equations to physics, such thatdF:dx=PanddF:dy=Q.  This is because mixed partials are equal for continuous functions.  The solution will then just beF(x,y)=C1.  The tricky part is finding the potential functionF(x,y), but it turns out to be easier than you would think.  Because of the fundamental theorem of calculus, the potential function is justExact Differential Equation Solution.  There is a restriction where the rectangle connecting(x0,y0),(x0,y),(x,y0), and(x,y)has to lie entirely in the domain of P andQ, but if we letx0=y0=0, then this is not really a problem for functions that SymPy will encounter.  UPDATE: It turns out this is a problem if the equation has singularities in it.  Fortunately, I was able to code up a workaround that usually works.

So you can see that this is easy to implement in SymPy if you know the above fact.  It turns out that most solution methods for ODEs are like this.  They can be solved in the general case, although usually students are only taught tricks because they are much easier to remember than generally solved formulas.  For example, to solve an exact differential equation, students are often taught to just integratePwith respect toxandQwith respect toy.  It is much simpler for a human being to do that than the above integral, because the integral involves evaluating limits and so on, but for a computer algebra system, the above integral is a one-liner.

By the way, if you want to test my code, you should clone my github repository and switch to the odes branch.  You can do this by typing ‘git clone git://’ and then ‘git checkout odes’ in a Terminal that is cd’d to the directory you want to clone to (of course, you will need git installed first!).  Then type ‘cd sympy’ and ‘./bin/isympy’, which will start SymPy.  Then from there, you can do stuff like ‘dsolve(sin(x)*cos(f(x))+cos(x)*sin(f(x))*f(x).diff(x),f(x)’ (this is exact).  It’s easy to generate an exact differential equation.  Just start with a random function of x and y, then take the derivative of it with respect to x and y each, and do a subs(y,f(x)) on each term (SymPy wants functions for the dependent variable, so we have to use f(x)).  Then do dsolve(<the derivative with respect to x>+<the derivative with respect to y>*f(x).diff(x),f(x)).  You should get your original function equals an arbitrary constant C1.  By the way, diff(<function>,x) will take the derivative of <function> with respect to x.

Up next: Initial conditions, and then first order homogeneous differential equations.


3 Responses to Work started — Exact Differential Equations

  1. vks says:

    You don’t need to clone the whole repository if you have already cloned sympy. Something along the following lines should also work (and use much less bandwidth):

    $ git remote add aaron git://
    $ git fetch aaron
    $ git checkout -b odes aaron/odes

    And you got a local branch to play with.

    • asmeurer says:

      Yeah. I am kind of new to git, so I would take my git advise with a grain of salt. On the other hand, the math has a higher probability of being correct since I am a math major and math is my passion. And if I post Python code, that should also be correct, since that sort of thing is at least verifiable.

      Another thing I should note is that if you pull my branch and then want to play with it later, you should do a ‘git pull’ again before you mess with it. I am always adding patches, so you would want to do that to remove all the bugs/get all the latest features.

      Thanks for pointing out errors in blog. If you find any bugs or inaccuracies in my code, let me know too (though hopefully none of it should make it through the review stage before it gets into the actual SymPy repository).

  2. […] example from my text book.  .  Astute readers of this blog may have noticed that this equation is exact.  It is easier to solve that way, but we will try using the methods outlined above.  First, we […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: