On Wed, 16 Mar 2022 at 23:33, Peter Stahlecker
<
peter.st...@gmail.com> wrote:
>
> Dear Oscar,
Hi Peter!
> Just for my understanding:
>
> Why do you feel, it would be useful for sympy to be able to numerically solve ordinary differential equations?
> Scipy seems to have very good routines to do this - or am I wrong, and they are not as good as I think?
The SciPy routines are good for most usage. The mpmath library that
sympy depends on also has routines that can do high accuracy
multiprecision routines. I don't propose that SymPy should reimplement
what those libraries do.
What is lacking is simply the code that connects from SymPy to the
existing routines in the other libraries. It should be
straight-forward for someone to use SymPy to build up a system of ODEs
and then obtain a numerical solution. Currently that is possible using
lambdify but it's more awkward than it should be given that it is a
common task.
Those of us who are used to numerical libraries are used to the idea
that you have to transform your system to obtain a numerical solution.
Let's give an example:
You want to solve x'' = -x with initial conditions x(0) = 1 and
x'(0)=0. How do I do that with SciPy:
1. First transform the 2nd order ODE into a system of 1st order ODEs:
x' = v
v' = -x
2. Imagine that this is a matrix ODE X' = f(X, t) and create the function f:
def f(X, t):
x, v = X
dxdt = v
dvdt = -x
dXdt = [dxdt, dvdt]
return dXdt
3. Now pass this to the numerical solver:
from scipy.integrate import ...
X0 = [1, 0]
t = np.linspace(0, 100, 1000)
(etc.)
My point is that it would be nice if SymPy could do most of these
steps for you. Why should you have to translate a 2nd order ODE to 1st
order when the computer can do that for you? Why should you have to
explicitly write out the function f when you already have equations
that describe your ODE?
The example I gave above was very simple but it takes time for people
to get their heads round how to implement those steps. (I have run
classes that teach this so I know it's not trivial!)
Yesterday I sat next to a student and carefully compared his code with
the relevant paper and I found three mistakes in one equation. That
equation spanned several lines of code and the comparison (code vs
paper) was painstaking. If the code had built up a SymPy equation then
the equation could have been printed and compared with the paper and
then the differences would have been much more obvious. Also by using
subs etc the chance of a mistake is lower.
That's one reason why it is better to build up your function f using
something like SymPy. Why shouldn't you do that and then ask for the
numerical solution (which the computer would then find with no other
input needed)? The chance of making mistakes could be much smaller.
--
Oscar