Using sage to derive symbolic finite difference approximations to differential equations.

asked 2013-03-31 15:59:49 -0500

boyfarrell gravatar image

updated 2013-03-31 18:49:38 -0500

I am currently working with a finite difference numerical method of advection-diffusion equation. This generated pages of maths, and it is sometime difficult to see where I have made a mistake. As the finite-difference approach is essentially a Taylor expansion with some re-arrangement I was wondering if anybody has used sage to help with writing, re-arrange and factoring finite difference expressions?

I have been using Sage a lot to check my results, but this involves typing long equations, treating subscripts as difference variables, which is error prone. My hope is that by using sage from the first step (i.e. a taylor expansion and so on) not only with the result be less error prone but it will be really quick to experiment with other finite-difference schemes.

So to summarise,

  1. Is there a better way to deal with subscripted/superscripted equation other than make a variables which have horrible names e.g. "phi_jm1_np1" = $\phi_{j-1}^{n+1}$

  2. Has anybody used sage to derive finite difference equations?

Example

For example, let's take the advection term,

$\frac{\partial u}{\partial t} = \boldsymbol{v}\frac{\partial u}{\partial x}$

After apply Crank-Nicholson discretization (which takes the average of the current (n) and future (n+1) time step where the n+1 values are unknowns),

$ \frac{\phi_{j}^{n+1} - \phi_{j}^{n}}{\Delta t} = \boldsymbol{v} \left[ \frac{1-\beta}{2\Delta x} \left( \phi_{j+1}^{n} - \phi_{j-1}^{n} \right) + \frac{\beta}{2\Delta x} \left( \phi_{j+1}^{n+1} - \phi_{j-1}^{n+1} \right) \right]$

Putting the unknowns on the right-hand side enables this to be written in the linear form,

$\beta r\phi_{j-1}^{n+1} + \phi_{j}^{n+1} -\beta r\phi_{j+1}^{n+1} = -(1-\beta)r\phi_{j-1}^{n} + \phi_{j}^{n} + (1-\beta)r\phi_{j+1}^{n}$

This is now a matrix equation which can be solved in the usual way. The solving step I am happy to do outside of Sage. But I would love to to use Sage to help me derive a finite-difference scheme.

edit retag flag offensive close merge delete

Comments

I'm working on the same. Would really appreciate if someone found a way to do this in sage.

MrGreen gravatar imageMrGreen ( 2017-05-10 02:57:12 -0500 )edit