ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 10 May 2017 09:57:12 +0200Using sage to derive symbolic finite difference approximations to differential equations.https://ask.sagemath.org/question/9967/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) 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](http://en.wikipedia.org/wiki/Crank%E2%80%93Nicolson_method) 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.Sun, 31 Mar 2013 22:59:49 +0200https://ask.sagemath.org/question/9967/using-sage-to-derive-symbolic-finite-difference-approximations-to-differential-equations/Comment by MrGreen for <p>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?</p>
<p>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.</p>
<p>So to summarise,</p>
<ol>
<li><p>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}$</p></li>
<li><p>Has anybody used sage to derive finite difference equations?</p></li>
</ol>
<p><strong>Example</strong></p>
<p>For example, let's take the advection term,</p>
<p>$\frac{\partial u}{\partial t} = \boldsymbol{v}\frac{\partial u}{\partial x}$</p>
<p>After apply <a href="http://en.wikipedia.org/wiki/Crank%E2%80%93Nicolson_method">Crank-Nicholson</a> discretization (which takes the average of the current (n) and future (n+1) time step where the n+1 values are unknowns),</p>
<p>$ \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]$</p>
<p>Putting the unknowns on the right-hand side enables this to be written in the linear form,</p>
<p>$\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}$</p>
<p>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.</p>
https://ask.sagemath.org/question/9967/using-sage-to-derive-symbolic-finite-difference-approximations-to-differential-equations/?comment=37559#post-id-37559I'm working on the same. Would really appreciate if someone found a way to do this in sage.Wed, 10 May 2017 09:57:12 +0200https://ask.sagemath.org/question/9967/using-sage-to-derive-symbolic-finite-difference-approximations-to-differential-equations/?comment=37559#post-id-37559