Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

finite difference

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) no 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.

click to hide/show revision 2
Oops. Forgot to update the title.

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

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) no 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.

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

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) no 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.